MultipartFile Tunneling

I have a controller springthat takes a class named FileUploadBeanon POST. The controller method is as follows:

First controller :

@RequestMapping(value = "/upload", method = RequestMethod.POST)
@ResponseBody
public ResponseEntity<byte[]> uploadFile(final FileUploadBean fileUploadBean) throws IOException {
   // Some code that works fine here
} 

One of the properties FileUploadBeanis of type MultipartFile.

Now I'm trying to add some kind of shell controller (which will run on another server) that also accepts FileUploadBeanand simply redirects the request to the first controller:

The second (wrapping) controller :

@RequestMapping(value="/upload", method = RequestMethod.POST)
@ResponseBody
public ResponseEntity<byte[]> uploadImage(final FileUploadBean fileUploadBean) throws IOException {
  ResponseEntity<byte[]> response = restTemplate.postForEntity([first controller url here], fileUploadBean, byte[].class);
  return response;
}

When I send a request to the first controller, I get:

org.springframework.http.converter.HttpMessageNotWritableException:

JSON: java.io.FileDescriptor , BeanSerializer ( , SerializationFeature.FAIL_ON_EMPTY_BEANS)) ( : com.outbrain.images.beans.FileUploadBean [ "" ] → org.springframework.web.multipart.commons.CommonsMultipartFile [ "fileItem" ] → org.apache.commons.fileupload.disk.DiskFileItem [ "InputStream" ] → java.io.FileInputStream [ "FD" ]); com.fasterxml.jackson.databind.JsonMappingException: java.io.FileDescriptor , BeanSerializer ( , SerializationFeature.FAIL_ON_EMPTY_BEANS)) ( : com.outbrain.images.beans.FileUploadBean [ "" ] → org.springframework.web.multipart.commons.CommonsMultipartFile [ "fileItem" ] → org.apache.commons.fileupload.disk.DiskFileItem [ "InputStream" ] → java.io.FileInputStream [ "FD" ])        org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.writeInternal

?

+4
2

, . , :

@RequestMapping(value = "/upload", method = RequestMethod.POST)
public @ResponseBody
ResponseEntity<byte[]> uploadImage(final FileUploadBean fileUploadBean) throws Exception {
  File file = null;
  try {
    final MultiValueMap<String, Object> requestParts = new LinkedMultiValueMap<>();

    final String tmpImageFileName = IMAGE_TMP_DIR + fileUploadBean.getFile().getOriginalFilename();
    file = new File(tmpImageFileName);
    fileUploadBean.getFile().transferTo(file);
    requestParts.add("file", new FileSystemResource(tmpImageFileName));

    HttpHeaders headers = new HttpHeaders();
    headers.set("Content-Type", "multipart/form-data"); // Sending it like the client-form sends it

    ResponseEntity<byte[]> response = restTemplate.exchange(ImageUrlUtils.getUploadUrl(), HttpMethod.POST, new HttpEntity<>(requestParts, headers),
      byte[].class);

    return new ResponseEntity<>(response.getBody(), response.getStatusCode());
  } catch (Exception ex) {
    return new ResponseEntity<>((ex.getMessage).getBytes("UTF-8"),
      HttpStatus.INTERNAL_SERVER_ERROR);
  } finally {
    if (file != null && file.exists()) {
      file.delete();
    }
  }
}
+8

    @PostMapping(value = "/upload")
public ResponseEntity<Object> upload(MultipartHttpServletRequest request) throws Exception {
    final MultiValueMap<String, Object> requestParts = new LinkedMultiValueMap<>();

    request.getParameterMap().forEach((name, value) -> requestParts.addAll(name, asList(value)));
    request.getMultiFileMap().forEach((name, value) -> {
        List<Resource> resources = value.stream().map(MultipartFile::getResource).collect(toList());
        requestParts.addAll(name, resources);
    });

    HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(requestParts, request.getRequestHeaders());
    return restTemplate.exchange("http://localhost:8081/upload?" + request.getQueryString(),
                                 request.getRequestMethod(), requestEntity, Object.class);

}
0

Source: https://habr.com/ru/post/1533301/


All Articles