使用两个应用程序之间的RestTemplate在post请求中传输文件和表单数据

时间:2018-06-19 10:22:07

标签: java spring spring-mvc spring-boot

我已经设置了两个

的应用程序
  1. 文件服务器(8000)
  2. App server(4000)
  3. 两者都单独工作(使用Postman测试)。

    当我从UI / Postman接收文件并将数据表格形式发送到App服务器并使用Rest模板传输到Document并将表格数据传输到文件服务器时,会出现问题。 同时App服务器给我一个以下错误。

    15:33:56.360 [http-nio-4206-exec-10] DEBUG o.s.web.client.RestTemplate - Created POST request for "http://localhost:8000/docserver/api/webservicegetway/savefile"
    15:33:56.428 [http-nio-4206-exec-10] DEBUG o.s.web.client.RestTemplate - Setting request Accept header to [application/json, application/*+json]
    15:33:56.909 [http-nio-4206-exec-10] DEBUG o.s.web.client.RestTemplate - POST request for "http://localhost:8000/docserver/api/webservicegetway/savefile" resulted in 406 (null); invoking error handler
    15:33:56.916 [http-nio-4206-exec-10] ERROR o.a.c.c.C.[.[.[.[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [/appserver] threw exception [Request processing failed; nested exception is org.springframework.web.client.HttpClientErrorException: 406 null] with root cause
    org.springframework.web.client.HttpClientErrorException: 406 null
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:94)
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:79)
    at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63)
    at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:766)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:724)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:680)
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:600)
    at com.org.appserver.main.services.DocServerService.transferFileToDocServer(DocServerService.java:101)
    

    并在doc服务器上关注问题

    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) ~[tomcat-embed-core-8.5.29.jar:8.5.29]
    at org.apache.tomcat.util.http.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:256) ~[tomcat-embed-core-8.5.29.jar:8.5.29]
    at org.apache.tomcat.util.http.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:280) ~[tomcat-embed-core-8.5.29.jar:8.5.29]
    at org.apache.catalina.connector.Request.parseParts(Request.java:2868) ~[tomcat-embed-core-8.5.29.jar:8.5.29]
    at org.apache.catalina.connector.Request.parseParameters(Request.java:3216) ~[tomcat-embed-core-8.5.29.jar:8.5.29]
    at org.apache.catalina.connector.Request.getParameter(Request.java:1137) ~[tomcat-embed-core-8.5.29.jar:8.5.29]
    at org.apache.catalina.connector.RequestFacade.getParameter(RequestFacade.java:381) ~[tomcat-embed-core-8.5.29.jar:8.5.29]
    

    我的RestWebservice代码低于一: -

    App Server: -

    @RequestMapping(value = "/uploadTest", method = RequestMethod.POST,consumes=MediaType.MULTIPART_FORM_DATA_VALUE)
    private WebserviceResultSet fileUpload_Test( @RequestParam( "file" ) MultipartFile file,@RequestParam( "filename" ) String filesource,@RequestParam( "filepassword" ) String filepassword, Model p_model ) throws IOException, URISyntaxException {
    
        WebserviceResultSet result=new WebserviceResultSet();  
        if ( file.isEmpty() ) {
            result.addErrormessage("Empty or Corrupted file upload");
            return result;
        }
    
                byte[] l_bytes = file.getBytes();
                Path l_path = Paths.get("C:\\Eclipse_workspace\\"+ file.getOriginalFilename() );
                Path p=Files.write( l_path, l_bytes );
    
    
    
                DocServerService docServerService=new DocServerService();
                docServerService.transferFileToDocServer(file, filesource, filepassword);
    
    
    
        return result;
    
    }
    

    Doc Server: -

    @RequestMapping(value = "savefile", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    private WebserviceResultSet saveFile(@RequestParam("file") MultipartFile p_file,
            @RequestParam("filename") String filename,
            @RequestParam("filepassword") String filepassword, Model p_model,
            HttpServletRequest request) {
    
        WebserviceResultSet savefile = new WebserviceResultSet();
    
        String applicationctxpath = request.getRealPath(request.getContextPath());
    
        String directoryName = applicationctxpath + File.separator +ROOT_PATH + File.separator +filename;
    
        File directory = new File(directoryName);
        if (!directory.exists()) {
            directory.mkdirs();
    
        }
    
        try {
    
            byte[] l_bytes = p_file.getBytes();
            Path l_path = Paths
                    .get(directoryName + File.separator +  p_file.getOriginalFilename());
            Files.write(l_path, l_bytes);
    
            savefile.setStatus(com.main.general.ResultConstent.STATUS_OK);
    
    
        } catch (IOException e) {
    
            savefile.addErrormessage("Exception occured while saving file in Doc Server");
    
        }
    
        return savefile;
    }
    

0 个答案:

没有答案