i'm using NanoHTTPD webserver 2.1.0 on a Java Desktop Env. (no Android)
Everything is working fine...but not the file upload using the POST method (PUT is not supported with forms)
Here is my HTML code:
<form method="post" enctype="multipart/form-data">
choose a file<br>
<input name="file" type="file" size="50" maxlength="100000"">
<button type="submit">upload</button>
</form>
And here is my server methode:
public Response serve(IHTTPSession session) {
if (session.getMethod() == Method.POST) {
Map<String, String> files = new HashMap<String, String>();
session.parseBody(files);
//this prints {file=C:\path-to-java-tmp-files\NanoHTTPD-4635244586997909485}
//the number is always different
System.out.println(files.toString());
} else {
//page containing the index.html including the form
return page;
}
}
And here is the Problem: The temp-file doesn't exist. There is another temp-file with a different "number" at the end existing and this seems to be the correct file, because the content is the same as the content of the uploaded file. So how to get the correct temp-filename?
Another problem is: The temp-file contains the hole POST content:
-----------------------------115801144322347
Content-Disposition: form-data; name="file"; filename="filename.txt"
Content-Type: application/octet-stream
-->content of the uploaded file
-----------------------------115801144322347--
This is a problem if the content is a pic or a binary.
It seems NanoHTTPD doesn't do any spezial with a POST request. it does always the same...saving the request to a tmp-file and serving a page. So: - How to get the correct temp-file? --> I think this is a bug. I'm getting a correct path and name, but the "number" is broken. idk...should i temporarily change the java tmp-path if an upload happens and then delete the file always. Then I only have one tmp-file independent of any naming? - how to kill the html request header out of the file
Or i'm doing something wrong? Is this the correct way to upload files to nanohttpd?
thx for your help!