使用带有spring for android的压缩jpeg字节数组进行多部分发布请求 [英] Making a multipart post request with compressed jpeg byte array with spring for android

查看:150
本文介绍了使用带有spring for android的压缩jpeg字节数组进行多部分发布请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android应用程序中成功使用spring for android来从/向服务器获取/发布数据。现在,我必须为多部分表单发帖请求,但我一直无法按照我想要的方式运行。

I've been using spring for android successfully in my android app to get/post data from/to the server. Now, I have to do a post request for a multipart form, but I've been unable to get it working the way I want.

使用案例:
1.从图库中选择一张照片
2.使用文件源将其加载到位图
3.将位图压缩为ByteArrayOutputStream
4.通过字节数组(ByteArrayOutputStream.toByteArray())到服务器。 (我需要发送它作为jpeg而不是application / octet-stream)

Use case: 1. Pick a photo from the gallery 2. Load it to a bitmap using the file source 3. Compress the bitmap to a ByteArrayOutputStream 4. Pass the byte array ( ByteArrayOutputStream.toByteArray() ) to the server. (I need to send this as jpeg not application/octet-stream)

上传照片的服务器端点接受只有以下Mime类型的MultipartFile(注意,它不接受MimeType:application / octet-stream ):

The server endpoint for upload photo accepts a MultipartFile with only the following Mime types ( Note, it does not accept MimeType: application/octet-stream ):

GIF("image/gif")
PNG("image/png", "image/x-png")
JPG("image/jpeg", "image/jpg", "image/pjpeg")
BMP("image/bmp")

我尝试过使用示例代码,但到目前为止都没有成功。

I've tried using the sample code, but been unsuccessful so far.

使用以下代码我收到以下错误:
org.springframework.web.bind.MissingServletRequest ParameterException:Required MultipartFile参数'file'不存在

With the following code I get the following error: org.springframework.web.bind.MissingServletRequest ParameterException: Required MultipartFile parameter 'file' is not present

非常感谢有关此问题的帮助。谢谢并保持良好的工作。

Help on this matter is greatly appreciated. Thanks and keep up the good work.

这是我的代码:

bitmap = BitmapFactory.decodeFile("/mnt/sdcard/DCIM/Camera/20130205_162546.jpg");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 60, bos);
byte[] data = bos.toByteArray();

// populate the data to post
MultiValueMap<String, Object> formData = new LinkedMultiValueMap<String, Object>();
formData.add("caption", "Test Caption");
formData.add("file", data);

// The URL for making the POST request
final String url = "http://api.example.com/imageUpload?oauth_token=XXXXXX";

HttpHeaders requestHeaders = new HttpHeaders();

// Sending multipart/form-data
requestHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);

// Populate the MultiValueMap being serialized and headers in an HttpEntity object to use for the request
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<MultiValueMap<String, Object>>(formData, requestHeaders);

// Create a new RestTemplate instance
RestTemplate restTemplate = new RestTemplate(true);

//    // Set a custom message converter that supports the application/json media type
//    final GsonHttpMessageConverter gsonMessageConverter = new GsonHttpMessageConverter();
//    gsonMessageConverter.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON));
//    final ByteArrayHttpMessageConverter byteArrayMessageConverter = new ByteArrayHttpMessageConverter();
//    byteArrayMessageConverter.setSupportedMediaTypes(Collections.singletonList(MediaType.IMAGE_JPEG));
//    final FormHttpMessageConverter formMessageConverter = new FormHttpMessageConverter();
//    formMessageConverter.setSupportedMediaTypes(Collections.singletonList(MediaType.IMAGE_JPEG));
//    restTemplate.getMessageConverters().addAll(Arrays.asList(gsonMessageConverter, byteArrayMessageConverter, formMessageConverter));

// Make the network request, posting the message, expecting a String in response from the server
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);

// Return the response body to display to the user
Log.i(TAG, "**** response.body : " + response.getBody());


推荐答案

我遇到了同样的问题,解决方案是覆盖 org.springframework.core.io.Resource#getFileName()实现。

I've bumped into the same kind of problem and the solution was to override the org.springframework.core.io.Resource#getFileName() implementation.

在我的情况下:

MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>();
// add values to map ... and when it comes to image:
Resource res = new ByteArrayResource(Utils.uriToByteArray(context, itemImage)) {
                    @Override
    public String getFilename() throws IllegalStateException {
        return imageFileName;
    }
};
HttpHeaders imageHeaders = new HttpHeaders();
imageHeaders.setContentType(MediaType.IMAGE_JPEG);
HttpEntity<Resource> imageEntity = new HttpEntity<Resource>(res, imageHeaders);
map.add("item[image]", imageEntity);

其中imageFilename是文件名。稍后将包含为多部分标题: Content-Disposition:form-data; NAME = your_image_form_item; filename =20130520_142401.jpg

Where imageFilename is the file name. That will be later included as multipart header: Content-Disposition: form-data; name="your_image_form_item"; filename="20130520_142401.jpg"

我希望它有所帮助!

这篇关于使用带有spring for android的压缩jpeg字节数组进行多部分发布请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆