0

Please read after answer.

I have a project that is separate in two modules, one for "SERVICE" and one for "WEB"

Service module works like a REST server and WEB module works like REST client to consume web services from Service module and works like a REST Server to an Angular APP

When i make the request directly to Service Module with Postman attaching a CSV File, works like a charm, but when i try doing the same action with the WEB Module it gets 500 Status Code, and Service module get the follow trace:

SERVICE MODULE

2018/oct/12 23:31:55.922 [http-nio-4501-exec-7] ERROR [dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.multipart.MultipartException: Failed to parse multipart servlet request; nested exception is java.io.IOException: org.apache.tomcat.util.http.fileupload.FileUploadException: the request was rejected because no multipart boundary was found] with root cause
org.apache.tomcat.util.http.fileupload.FileUploadException: the request was rejected because no multipart boundary was found
    at org.apache.tomcat.util.http.fileupload.FileUploadBase$FileItemIteratorImpl.<init>(FileUploadBase.java:831)
    at org.apache.tomcat.util.http.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:256)
    at org.apache.tomcat.util.http.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:280)
    at org.apache.catalina.connector.Request.parseParts(Request.java:2884)
    at org.apache.catalina.connector.Request.parseParameters(Request.java:3232)
    at org.apache.catalina.connector.Request.getParameter(Request.java:1137)
    at org.apache.catalina.connector.RequestFacade.getParameter(RequestFacade.java:381)
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:75)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)

SERVICE MODULE CONTROLLER

    @Autowired
    UtilitarioServicio utilitarioServicio;

    @RequestMapping(path = "/uploadFile", method = RequestMethod.POST,
        consumes = "multipart/form-data")
    public String getUploadedFile(
        @RequestParam("file") MultipartFile file,
        @RequestParam("procesoId") Integer procesoId, 
        @RequestParam("fuenteId") Integer fuenteId) throws IOException {

        utilitarioServicio.getUploadedFile(file, fuenteId, procesoId);

        return "";
    }

WEB MODULE CONTROLLER

    @Autowired
    UtilitarioServicioProxy restProxy;

    @RequestMapping(path = "/uploadFile", method = RequestMethod.POST)
    public String getUploadedFile(
        @RequestParam("file") MultipartFile file,
        @RequestParam("fuenteId") Integer fuenteId,
        @RequestParam("procesoId") Integer procesoId) throws IOException {

        restProxy.getUploadedFile(file, fuenteId, procesoId);

        return "";
    }

Thanks in advance.

Juliano Macedo
  • 668
  • 8
  • 21

1 Answers1

0

I solved my problem getting Bytes from Multipart file, then convert to base64Encoded, and sending like String parameter to Service, and then convert base64Encoded to Bytes and then to File.

WEB

@Override
    public String getUploadedFile(MultipartFile file, Integer fuenteId, Integer procesoId) {

        try {
            byte[] fileBytes = file.getBytes();
            String base64Encoded = DatatypeConverter.printBase64Binary(fileBytes);
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
            map.add("file", base64Encoded);
            map.add("procesoId", procesoId.toString());
            map.add("fuenteId", fuenteId.toString());
            HttpEntity<MultiValueMap<String, String>> requestEntity
                    = new HttpEntity<MultiValueMap<String, String>>(map, headers);
            getRestTemplate().postForObject(url + "/uploadFile", requestEntity, String.class);
        } catch (IOException ex) {
            Logger.getLogger(UtilitarioServicioProxyImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return "";
    }

SERVICE

@RequestMapping(path = "/uploadFile", method = RequestMethod.POST,
            consumes = "application/*")
    public String getUploadedFile(@RequestParam("file") String file,
            @RequestParam("procesoId") Integer procesoId, @RequestParam("fuenteId") Integer fuenteId) throws IOException {

        byte[] fileDecoded = DatatypeConverter.parseBase64Binary(file);
        utilitarioServicio.getUploadedFile(fileDecoded, fuenteId, procesoId);

        return "";
    }