我目前通过将图像编码为base64字符串将图像发送到我的服务器,如下所示:https://gist.github.com/fabdarice/a4006a97171cc415892e
但是,当我查看HTTP POST请求时,我看到的是这样的内容:
{"login"=>"Jerem", "mobile_upload_file"=>"data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB\nAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/2wBDAQEBAQEBAQEBAQEBAQEBAQEBAQEB\nAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/wAARCAQ4BDgDASIA\nAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQA\nAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3\nODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWm\np6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEA\nAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3....................................}
这种情况持续了一段时间,(大约1百万个字符),似乎让我的服务器变得迟钝。
我想知道这是否是正常行为才能看到发送的字符数量?如果没有,我该如何实现将图像发送到服务器的过程?
感谢您的帮助
答案 0 :(得分:0)
您可以通过Multipart实体在FileBody中发送图像......
public String sendMultipartRequestToServer(BusinessDataObject dataObject,String url) {
String responseString=null;
HttpResponse response;
HttpClient client=new DefaultHttpClient();
HttpPost post=new HttpPost(url);
HttpConnectionParams.setConnectionTimeout(client.getParams(),50000);
MultipartEntity multipartEntity=new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
try {
multipartEntity.addPart(KEY_EMAIL,new StringBody(dataObject.getEmail()));
multipartEntity.addPart(KEY_PASSWORD, new StringBody(dataObject.getPassword()));
multipartEntity.addPart(KEY_IMAGE,new FileBody(new File(Here your image path on your memory)));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
//Setting parameter to post method
post.setEntity(multipartEntity);
try {
response=client.execute(post);
InputStream inputStream=response.getEntity().getContent();
responseString=convertStreamToString(inputStream);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return responseString;
}
如果你找不到类MultipartEntity。那么你必须在你的项目中添加HttpMime.jar。这是迄今为止发送图像的最佳方式我知道这里不需要在Base64中转换图像。你可以通过所选图像的url到FileBody。