JAVA-SPRING-请求被拒绝,因为未找到多部分边界

时间:2018-10-13 04:42:16

标签: java web-services spring-mvc spring-boot

请在回答后阅读。

我有一个项目,该项目分为两个模块,一个模块用于“ SERVICE”,另一个模块用于“ WEB”

Service模块的工作原理类似于REST服务器,而WEB模块的工作原理类似于REST客户端,以使用Service模块中的Web服务,并且工作原理类似于REST Server到Angular APP。

当我通过邮递员附加CSV文件直接向服务模块发出请求时,就像一个超级按钮,但是当我尝试对WEB模块执行相同操作时,它会得到500状态代码,而服务模块会得到以下跟踪:

服务模块

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)

服务模块控制器

    @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模块控制器

    @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 "";
    }

谢谢。

1 个答案:

答案 0 :(得分:0)

我解决了从Multipart文件中获取字节,然后将其转换为base64Encoded,然后将String参数发送到Service,然后将base64Encoded转换为Bytes,然后转换为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 "";
    }

服务

@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 "";
    }