将文件上传到nanoHttpd服务器Android

时间:2016-11-29 09:01:12

标签: android nanohttpd

如何在NanoHttpd服务方法中检索POST参数。这是我的html表格吼叫

<form  name="a" enctype="multipart/form-data\  method="post" >"
     <input type="file" name"file" multiple/>"+
     <input type="hidden" name="extradata" value="test"/>
     <input type="submit" value="upload" >
</form>

我无法通过表格

获得提交文件的内容长度
  public Response serve(IHTTPSession session) {
        Map<String, String> params = session.getParms();                           
        Map<String, String> files = new HashMap<String, String>();
        if (Method.POST.equals(session.getMethod()) || Method.PUT.equals(session.getMethod())) {
            try {
                session.parseBody(files);    
                StringBuilder builder = new StringBuilder();    
                builder.append("| conLen  " + (session.getHeaders().get("content-length")) + "| \n");    
                Set keys = files.keySet();
                for (Object key : keys) {
                    builder.append(" key: " + key + " value: " + files.get(key) + "\n");
                }
                return new Response(builder.toString());
            } catch (Exception ex) {
                Log.d("server", ex.toString());
            }

        }

}

}

此代码的回复是:

确认为空 key:postData value:------ WebKitFormBoundaryt5gZYrmlLiPVa6aM 内容处理:表格数据; name =“extradata”test ------ WebKitFormBoundaryt5gZYrmlLiPVa6aM -

1 个答案:

答案 0 :(得分:1)

       if (Method.POST.equals(session.getMethod()) || Method.PUT.equals(session.getMethod())) {
            Log.d("server","inside receive file!");
            try{session.parseBody(files);}catch (Exception e){Log.d("server","error on parseBody" +e.toString());}


            File file = new File(files.get("uploadFile"));
}

实际上问题出在NanoHTTPD服务器上。我改为2.1.1版本,现在正在按原样运行。通过上面的代码,我可以获取上传的文件。