图像被扭曲的某个时候在上传 [英] Image gets distorted sometime while uploading
问题描述
我使用的 MIME多作为上传图片到服务器。有时图像失真。如何解决这个问题?
注:扭曲的手段,一些像素已丢失。
我使用以下code上传:
I am using MIME multipart for uploading images to server. Sometime image get distorted. How to resolve this issue? Note: Distorted means, some pixels are lost. I am using following code for uploading:
File file = new File(filePath[0]);
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost("serverurl");
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("Content-Type",new StringBody("multipart/form-data;charset=utf-8"));
entity.addPart("Content-Length", new StringBody(String.valueOf(file.length())));
entity.addPart("UploadContentPostD", new FileBody(file));
entity.addPart("DocumentName", new StringBody(file.getName()));
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost,localContext);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
扭曲形象是:
Distorted image is:
推荐答案
我使用 Apache的百科全书上传这里是我上传的code这每一次的作品完美...
I use Apache Commons for upload and here is my upload code which works perfectly every time...
public Integer uploadByteArray(String remoteFilename, byte[] bitmapdata){
HttpClient client = new HttpClient();
PostMethod filePost = new PostMethod( URL_PATH);
Integer ret = null;
try {
Part[] parts = new Part[2];
parts[0] = new StringPart("file_name" ,remoteFilename);
ByteArrayPartSource ps = new ByteArrayPartSource("file", bitmapdata);
parts[1] = new FilePart("file", ps);
filePost.setRequestEntity(new MultipartRequestEntity(parts,
filePost.getParams()));
} catch (Exception e) {
Log.d(TAG, e.toString());
}
try {
ret = client.executeMethod(filePost);
Log.d(TAG, "statusCode>>>" + ret);
if(ret != 200){
Log.d(TAG, "Error:" + ret + " from server. Please try again later.");
}else{
responseBody = filePost.getResponseBodyAsString();
Log.d(TAG, filePost.getResponseBodyAsString());
}
} catch (Exception e) {
Log.d(TAG, e.toString());
}
filePost.releaseConnection();
return ret;
}
如果您继续看到您的问题反复出现,我会在你的服务器上,并发送回值在响应,并将其与您已发送了文件的本地MD5文件使用MD5。如果他们不一样,你就知道出事了。
If you continue to see your problem recurring, I would use MD5 on the file you get on the server and send that value back in the response and compare it to a local MD5 of the file you've sent up. If they're not the same, you know something went wrong.
这篇关于图像被扭曲的某个时候在上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!