I am using sun.net.httpserver from JDK to create a simple http service for my device.
Now I want to save the post form data in to a file, but I always found there are additional content saved in the file.
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--
how can I handle this to only save the "CONTENT of the uploaded file"?
My current code is:
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();
}