Tôi đã sử dụng ứng dụng Android cho android thành công trong ứng dụng android của mình để nhận/đăng dữ liệu từ/đến máy chủ. Bây giờ, tôi phải thực hiện yêu cầu đăng bài cho một biểu mẫu nhiều hình , nhưng tôi không thể làm cho nó hoạt động theo cách tôi muốn.Thực hiện yêu cầu đăng nhiều phần với mảng byte jpeg nén với mùa xuân cho android
Sử dụng trường hợp: 1. Chọn một ảnh từ thư viện 2. tải nó vào một bitmap sử dụng nguồn tập tin 3. Nén bitmap để một ByteArrayOutputStream 4. Vượt qua mảng byte (ByteArrayOutputStream.toByteArray()) đến máy chủ. (Tôi cần phải gửi thông điệp này như jpeg không application/octet-stream)
Các thiết bị đầu cuối máy chủ cho ảnh upload chấp nhận một MultipartFile với chỉ có các loại Mime sau (Lưu ý, nó không chấp nhận Loại Mime: application/octet-stream):
GIF("image/gif")
PNG("image/png", "image/x-png")
JPG("image/jpeg", "image/jpg", "image/pjpeg")
BMP("image/bmp")
Tôi đã thử sử dụng sample code nhưng đã không thành công cho đến nay.
Với đoạn mã sau tôi nhận được thông báo lỗi sau: org.springframework.web.bind.MissingServletRequest ParameterException: tham số yêu cầu MultipartFile 'tập tin' không phải là hiện tại
Trợ giúp về vấn đề này được đánh giá rất nhiều. Cảm ơn bạn, hãy tiếp tục làm tốt như vậy nhé.
Dưới đây là mã của tôi:
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());