【问题标题】:how to read file sent from http post in jersy如何读取从球衣的http post发送的文件
【发布时间】:2016-08-12 05:15:48
【问题描述】:

我测试了这段代码的值

@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.Throwable org.glassfish.jersey.server.model.ModelValidationException:应用程序资源模型的验证在应用程序初始化期间失败。 [[致命] 在索引 0 处找不到公共 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=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),参数=[参数[type=class java.io.InputStream,source=file,defaultValue=null],参数[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 子类

this is how i am sending request and file this is how my request header and body part looks like 谢谢!

【问题讨论】:

  • 请出示一段代码。
  • 我正在使用 Chrome 扩展的 REST 控制台
  • 您的 REST API 使用什么框架?
  • 显示项目中的依赖项。
  • 是的,谢谢我得到了答案,这是我的错误

标签: web-services rest


【解决方案1】:

添加 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 工件依赖项)。

注册多部分功能

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

如果您有Application/ResourceConfig 子类,请执行以下操作:

@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);
    }
}

如果您没有Application/ResourceConfig 子类,您可以在您的web.xml 部署描述符中注册MultiPartFeature。可以在jersey.config.server.provider.classnames 初始化参数的逗号分隔值中提供特定的资源、提供者和功能完全限定的类名。

<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请求实体主体的命名主体部分绑定到资源方法参数,如下:

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

更多详情,请查看Jersey documentation about multipart requests

【讨论】:

  • 我已经编辑了我的问题,请给我解决方案,谢谢!
  • 对不起,因为我是第一次使用这个网站,我不知道它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-24
相关资源
最近更新 更多