Rectangle 27 4

The filename is not URL encoded. NanoHTTPD looks in the Content-Type header to find a charset directive. Unfortunately it seems that for multipart/form-data the only valid directive is boundary, and NanoHTTPD falls back on ASCII.

Besides modifying NanoHTTPD's ContentType.getEncoding() method, the only workaround I can think of is to:

  • Add accept-charset="UTF-8" to ensure the form encoding. <div id="content"> <form action="" method="post" enctype="multipart/form-data" accept-charset="UTF-8"> <input type="file" name="myupload" multiple="multiple" /> <input type="submit" value="Upload Files"/> </form>
  • Add the charset directive to the NanoHTTPD session before parsing the content, the ContentType class has a method which does exactly that: @Override public Response serve(IHTTPSession session) { // add "; charset=UTF-8" to the content type ContentType ct = new ContentType(session.getHeaders().get("content-type")).tryUTF8(); session.getHeaders().put("content-type", ct.getContentTypeHeader()); ... // no need to URL decode String filename=session.getParms().get(key);

And more, does it mean that this is a bug in NanoHTTPD ?

I think it's not really a bug : NanoHttpt has no way to tell wich encoding is used. On the other hand the application knows which encoding was declared in the form. It would be nice to be able to set an other default encoding than ASCII

Why do I get messy code of chinese filename when I upload files to Nan...

android nanohttpd