本文介绍了如何读取从jersy中的http post发送的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这段代码我测试了值

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_PLAIN)
public String getE(@FormDataParam("file") InputStream inputStream,@FormDataParam("file") FormDataContentDisposition fileMetaData) {
    String out = "";
    Map<String, String> l = fileMetaData.getParameters();
    Collection<String> s = l.values();
    Iterator i = s.iterator();
    while(i.hasNext())
    {
        out += i.next()+"      ";
    }

返回;}

我收到此异常

严重:StandardWrapper.Throwableorg.glassfish.jersey.server.model.ModelValidationException:应用程序初始化期间应用程序资源模型的验证失败.[[致命] 在索引 0 处找不到类型为 public java.lang.String my.Service.getE(java.io.InputStream,org.glassfish.jersey.media.multipart.FormDataContentDisposition) 的参数的注入源.source='ResourceMethod{httpMethod=POST,consumedTypes=[multipart/form-data],producedTypes=[text/plain],suspended=false,suspendTimeout=0,suspendTimeoutUnit=MILLISECONDS,invocable=Invocable{handler=ClassBasedMethodHandler{handlerClass=classmy.Service, handlerConstructors=[org.glassfish.jersey.server.model.HandlerConstructor@4023c98]}, definitionMethod=public java.lang.String my.Service.getE(java.io.InputStream,org.glassfish.jersey.media.multipart.FormDataContentDisposition),参数=[参数[类型=类java.io.InputStream,源=文件,默认值=空],参数[类型=类org.glassfish.jersey.media.multipart.FormDataContentDisposition,源=文件,defaultValue=null]], responseType=class java.lang.String}, nameBindings=[]}']

SEVERE: StandardWrapper.Throwableorg.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 java.lang.String my.Service.getE(java.io.InputStream,org.glassfish.jersey.media.multipart.FormDataContentDisposition) at index 0.; source='ResourceMethod{httpMethod=POST, consumedTypes=[multipart/form-data], producedTypes=[text/plain], suspended=false, suspendTimeout=0, suspendTimeoutUnit=MILLISECONDS, invocable=Invocable{handler=ClassBasedMethodHandler{handlerClass=class my.Service, handlerConstructors=[org.glassfish.jersey.server.model.HandlerConstructor@4023c98]}, definitionMethod=public java.lang.String my.Service.getE(java.io.InputStream,org.glassfish.jersey.media.multipart.FormDataContentDisposition), parameters=[Parameter [type=class java.io.InputStream, source=file, defaultValue=null], Parameter [type=class org.glassfish.jersey.media.multipart.FormDataContentDisposition, source=file, defaultValue=null]], responseType=class java.lang.String}, nameBindings=[]}']

依赖是

<dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.39</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey</groupId>
        <artifactId>jersey-bom</artifactId>
        <version>2.16</version>
        <type>pom</type>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
        <version>2.16</version>
    </dependency>
<dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-moxy</artifactId>
        <version>2.17</version>
    </dependency>
    <dependency>
        <groupId>com.googlecode.json-simple</groupId>
        <artifactId>json-simple</artifactId>
        <version>1.1.1</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-multipart</artifactId>
        <version>2.16</version>
    </dependency>

我的服务类

@Path("empdata")

公共类服务{

EmpService service = new EmpService();
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_PLAIN)
public String getE(@FormDataParam("file") InputStream inputStream,@FormDataParam("file") FormDataContentDisposition fileMetaData) {
    String out = "";
    Map<String, String> l = fileMetaData.getParameters();
    Collection<String> s = l.values();
    Iterator i = s.iterator();
    while(i.hasNext())
    {
        out += i.next()+"      ";
    }
    return out;//
}

}

然后我注册了我的 ResourceConfig 子类

and i registered my ResourceConfig sub class

这是我发送请求和文件的方式这是我的请求头和正文部分的样子谢谢!

推荐答案

添加 Jersey 多部分依赖

要在您的 Jersey 应用程序中使用多部分功能,您需要添加 jersey-media-multipart 模块到您的 pom.xml 文件:

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-multipart</artifactId>
    <version>2.23.1</version>
</dependency>

如果您不使用 Maven,请确保拥有所有需要的依赖项(请参阅 jersey-media-multipart 工件依赖项)在类路径上.

If you're not using Maven make sure to have all needed dependencies (see the jersey-media-multipart artifact dependencies) on the classpath.

除了添加依赖之外,还需要注册MultiPartFeature.请参阅以下方法:

Besides adding the dependency, you need to register the MultiPartFeature. See the approaches below:

如果您有 应用/ResourceConfig 子类,操作如下:

If you have an Application/ResourceConfig sub-class, do as following:

@ApplicationPath("/api")
public class MyApplication extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> classes = new HashSet<Class<?>>();
        classes.add(MultipartFeature.class);
        return classes;
    }
}
@ApplicationPath("/api")
public class MyApplication extends ResourceConfig {

    public MyApplication() {
        register(MultipartFeature.class);
    }
}

如果您没有 应用/ResourceConfig 子类,可以注册MultiPartFeature 在您的 web.xml 部署描述符中.可以在 初始化参数.

If you don't have an Application/ResourceConfig sub-class, you can register the MultiPartFeature in your web.xml deployment descriptor. The specific resource, provider and feature fully-qualified class names can be provided in a comma-separated value of jersey.config.server.provider.classnames initialization parameter.

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

处理多部分请求

使用@FormDataParam 注解将multipart/form-data 请求实体主体的命名主体部分绑定到资源方法参数,如下所示:

Handling multipart requests

Use the @FormDataParam annotation to bind the named body part(s) of a multipart/form-data request entity body to a resource method parameter, as following:

@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response upload(@FormDataParam("file") InputStream inputStream,
                       @FormDataParam("file") FormDataContentDisposition fileMetaData) {
    ...
}

有关详细信息,请查看 关于多部分请求的 Jersey 文档.

这篇关于如何读取从jersy中的http post发送的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 12:25