0

I want to build simple servlet that accepts single file via post and write it somewhere. The curl will be like:

$ curl -X POST -H "Content-Type: Application/Octet-Stream" --data-binary @thrw.pdf http://localhost:8080/MyServlet

However, my servlet request.getContentLength() returns > 0, but request.getInputStream().available() return 0. Perhaps this is trivial, but i am unable to figure out how to read the uploaded file. As getInputStream().available() returns 0, it is not possible to read the bytes via:

InputStream in = request.getInputStream();
in.read(chunk);

Basically, i want to ask. Is it possible to get the file? If it is, how to achieve that?

Update Actually the InputStream in, is now working - i don't know why, it won't work before - however, it breaks uploaded files (pdf, png, but not txt).

Bagus Tesa
  • 1,317
  • 2
  • 19
  • 42
  • From [docs](http://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#available()): *Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream[...]* ***Note that while some implementations of InputStream will return the total number of bytes in the stream, many will not. It is never correct to use the return value of this method to allocate a buffer intended to hold all data in this stream.*** – BackSlash Nov 08 '15 at 12:10
  • so basically i should not declare container like **byte[] chunk = new byte[request.getInputStream().available];** ? Well, how do i actually retrieve the file, if it within the request? – Bagus Tesa Nov 08 '15 at 12:16

0 Answers0