In my Java application I use the com.sun.net.httpserver
classes for a small built-in web server. I also want to support file uploads. From a web page the user can select a file in a standard HTML form:
<form action="${home_url}/upload" enctype="multipart/form-data" method="post">
<fieldset>
<input type="file" id="inp" name="filename" size="50"><br>
<input type="submit" id="btn" value="Upload">
</fieldset>
</form>
In the handler for that upload page (where data is posted to) I do the following:
InputStreamReader input = new InputStreamReader(exchange.getRequestBody(), "utf-8");
BufferedReader reader = new BufferedReader(input);
String line = reader.readLine();
while(line != null)
{
parser.addLine(line);
line = reader.readLine();
}
However, the first time I call reader.readLine()
the String that I receive does not contain the first line of the original file. Instead, this first line I get in Java code is part of a line somewhere in the middle of the uploaded file.
It seems the first part of the file is lost somehow. This is reproducible, and what I get from the first readLine()
is always the same (as always the same amount of bytes get lost during upload).
Any ideas?