有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

请在回答后阅读

我有一个项目,分为两个模块,一个用于“服务”,另一个用于“网络”

服务模块的工作原理类似于REST服务器,WEB模块的工作原理类似于REST客户端,以使用服务模块中的WEB服务,并将其工作原理类似于REST服务器和应用程序

当我通过邮递员附加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) 个答案

  1. # 1 楼答案

    我解决了从多部分文件中获取字节的问题,然后将其转换为base64Encoded,并将like字符串参数发送到服务,然后将base64Encoded转换为字节,然后再转换为文件

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