org.glassfish.jersey使用FormDataContentDisposition上传文件 [英] org.glassfish.jersey upload file with FormDataContentDisposition

查看:338
本文介绍了org.glassfish.jersey使用FormDataContentDisposition上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

http://www.mkyong.com / webservices / jax-rs / file-upload-example-in-jersey /
我遵循本指南并遇到问题。我有一些问题。


  1. 所有依赖关系都必须对应吗?我的项目有一些org.glassfish.jersey的依赖关系,本指南建议使用org.sun.jersey。我必须改变它与这样的相同版本吗?

     <依赖关系> 
    < groupId> org.glassfish.jersey.media< / groupId>
    < artifactId> jersey-media-multipart< / artifactId>
    < version> 2.16< / version>
    < /依赖关系>
    < dependency>
    < groupId> org.glassfish.jersey.core< / groupId>
    < artifactId>球衣服务器< / artifactId>
    < version> 2.16< / version>


  2. 错误

      org.glassfish.jersey.server.model.ModelValidationException:应用程序资源模型的验证在应用程序初始化期间失败。 [[致命]]找不到公开类型参数的注入源***。***。****。common.dto.response.AbstractResponse ***。***。****。m2m.instancetypeupload .webservice.InstanceTypeUploadWebService.upload(java.io.InputStream,org.glassfish.jersey.media.multipart.FormDataContentDisposition)位于索引0处。 source ='ResourceMethod {httpMethod = POST,consumeTypes = [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。参数[type = class java.io.InputStream,source = file1,defaultValue = null],参数[type = class org.glassfish.jersey.media.multipart.FormDataContentDisposition,source = file1,defaultValue = null]],responseType = class ***。***。***。common.dto.response.AbstractResponse},nameBindings = []}'] 

    这是我的网络服务

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

    这是我的电话:

    <$ p $ ajax({
    url:'http:// localhost:8080 / ****** / webapi / m2m / upload',
    data:fd,
    processData:false,
    contentType:'multipart / form-data',
    类型:'POST',
    成功:函数(数据){
    alert(JSON .stringify(数据));
    return;
    }
    });


如果Web服务只有1个参数(FormData InputStream)。如何解决它?


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






感谢peeskillet寻求答案。

  SEVERE:Web应用程序[/ linterm2m]创建了一个ThreadLocal类型的键[org.jvnet.hk2 .internal.PerLocatorUtilities $ 1](value [org.jvnet.hk2.internal.PerLocatorUtilities$1@df94b1])和一个类型为[java.util.WeakHashMap](值为[{}])的值,但是当web应用程序已停止。这很可能造成内存泄漏。 


解决方案

如果您的项目使用 org.glassfish ,你正在使用Jersey 2. com.sun 是Jersey 1,你不应该混淆两者。话虽如此,您遇到的错误很可能是由于您未注册 MultipartFeature 。当资源模型(资源方法)在启动时对正确性进行验证时,如果该功能未注册,则此功能的注释是未知的,就像没有注释一样。如果您使用的是 ResourceConfig ,那么您可以使用一个以上的方法参数,而不使用注释。

只需使用

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




$ b如果你使用的是web.xml,那么可以设置对于您注册的Jersey servlet,< init-param> $ b

  < INIT-PARAM> 
< param-name> jersey.config.server.provider.classnames< / param-name>
< param-value> org.glassfish.jersey.media.multipart.MultiPartFeature< / param-value>
< / init-param>




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


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


http://www.mkyong.com/webservices/jax-rs/file-upload-example-in-jersey/ I'm following this guide and run to a problem. I have some questions.

  1. Do all the dependency have to be corresponded? My project has some org.glassfish.jersey dependency and this guide suggest to use org.sun.jersey. Do I have to change it with also the same version like this?

    <dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-multipart</artifactId>
    <version>2.16</version>
    </dependency>
    <dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-server</artifactId>
    <version>2.16</version>
    

  2. I have this error

    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=[]}']
    

    This is my web service

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

    This is my call:

    $.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;
          }
        });
    

The web service is reachable if it only has 1 parameter (FormData InputStream). How to fix it?

  1. I also want to add another String parameter for the web service. What should I do?


Thank you peeskillet for the answers. A bit extra.

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.

解决方案

If you project is using org.glassfish, you are using Jersey 2. com.sun is Jersey 1, and you should never mix the two. That being said, the error you are facing is most likely due to the fact that you didn't register the MultipartFeature. When the resource model (the resource methods) are being validated for "correctness" at startup, if the feature is not registered, the annotations specific to this feature is unknown, and it's like just having no annotation. And you cant have more than one method param with no annotation.

If you are using a ResourceConfig, you can simply use

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

If you are using web.xml, then can set an <init-param> for the Jersey servlet you registered

<init-param>
    <param-name>jersey.config.server.provider.classnames</param-name>
    <param-value>org.glassfish.jersey.media.multipart.MultiPartFeature</param-value>
</init-param>

"I also want to add another String parameter for the web service. What should I do?"

You will need to make that as part of the multipart request and the client needs to make sure to send it as part of the multipart also. On the server side just add another @FormDataParam("anotherString") String anotherString as a method parameter. As for the client, I don't know jQuery that will to help with that. Haven't tested, but you can try this, which shows data being appended to FormParam. Here's something with Angular, where I built the request body myself. Might be a little much, as you may not need to explicitly set the content types.

这篇关于org.glassfish.jersey使用FormDataContentDisposition上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆