[英]BAD Request 400 consume multipart/form-data jersey clientresponse java
[英]Jersey 2.x Multipart/form-data server returns 400 Bad Request and MessageBodyWriterNotFoundException
当我在 jersey 2.26 中运行我的 Multipart/form-data 上传功能时,它返回 400-Bad 请求。 我尝试了很多方法但没有用
无文件上传时,显示响应消息。 当我使用客户端“MessageBodyWriterNotFoundException”时显示。
上传.java
@Path("/upload")
public class UploadFileDemo {
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_PLAIN)
public Response uploadFile(@FormDataParam("file") FileInputStream fileInputStream,
@FormDataParam("file") FormDataContentDisposition fileInfo) {
System.out.println(fileInfo.getFileName()+"-"+fileInfo.getSize());
String path= "D://"+fileInfo.getFileName();
int read=0;
byte[] bytes = new byte[1024];
try {
OutputStream out = new FileOutputStream(new File(path));
while((read = fileInputStream.read(bytes))!=-1) {
out.write(bytes, 0, read);
}
out.flush();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return Response.ok("Data uploaded successfully").build();
}
}
网页.xml
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.packages.resource</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.provider.classnames</param-name>
<param-value>org.glassfish.jersey.filter.LoggingFilter;
org.glassfish.jersey.media.multipart.MultiPartFeature</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/webapi/*</url-pattern>
</servlet-mapping>
仅供参考 - 没有异常显示
My logging filter log
Aug 25, 2020 12:53:29 PM org.glassfish.jersey.filter.LoggingFilter log
INFO: 1 * Server has received a request on thread http-nio-9090-exec-8
1 > POST http://localhost:9090/rest-new-version/webapi/upload/file
1 > accept: */*
1 > accept-encoding: gzip, deflate, br
1 > connection: keep-alive
1 > content-length: 240
1 > content-type: multipart/form-data; boundary=--------------------------247816562830114154909314
1 > host: localhost:9090
1 > postman-token: 91bec8ef-5857-4604-a512-6dc97d5e22c6
1 > user-agent: PostmanRuntime/7.26.3
Aug 25, 2020 12:53:30 PM org.glassfish.jersey.filter.LoggingFilter log
INFO: 1 * Server responded with a response on thread http-nio-9090-exec-8
1 < 400
请帮我解决这个问题:)
FileInputStream 用作参数。 所以它返回了 400 Bad request。 而不是使用InputStream
@Path("/upload")
public class UploadFileDemo {
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_PLAIN)
public Response uploadFile(@FormDataParam("file") InputStream fileInputStream,
@FormDataParam("file") FormDataContentDisposition fileInfo) {
System.out.println(fileInfo.getFileName()+"-"+fileInfo.getSize());
String path= "D://"+fileInfo.getFileName();
int read=0;
byte[] bytes = new byte[1024];
try {
OutputStream out = new FileOutputStream(new File(path));
while((read = fileInputStream.read(bytes))!=-1) {
out.write(bytes, 0, read);
}
out.flush();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return Response.ok("Data uploaded successfully").build();
}
}
这将起作用。 编码快乐!!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.