我正在使用Jersey 2.0开发表单提交服务。该表单包含多个文本字段和一个文件字段。我需要提取文件,文件名,文件媒体类型和文件内容类型并将它们保存在对象库中
@Path("upload")
@Consumes({MediaType.MULTIPART_FORM_DATA})
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public class UploadService {
@POST
public BlobDo uploadFile(FormDataMultiPart uploadedBody) {
String accountSid = uploadedBody.getField("account-sid").getValue();
String apiToken = uploadedBody.getField("api-token").getValue();
String checksum = uploadedBody.getField("checksum").getValue();
FormDataBodyPart bodyPart = uploadedBody.getField("file");
MySwiftObject obj = new MySwiftObject(bodyPart.getValueAs(InputStream.class));
obj.setName(bodyPart.getContentDisposition().getFileName());
obj.setContentType(bodyPart.getMediaType().toString());
obj.setContentDisposition(bodyPart.getContentDisposition().toString());
...
}
的pom.xml
<jersey.version>2.17</jersey.version>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-inmemory</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-bean-validation</artifactId>
</dependency>
表单提交请求
POST /nbs/v2/upload HTTP/1.1
Host: 127.0.0.1:8080
Cache-Control: no-cache
Postman-Token: a4c1d4e9-5f71-2321-3870-e9cac0524f8d
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryA2Z9pPMA7y3da8BG
------WebKitFormBoundaryA2Z9pPMA7y3da8BG
Content-Disposition: form-data; name="account-sid"
Q45Ppm5ukvdqjTQ6eW0O5ztTXipwnjKQx1p6cf+fbCQ=
------WebKitFormBoundaryA2Z9pPMA7y3da8BG
Content-Disposition: form-data; name="api-token"
6397cd691909fdc14cef67dbc1dc2dc3
------WebKitFormBoundaryA2Z9pPMA7y3da8BG
Content-Disposition: form-data; name="file"; filename="screen_4_100155.jpg"
Content-Type: image/jpeg
......Exif..MM.*.............................b...........j
------WebKitFormBoundaryA2Z9pPMA7y3da8BG
Content-Disposition: form-data; name="checksum"
6a3381b1d16bded4a3dfc325a8bb800e
------WebKitFormBoundaryA2Z9pPMA7y3da8BG
JVM堆大小
-Xmx=1024mb
上传~50MB文件时,在名为/tmp/tomcat7-tomcat7-tmp
和FileBackedOutputStream7949386530699987086.tmp
MIME8234229766850016150.tmp
目录中创建两个具有相似MD5总和的临时文件
在上传完成之前,服务器抛出异常
javax.servlet.ServletException: org.glassfish.jersey.server.ContainerException: java.lang.OutOfMemoryError: Java heap space org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:421) org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:386) org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:335) org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:222)
并删除名为MIME8234229766850016150.tmp
的文件,但保留另一个文件。未删除的FileBackedOutputStream....tmp
文件填满了硬盘上的整个空间。
将堆空间增加到7GB,但无法上传~200MB文件。
在服务器上运行作业以删除旧的临时文件。
创建名称为jersey-multipart-config.properties
且内容
jersey.config.multipart.bufferThreshold = -1
不再创建文件MIME[random numbers].tmp
,但除非重新启动tomcat,否则FileBackedOutputStream[random number].tmp
仍会在硬盘驱动器上挂起。
答案 0 :(得分:3)
问题#1似乎是通过在<servlet>
标记
<multipart-config>
<location>/tmp</location>
<max-file-size>1000000000</max-file-size>
<max-request-size>1500000000</max-request-size>
<file-size-threshold>0</file-size-threshold>
</multipart-config>
并删除了jersey-multipart-config.properties
文件。
现在我可以上传超过200Mb的文件。不再创建临时文件。
但我仍然无法解释问题#2。