server 代码:
@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(
@FormDataParam("file") InputStream uploadedInputStream,
@FormDataParam("file") FormDataContentDisposition fileDetail) {
System.out.println("uploadFile start");
String uploadedFileLocation = "E://uploaded/"
+ fileDetail.getFileName();
writeToFile(uploadedInputStream, uploadedFileLocation);
String output = "File uploaded to : " + uploadedFileLocation;
System.out.println("uploadFile end");
return Response.status(200).entity(output).build();
}
测试代码:
结果总是报:
2013-8-22 10:19:38 com.sun.jersey.spi.container.ContainerRequest getEntity
严重: A message body reader for Java class com.sun.jersey.core.header.FormDataContentDisposition, and Java type class com.sun.jersey.core.header.FormDataContentDisposition, and MIME media type multipart/form-data;boundary=----WebKitFormBoundaryuLityYGZVIgcPrJC was not foundcom.sun.jersey.core.impl.provider.entity.EntityHolderReader
type Status report
message Unsupported Media Type
description The server refused this request because the request entity is in a format not supported by the requested resource for the requested method.
要包含jersey-multipart.jar 和 mimepull.jar 并且要和 jersey-server.jar 版本一致。
调了很据还是会出现以上问题。最后在tomcat的webapps\HTTPtest\WEB-INF\lib里边没有发现Reference Library 里的jar包!
原来我把jar包放在了项目根目录的lib文件夹里了而没有放在WEB-INF文件夹里的lib文件夹里。
改后过段问题解决。
上传文件成功后,测试了上传文件大小的上限,当大于262M 左右会OutOfMemoryError。
至于jersey 上传文件的大小限制还不是很清楚,有待继续研究。