我有Tomcat Jersey服务,它应该接受文件和其他参数。以下代码说明了用例:
@POST
@Path("/fileupload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public void post(@FormDataParam("file")File file, @FormDataParam("fileType")String fileType) throws FileNotFoundException
{
//Your local disk path where you want to store the file
String uploadedFileLocation = "C://AlienTemp/" + file.getName();
System.out.println(uploadedFileLocation);
System.out.println(fileType);
// save it
File objFile=new File(uploadedFileLocation);
if(objFile.exists())
{
objFile.delete();
}
saveToFile(new FileInputStream(file), uploadedFileLocation);
}
private void saveToFile(InputStream uploadedInputStream,
String uploadedFileLocation) {
try {
OutputStream out = null;
int read = 0;
byte[] bytes = new byte[1024];
out = new FileOutputStream(new File(uploadedFileLocation));
while ((read = uploadedInputStream.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
上传本身有效但仅当我删除了fileType参数和所有注释时。如果存在任何注释,则它不起作用。 Tomcat抛出一个无用的400错误:“客户端发送的请求在语法上是不正确的。”如果我没有指定任何注释,则servlet不会启动,从而引发资源错误。
换句话说,只有当一个参数存在(文件)而没有任何注释时,上传请求才会成功。
其他细节:
为什么注释不起作用?
编辑1:
这是服务器输出的内容:
Apr 10, 2016 5:04:51 AM org.glassfish.jersey.filter.LoggingFilter log
INFO: 2 * Server has received a request on thread http-nio-8070-exec-2
2 > POST http://localhost:8070/web/feaf/fileupload
2 > accept: */*
2 > accept-encoding: gzip, deflate
2 > accept-language: en-US,en;q=0.8
2 > cache-control: no-cache
2 > connection: keep-alive
2 > content-length: 3903
2 > content-type: multipart/form-data
2 > host: localhost:8070
2 > origin: chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop
2 > postman-token: 4eb8418b-9976-ca61-6d0e-f98c7d96d3b8
2 > user-agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36
Apr 10, 2016 5:04:51 AM org.glassfish.jersey.filter.LoggingFilter log
INFO: 2 * Server responded with a response on thread http-nio-8070-exec-2
2 < 400
编辑2:
这是web.xml。我不得不更改服务名称并删除一些不相关的servlet。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>MyWebservice</display-name>
<welcome-file-list>
<welcome-file>status.jsp</welcome-file>
</welcome-file-list>
<!-- RESTful services -->
<servlet>
<servlet-name>feaf Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>web.feaf</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>feaf Service</servlet-name>
<url-pattern>/web/feaf/*</url-pattern>
</servlet-mapping>
</web-app>
编辑3:
感谢@Sanj,事实证明,Jersey拒绝了标题中包含multipart-form-data的请求,至少是Postman。发送空标题可以解决问题。
答案 0 :(得分:1)
您的代码看起来不错。依赖项(pom.xml)或web.xml可能存在问题。
以下是使用jersey 2.22.1 @ https://github.com/shaimakh/so36524919
的工作文件上传示例以下是我的示例中的依赖项和web.xml:
的pom.xml(依赖关系)
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.22.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>2.22.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.22.1</version>
</dependency>
的web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Restful File Upload</display-name>
<servlet>
<servlet-name>jersey-servlet</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.foo.rest</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.provider.classnames</param-name>
<param-value>org.glassfish.jersey.media.multipart.MultiPartFeature</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-servlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
邮递员输出