我正在使用 JDK 中的 sun.net.httpserver 为我的设备创建一个简单的 http 服务。
现在我想将帖子表单数据保存到文件中,但我总是发现文件中保存了其他内容。
root@android:/sdcard # cat a.jpg
-----------------------------288271103222787
Content-Disposition: form-data; name="myfile"; filename="a.jpg"
Content-Type: text/plain
CONTENT of the uploaded file
-----------------------------288271103222787--
我该如何处理才能只保存“上传文件的内容”?
我目前的代码是:
private String handleWebSvcApi(HttpExchange exchange, String apiName) throws Exception {
Log.d(TAG, "handleWebSvcApi : " + apiName);
InputStream is = exchange.getRequestBody();
String ret = mWebServiceApiHandler.handleWebServiceApi(apiName, is);
return ret;
}
public String handleWebServiceApi(String apiName, InputStream apiData) throws Exception {
Object ret = null;
if(apiName.equals("upload")) {
String filePath = saveApiDataAsFile(apiData);
}
}
private static String saveApiDataAsFile(InputStream is) throws IOException {
BufferedInputStream in = new BufferedInputStream(is);
File outPutFile = new File(Environment.getExternalStorageDirectory().getPath() + "/newfirmware.zip");
OutputStream os = new FileOutputStream(outPutFile);
int len = 0;
byte buff[] = new byte[8192];
while ((len = in.read(buff)) > 0) {
os.write(buff, 0, len);
}
os.close();
return outPutFile.getAbsolutePath();
}