带有FormDataContentDisposition的org.glassfish.jersey上传文件

http://www.mkyong.com/webservices/jax-rs/file-upload-example-in-jersey/我正在按照本指南操作并遇到问题。 我有一些疑问。

  1. 所有的依赖都必须对应吗? 我的项目有一些org.glassfish.jersey依赖项,本指南建议使用org.sun.jersey。 我是否必须使用与此相同的版本进行更改?

     org.glassfish.jersey.media jersey-media-multipart 2.16   org.glassfish.jersey.core jersey-server 2.16 
  2. 我有这个错误

     org.glassfish.jersey.server.model.ModelValidationException: Validation of the application resource model has failed during application initialization. [[FATAL] No injection source found for a parameter of type public ***.***.****.common.dto.response.AbstractResponse ***.***.****.m2m.instancetypeupload.webservice.InstanceTypeUploadWebService.upload(java.io.InputStream,org.glassfish.jersey.media.multipart.FormDataContentDisposition) at index 0.; source='ResourceMethod{httpMethod=POST, consumedTypes=[multipart/form-data], producedTypes=[application/json], suspended=false, suspendTimeout=0, suspendTimeoutUnit=MILLISECONDS, invocable=Invocable{handler=ClassBasedMethodHandler{handlerClass=class ***.***.****.m2m.instancetypeupload.webservice.InstanceTypeUploadWebService, handlerConstructors=[org.glassfish.jersey.server.model.HandlerConstructor@90516e]}, definitionMethod=public ***.***.***.common.dto.response.AbstractResponse ***.***.*****.m2m.instancetypeupload.webservice.InstanceTypeUploadWebService.upload(java.io.InputStream,org.glassfish.jersey.media.multipart.FormDataContentDisposition), parameters=[Parameter [type=class java.io.InputStream, source=file1, defaultValue=null], Parameter [type=class org.glassfish.jersey.media.multipart.FormDataContentDisposition, source=file1, defaultValue=null]], responseType=class ***.***.***.common.dto.response.AbstractResponse}, nameBindings=[]}'] 

    这是我的网络服务

     @POST @Path("/upload") @Consumes(MediaType.MULTIPART_FORM_DATA) @Produces(MediaType.APPLICATION_JSON) public AbstractResponse upload(@FormDataParam("file1") InputStream file1, @FormDataParam("file1") FormDataContentDisposition filename1 ) { 

    这是我的电话:

     $.ajax({ url: 'http://localhost:8080/******/webapi/m2m/upload', data: fd, processData: false, contentType: 'multipart/form-data', type: 'POST', success: function(data){ alert(JSON.stringify(data)); return; } }); 

如果Web服务只有1个参数(FormData InputStream),则可以访问该服务。 怎么解决?

  1. 我还想为Web服务添加另一个String参数。 我该怎么办?

谢谢你peeskillet的答案。 多一点。

 SEVERE: The web application [/linterm2m] created a ThreadLocal with key of type [org.jvnet.hk2.internal.PerLocatorUtilities$1] (value [org.jvnet.hk2.internal.PerLocatorUtilities$1@df94b1]) and a value of type [java.util.WeakHashMap] (value [{}]) but failed to remove it when the web application was stopped. This is very likely to create a memory leak. 

如果你的项目是使用org.glassfish ,你使用的是泽西2, com.sun是泽西1,你永远不应该混合这两者。 话虽如此,您所面临的错误很可能是因为您没有注册MultipartFeature 。 当资源模型(资源方法)在启动时被validation为“正确性”时,如果未注册该特征,则该特征的特定注释是未知的,并且就像没有注释一样。 并且你不能有多个没有注释的方法参数。

如果您使用的是ResourceConfig ,则可以使用

 public class JerseyConfig extends ResourceConfig { public JerseyConfig() { register(MultiPartFeature.class); } } 

如果您使用的是web.xml,则可以为您注册的Jersey servlet设置

  jersey.config.server.provider.classnames org.glassfish.jersey.media.multipart.MultiPartFeature  

“我还想为Web服务添加另一个String参数。我该怎么办?”

您需要将其作为multipart请求的一部分,并且客户端需要确保将其作为multipart的一部分发送。 在服务器端,只需添加另一个@FormDataParam("anotherString") String anotherString作为方法参数。 至于客户端,我不知道jQuery会对此有所帮助。 尚未测试,但您可以尝试这一点 ,它显示数据被附加到FormParam 。 这是Angular的一些东西 ,我自己构建了请求体。 可能有点多,因为您可能不需要显式设置内容类型。