我的代码如下。我发现使用此代码无法正确上载某些类型的文件,例如7-zip压缩文件。此外,还有一些正确上传。
发生此问题时,上载的文件大小小于整个文件。如果我更改了mime类型,则会更改上传的文件大小。
所以我猜这是mime类型的问题。我应该设置哪种mime类型来上传不同类型的文件?我应该为不同的文件类型单独设置,还是为所有文件类型设置一般类型?
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();
}