NanoHTTPD: upload file using html form (POST)

I am using NanoHTTPD webserver 2.1.0 on the Java Desktop Env. (without Android)

Everything works fine ... but not uploading the file using the POST method (PUT is not supported by 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-side method:

 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: temp file does not exist. In the end, there is another temporary file with a different β€œnumber”, and this seems to be the correct file because the contents are the same as the contents of the downloaded file. So how to get the correct name temp-filename?

Another problem: the temp file contains the contents of the POST hole:

 -----------------------------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 pic or binary.

NanoHTTPD does not seem to make any special requests with POST. it is always the same ... saving the request to a tmp file and serving the page. So: - How to get the correct temp file? -> I think this is a mistake. I get the correct path and name, but the "number" is broken. idk ... should I temporarily change the java tmp path if downloading occurs and then always delete the file. Then I have only one tmp file, regardless of any naming convention? - how to kill html request header from file

Or am I doing something wrong? Is this the right way to upload files to nanohttpd?

thank you for your help!

+6
source share
1 answer

I realized this shortly after I posted the message "did you find a fix?" question, but forgot to post it until you answered today.

 <form method="post" enctype="multipart/form-data" action="http://whatever.com/path/"> <input type="file" name="file" /> <input type="hidden" name="extra_data" value='blah blah blah' /> <input type="submit" value="Send" /> </form> 

And Java code:

 if (session.getMethod() == Method.POST) { try { session.parseBody(files); } catch (IOException e1) { e1.printStackTrace(); } catch (ResponseException e1) { e1.printStackTrace(); } extraData = session.getParms().get("extra_data"); File file = new File(files.get("file")); } 

The important thing is that the data you send contains the file data, file name, and mime file type.

In my case, I used a python request for POST and did not send enough data to it. The correct message structure will be as follows:

 file_data = { 'file': ('test.png', open('../some_file.png', 'rb'), 'image/png') } requests.post("http://whatever.com/path", data = { 'extra_data': "blah blah blah" }, files = file_data) 

The above code is read from "some_file.png", but tells the server that it is called "test.png".

Hope this helps!

+3
source

Source: https://habr.com/ru/post/972587/


All Articles