正在将文件上传到 nanoHttpd 服务器 Android
Uploading file to nanoHttpd server Android
如何在 NanoHttpd Serve 方法中检索 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());
}
}
}
}
此代码的响应是:
conLen 为空
键:postData 值:------WebKitFormBoundaryt5gZYrmlLiPVa6aM
Content-Disposition:表单数据; name="extradata" test ------WebKitFormBoundaryt5gZYrmlLiPVa6aM--
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 版本,现在可以正常工作了。使用上面的代码,我可以获取上传的文件。
如何在 NanoHttpd Serve 方法中检索 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());
}
}
}
}
此代码的响应是:
conLen 为空 键:postData 值:------WebKitFormBoundaryt5gZYrmlLiPVa6aM Content-Disposition:表单数据; name="extradata" test ------WebKitFormBoundaryt5gZYrmlLiPVa6aM--
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 版本,现在可以正常工作了。使用上面的代码,我可以获取上传的文件。