英文:
Jersey 2.x Multipart/form-data server returns 400 Bad Request and MessageBodyWriterNotFoundException
问题
以下是您提供的内容的翻译:
当我在Jersey 2.26中运行我的Multipart/form-data上传功能时,它返回400-Bad请求。我尝试了许多方法,但没有用。
当不上传文件时,它显示响应消息。当我使用客户端时,出现了 "MessageBodyWriterNotFoundException"。
Upload.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("数据上传成功").build();
}
}
web.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>
FYI- 未显示任何异常
我的日志过滤器日志
Aug 25, 2020 12:53:29 PM org.glassfish.jersey.filter.LoggingFilter log
INFO: 1 * 服务器在线程 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 * 服务器在线程 http-nio-9090-exec-8 上作出了响应
1 < 400
请帮助我解决这个问题
英文:
When I run my Multipart/form-data uploading feature in jersey 2.26, it returns 400-Bad request. I triedno many ways but no use
when uploading without file, it shows the response message. when I use client "MessageBodyWriterNotFoundException" showed.
Upload.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();
}
}
web.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>
FYI- No exception showed
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
please help me to get through this
答案1
得分: 0
@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();
}
}
This will work.
Happy Coding!!!
<details>
<summary>英文:</summary>
FileInputStream Used as Parameter. SO it returned 400 Bad request. Instead of that use **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();
}
}
This will work.
Happy Coding!!!
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论