使用Spring for Android进行压缩的JPEG字节数组的多部分POST请求
使用Spring for Android进行压缩的JPEG字节数组的多部分POST请求
我在我的Android应用程序中成功地使用Spring for Android来从/向服务器获取/发送数据。现在,我需要进行一个多部分表单的POST请求,但是我一直无法以我想要的方式使其工作。
使用情况:
1. 从图库选择一张照片
2. 使用文件源将其加载到位图中
3. 将位图压缩到ByteArrayOutputStream中
4. 将字节数组(ByteArrayOutputStream.toByteArray())传递给服务器(我需要将其作为jpeg而不是application/octet-stream发送)
上传照片的服务器端点只接受以下Mime类型的MultipartFile(注意,它不接受MimeType: application/octet-stream):
GIF("image/gif") PNG("image/png", "image/x-png") JPG("image/jpeg", "image/jpg", "image/pjpeg") BMP("image/bmp")
我尝试使用示例代码,但是到目前为止没有成功。
使用以下代码我得到了以下错误:
org.springframework.web.bind.MissingServletRequest ParameterException: Required MultipartFile parameter 'file' is not present
非常感谢对此事的帮助。谢谢并继续保持良好的工作。
这是我的代码:
bitmap = BitmapFactory.decodeFile("/mnt/sdcard/DCIM/Camera/20130205_162546.jpg"); ByteArrayOutputStream bos = new ByteArrayOutputStream(); bitmap.compress(CompressFormat.JPEG, 60, bos); byte[] data = bos.toByteArray(); // 填充要发送的数据 MultiValueMapformData = new LinkedMultiValueMap (); formData.add("caption", "Test Caption"); formData.add("file", data); // 进行POST请求的URL final String url = "http://api.example.com/imageUpload?oauth_token=XXXXXX"; HttpHeaders requestHeaders = new HttpHeaders(); // 发送multipart/form-data requestHeaders.setContentType(MediaType.MULTIPART_FORM_DATA); // 将要序列化的MultiValueMap和头信息填充到HttpEntity对象中以供请求使用 HttpEntity > requestEntity = new HttpEntity >(formData, requestHeaders); // 创建一个新的RestTemplate实例 RestTemplate restTemplate = new RestTemplate(true); // // 设置一个支持application/json媒体类型的自定义消息转换器 // 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)); // 发起网络请求,发布消息,并期望从服务器返回一个字符串作为响应 ResponseEntity response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class); // 返回响应体以显示给用户 Log.i(TAG, "**** response.body : " + response.getBody());
问题的原因是在使用Spring for Android进行多部分POST请求时,无法正确压缩JPEG字节数组。解决方法是重写org.springframework.core.io.Resource#getFileName()方法来指定文件名。具体实现如下:
MultiValueMapmap = new LinkedMultiValueMap (); // 添加其他参数到map中... // 当涉及到图片时: Resource res = new ByteArrayResource(Utils.uriToByteArray(context, itemImage)) { public String getFilename() throws IllegalStateException { return imageFileName; } }; HttpHeaders imageHeaders = new HttpHeaders(); imageHeaders.setContentType(MediaType.IMAGE_JPEG); HttpEntity imageEntity = new HttpEntity (res, imageHeaders); map.add("item[image]", imageEntity);
其中,imageFileName是文件名,它将作为multipart头的一部分:Content-Disposition: form-data; name="your_image_form_item"; filename="20130520_142401.jpg"。
希望这能帮到你!如果你仍然无法实现所需的结果,请参考此链接寻求解决方案:stackoverflow.com/questions/54429549/…