Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

@POST
@Path("/getphotos")
@Produces(MediaType.TEXT_HTML)
public String getPhotos() throws IOException{
    // DataInputStream rd = new DataInputStream(request.getInputStream());
    BufferedReader rd = new BufferedReader(
        new InputStreamReader(request.getInputStream(), "UTF-8")
    );
    String line = null;
    String message = new String();
    final StringBuffer buffer = new StringBuffer(2048);
    while ((line = rd.readLine()) != null) {
        // buffer.append(line);
        message += line;
    }
    System.out.println(message);
    JsonObject json = new JsonObject(message);
    return message;
}

The code above is for my servlet. Its purpose is to get a stream, make a a Json file from it, and then send the Json to the client back. But in order to make Json, I have to read BufferedReader object rd using a "while" loop. However I'd like to convert rd to string in as few lines of code as possible. How do I do that?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
241 views
Welcome To Ask or Share your Answers For Others

1 Answer

From Java 8:

rd.lines().collect(Collectors.joining());

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...