Reputation: 1521
I am currently writing a programming a webserver, I am working on the HTTP PUT method. When the client is connect and he types something like this:
PUT /newfile.txt HTTP/1.1
Host: myhost
BODY This text is what has to be written to the new created file
I want to be able to write the BODY of the client request into the created file.
This is what I have to far, it work but after I press enter it stays there.
InputStream is = conn.getInputStream();
OutputStream fos = Files.newOutputStream(file);
int count = 0;
int n = 10;
while (count < n) {
int b = is.read();
if (b == -1) break;
fos.write(b);
++count;
}
fos.close();
conn.close();
Upvotes: 0
Views: 75
Reputation: 7057
You may try this
Scanner sc = new Scanner(is);
Scanner
will enable you to read file easily to a String
. You may separate BODY using regex. You just have to provide it as an argument of the next
method of Scanner
.
After separating, you'll need to write that String
to the file. Just use
FileWriter writer = new FileWriter(file);
writer.write(bodyContentString);
writer.flush();
writer.close();
Good luck.
Upvotes: 1