此代码我测试了值
@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:应用程序初始化期间应用程序资源模型的验证失败。 [[FATAL]在索引0处找不到类型为public java.lang.String my.Service.getE(java.io.InputStream,org.glassfish.jersey.media.multipart.FormDataContentDisposition)的参数的注入源。 source ='ResourceMethod {httpMethod = POST,consumeTypes = [multipart / form-data],generatedTypes = [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子类
this is how i am sending request and file this is how my request header and body part looks like 谢谢!
答案 0 :(得分:1)
要在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) {
...
}