Java 如何通过RESTAPI上传多部分文件?

Java 如何通过RESTAPI上传多部分文件?,java,rest,file-upload,multipartform-data,Java,Rest,File Upload,Multipartform Data,我知道如何从postman上传多部分文件,但是如何通过RESTAPI实现同样的操作。当我点击postman时,consumer API工作得很好,但在REST中做同样的事情时,它不起作用 我通过REST做了同样的事情,但它不起作用: HttpHeaders=newhttpheaders(); headers.setContentType(MediaType.MULTIPART\u FORM\u DATA); 多值映射体 =新的LinkedMultiValueMap(); 添加(“文件”,文件)

我知道如何从postman上传多部分文件,但是如何通过RESTAPI实现同样的操作。当我点击postman时,consumer API工作得很好,但在REST中做同样的事情时,它不起作用

我通过REST做了同样的事情,但它不起作用:

HttpHeaders=newhttpheaders();
headers.setContentType(MediaType.MULTIPART\u FORM\u DATA);
多值映射体
=新的LinkedMultiValueMap();
添加(“文件”,文件);
HttpEntity请求实体
=新的HttpEntity(主体、标题);
字符串serverUrl=”http://localhost:9001/communication/api/messageEngine/event/RECIPT/sendEmailAttachment";
参数化类型引用参数化类型引用=
新的ParameteredTypeReference(){};
RestTemplate RestTemplate=新RestTemplate();
试一试{
响应结果=
exchange(serverUrl、HttpMethod.POST、requestEntity、parameterizedTypeReference);
if(result.getStatusCode().is2xxSuccessful()==false){
抛出新的DenoTreasableException();
}
}捕获(例外e){
e、 printStackTrace();
投掷e;
}
目标API或消费者API

     @CrossOrigin
    @RequestMapping(method = RequestMethod.POST, value = "/event/{event}/sendEmailAttachment", consumes = {"multipart/form-data"})
    public ApiResponse<Object> sendReceiptWithAttachment(@RequestPart("file") MultipartFile file, @PathVariable("event") String event) {

        LidsysUtil.messageId.set(String.valueOf(new Date().getTime()));

        MessageTracker tracker = new MessageTracker(LidsysUtil.messageId.get(), event);
        LidsysUtil.tracker.set(tracker);
        LOGGER.info("Executing Message Id : {} ", LidsysUtil.messageId.get());
        LOGGER.info("Request received for event : {}", event);
       // LOGGER.info("Request Body : {}", LidsysUtil.displayJSON(requestBody));
        Map<String, Object> request = messageEngineService.initiateEmailwithAttachmentV2( file, event);

        return new ApiResponse<>(APIResponseKey.ALL_GOOD, messageEngineService.execute(request, event), null);
    }
目标微服务异常

020-10-21 19:11:18,237 [ERROR]---[DirectJDKLog.java]---[http-nio-8010-exec-2]: Servlet.service() for servlet [dispatcherServlet] in context with path [/dataacquisition] threw exception [Request processing failed; nested exception is org.springframework.web.client.HttpClientErrorException: 400 null] with root cause
org.springframework.web.client.HttpClientErrorException: 400 null
2020-10-21 19:11:17,445 [ERROR]---[HttpLoggingFilter.java]---[http-nio-9001-exec-10]: null

在实例化
参数化TypeReference
对象时,为什么要以两种不同的方式引用
ApiResponse
?(您同时使用简单名称和后来的完整限定名称
com.loylty.dataacquisition.model.ApiResponse
)这是否意味着这可能是两个不同的类

另外,在使用者端,您已经声明了
sendReceiptWithAttachment()
方法以返回
ApiResponse
,而不是
ApiResponse


确保消费者和生产者都同步。

我找到了解决方案,我在地图中添加了文件,而不是资源类型所需的文件

MultiValueMap<String, Object> body
                        = new LinkedMultiValueMap<>();
                body.add("file", file);
多值映射体
=新的LinkedMultiValueMap();
添加(“文件”,文件);
解决方案

 Resource rfile = new FileSystemResource(file)
 MultiValueMap<String, Object> body
                        = new LinkedMultiValueMap<>();
                body.add("file", rfile );
Resource rfile=新文件系统资源(文件)
多值映射体
=新的LinkedMultiValueMap();
添加(“文件”,rfile);

当您说它不起作用时,它是否会打印任何有助于理解问题的有意义的信息?是否有异常?@EvrisTzam是的,它会打印出来,但没有多大帮助。请更新您的问题并添加它。@EvrisTzam已更新为异常