What mimeType shall I set when uploading files by HttpClient?
-
12-10-2022 - |
Question
My code is below. I found some types of files can't be uploaded correctly with this code, such as the 7-zip compressed files. Also, there are some uploaded correctly.
The uploaded file size is smaller than the whole file when this problem occurred. And if I changed the mime type, the uploaded file size will be changed.
So I guess it's the problem of the mime type. What mime type shall I set to upload files of different types? Shall I set it separately for different file types, or set a general type for all of the file types?
public void DoUpload(String url, String filename) {
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpPost httppost = new HttpPost(url);
File file = new File(filename);
MultipartEntity mpEntity = new MultipartEntity();
ContentBody cbFile = new FileBody(file, "application/x-normal");
mpEntity.addPart("userfile", cbFile);
httppost.setEntity(mpEntity);
//writeLog("executing request " + httppost.getRequestLine());
initTime = System.nanoTime();
try {
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
//writeLog(response.getStatusLine().toString());
if (resEntity != null) {
//writeLog(EntityUtils.toString(resEntity));
}
if (resEntity != null) {
resEntity.consumeContent();
}
writeLog(String.format("Upload: [%s] Success!!! ...upload time: %d ms",
file.toString(), (System.nanoTime()-initTime)/1000000));
}
catch (Exception e) {
writeLog(String.format("Upload: [%s] FAILED!!! error: %s", file.toString(), e.toString()));
}
httpclient.getConnectionManager().shutdown();
}
No correct solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow