gpt4 book ai didi

java - 接收多部分文件请求的 415 Unsupported Media Type

转载 作者:行者123 更新时间:2023-12-04 10:16:19 27 4
gpt4 key购买 nike

我想使用javax和spring框架(不是spring boot)编写一个API,它应该接受一个发布请求并将文件上传到 Controller 在tomcat服务器中运行的服务器。问题是我有所有必需的代码,但是当我调用 API 时,我得到 415 unsupported type error。所需代码定义如下:

@POST
@Path("file/{productName}")
public String uploadFile(@RequestParam("file") MultipartFile file, @PathParam("productName") String productName) throws IOException {
File folderPath = new File("/Users/homeDirectory/Desktop/FileUploadTest");
FileOutputStream fout = new FileOutputStream(folderPath+"/"+ file.getOriginalFilename());
fout.write(file.getBytes());
fout.close();
return "Response";

我有另一个客户端(子模块)正在使用 Unirest 调用上传端点。文件类型可以是 CSV 或 PNG
File f = new File("/Users/homeDirectory/Desktop/aggregateReport.csv");
HttpResponse<String> response = Unirest.post("http://localhost:8080/report-rest/report/v1/file/test123")
.field("file", f).asString();

在服务器端,我看到以下错误:

Apr 04, 2020 10:13:33 PM org.apache.cxf.jaxrs.utils.JAXRSUtils logMessageHandlerProblem
SEVERE: No message body reader has been found for class org.springframework.web.multipart.MultipartFile, ContentType: multipart/form-data;boundary=1MbhkGkO-__me1XVpPXpsLEWcSMn4ltIz4
Apr 04, 2020 10:13:33 PM org.apache.cxf.jaxrs.impl.WebApplicationExceptionMapper toResponse
WARNING: javax.ws.rs.WebApplicationException: HTTP 415 Unsupported Media Type
at org.apache.cxf.jaxrs.utils.JAXRSUtils.readFromMessageBody(JAXRSUtils.java:1339)
at org.apache.cxf.jaxrs.utils.JAXRSUtils.processParameter(JAXRSUtils.java:824)
at org.apache.cxf.jaxrs.utils.JAXRSUtils.processParameters(JAXRSUtils.java:788)
at org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor.processRequest(JAXRSInInterceptor.java:212)
at org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor.handleMessage(JAXRSInInterceptor.java:77)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:308)
at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121)
at org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:254)
at org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:234)
at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:208)
at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:160)
at org.apache.cxf.transport.servlet.CXFNonSpringServlet.invoke(CXFNonSpringServlet.java:180)
at org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:299)
at org.apache.cxf.transport.servlet.AbstractHTTPServlet.doPost(AbstractHTTPServlet.java:218)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
at org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:274)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
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)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:496)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1468)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)

最佳答案

@Paul 感谢您提供正确的方向。在此处发布正确的代码,以便其他人提供帮助

@POST
@Path("file/{productName}")
public Response uploadFile(MultipartBody multipartBody, @PathParam("productName") String productName) {
List<Attachment> attachments = multipartBody.getAllAttachments();
List<DataHandler> dataHandlers = attachments.stream().map(Attachment::getDataHandler).collect(Collectors.toList());
dataHandlers.forEach(dataHandler -> {
String fileName = dataHandler.getName();
try {
InputStream fileContentStream = dataHandler.getInputStream();
if (Objects.isNull(fileContentStream)) {
LOGGER.error("Null Stream received in request");
} else {
String directoryPath = fileUtil.createDirectoryToSaveFiles(uploadProperties.getSourceDirectoryLocation(), productName);
File fileToWrite = fileUtil.createFileToWrite(directoryPath + File.separator + fileName);
fileUtil.writeContentInFile(fileContentStream, fileToWrite);
}
} catch (Exception e) {
throw new RuntimeException("Something went wrong while writing to file "+ e.getMessage());
}
});
return Response.ok("Sample String").build();
}

关于java - 接收多部分文件请求的 415 Unsupported Media Type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61037605/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com