无法使用 Jax-RS 上传文件
Cannot Upload a file using Jax-RS
我想使用球衣框架。我是 运行 Web 服务,使用 ant 应用程序,在 Java EE7 上。我的应用服务器是 Glassfish
我的方法是这样的:
package mypackage.service;
...
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataParam;
@POST
@Path("createSomething")
@Consumes(MULTIPART_FORM_DATA)
@Produces(APPLICATION_XML)
public Response createSomething(@FormDataParam("upload") InputStream is, @FormDataParam("upload") FormDataContentDisposition formData, @QueryParam("some") String some, @Context HttpServletRequest request) {
String fileLocation = "C:\UploadFile\" + formData.getFileName();
//more things, do not matter
try {
ctrl.saveFile(is, fileLocation);
String result = "Successfully File Uploaded on the path " + fileLocation;
return Response.status(Response.Status.OK).entity(result).build();
} catch (IOException e) {
e.printStackTrace();
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
我还有一个应用程序配置:
package mypackage.service;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.core.Application;
import org.glassfish.jersey.media.multipart.MultiPartFeature;
@javax.ws.rs.ApplicationPath("")
public class ApplicationConfig extends Application {
@Override
public Set<Class<?>> getClasses() {
final Set<Class<?>> resources = new HashSet<>();
addRestResourceClasses(resources);
resources.add(MultiPartFeature.class);
return resources;
}
/**
* Do not modify addRestResourceClasses() method. It is automatically
* populated with all resources defined in the project. If required, comment
* out calling this method in getClasses().
*/
private void addRestResourceClasses(Set<Class<?>> resources) {
resources.add(mypackage.service.MYSERVICE.class);
}
}
在 myweb.xml 我有:
<servlet>
<servlet-name>ServletAdaptor</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>mypackage.service.ApplicationConfig</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>mypackage.service</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.provider.classnames</param-name>
<param-value>org.glassfish.jersey.filter.LoggingFilter;org.glassfish.jersey.media.multipart.MultiPartFeature</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ServletAdaptor</servlet-name>
<url-pattern>/createSomething/*</url-pattern>
</servlet-mapping>
我仍然收到相同的消息:
原因:org.apache.catalina.LifecycleException:org.glassfish.jersey.server.model.ModelValidationException:应用程序初始化期间应用程序资源模型验证失败。
[[致命] 未找到类型为 public javax.ws.rs.core.Response
的参数的注入源
我哪里做错了??
对我来说效果很好。尽管我会完全摆脱 Application 子类。它不是必需的,可能会导致 conflict/confusion。你的xml已经足够配置了,去掉javax.ws.rs.Application
<init-param>
就行了。我还会考虑将多部分 jar 制作成仅编译时 jar(意味着不内置到 war - 它们可能与 Glassfish 的版本冲突)。我对 Ant 的工作不多,所以我不确定你如何做到这一点,但我知道这是可能的。
以下代码对我有用:
Class ->>> 添加
Class 属性 --->> 添加
Public Class userREST () {
@POST
@Path("upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
public Response uploadImageFile(@FormDataParam("uploadFile") InputStream fileInputStream,
@FormDataParam("uploadFile") FormDataContentDisposition fileFormDataContentDisposition,
@FormDataParam("FIR_REG_NUM") String FIR_REG_NUM, @FormDataParam("LOGIN_ID") String LOGIN_ID) {
final_json_result = WriteFileInFolder.fileAnalysis(fileInputStream, fileFormDataContentDisposition, FIR_REG_NUM,
LOGIN_ID);
return Response.ok(final_json_result).build();
}// uploadImageFile
package ####.jaxrs.jwt;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import ####.helper.Common@@@;
import ####.jaxrs.jwt.filters.JWTRequestFilter;
import ####.jaxrs.jwt.filters.JWTResponseFilter;
import ####.service.FileServicesREST;
@ApplicationPath("fileservice")
public class FileJAXRSConfig extends Application {
@Override
public Set<Class<?>> getClasses() {
Common@@@.logging("@ApplicationPath@FileServicesREST...");
Set<Class<?>> clazzes = new HashSet<Class<?>>();
clazzes.add(JWTRequestFilter.class);
clazzes.add(FileServicesREST.class);
clazzes.add(JWTResponseFilter.class);
return clazzes;
}
@Override
public Map<String, Object> getProperties() {
Map<String, Object> properties = new HashMap<String, Object>();
properties.put("jersey.config.server.provider.packages", "####.service");
properties.put("jersey.config.server.provider.classnames", "org.glassfish.jersey.media.multipart.MultiPartFeature");
return properties;
}
}
不需要在web.xml中添加以下内容
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>mha.@@@.service</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.provider.classnames</param-name>
<param-value>org.glassfish.jersey.media.multipart.MultiPartFeature</param-value>
</init-param>
我想使用球衣框架。我是 运行 Web 服务,使用 ant 应用程序,在 Java EE7 上。我的应用服务器是 Glassfish
我的方法是这样的:
package mypackage.service;
...
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataParam;
@POST
@Path("createSomething")
@Consumes(MULTIPART_FORM_DATA)
@Produces(APPLICATION_XML)
public Response createSomething(@FormDataParam("upload") InputStream is, @FormDataParam("upload") FormDataContentDisposition formData, @QueryParam("some") String some, @Context HttpServletRequest request) {
String fileLocation = "C:\UploadFile\" + formData.getFileName();
//more things, do not matter
try {
ctrl.saveFile(is, fileLocation);
String result = "Successfully File Uploaded on the path " + fileLocation;
return Response.status(Response.Status.OK).entity(result).build();
} catch (IOException e) {
e.printStackTrace();
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
我还有一个应用程序配置:
package mypackage.service;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.core.Application;
import org.glassfish.jersey.media.multipart.MultiPartFeature;
@javax.ws.rs.ApplicationPath("")
public class ApplicationConfig extends Application {
@Override
public Set<Class<?>> getClasses() {
final Set<Class<?>> resources = new HashSet<>();
addRestResourceClasses(resources);
resources.add(MultiPartFeature.class);
return resources;
}
/**
* Do not modify addRestResourceClasses() method. It is automatically
* populated with all resources defined in the project. If required, comment
* out calling this method in getClasses().
*/
private void addRestResourceClasses(Set<Class<?>> resources) {
resources.add(mypackage.service.MYSERVICE.class);
}
}
在 myweb.xml 我有:
<servlet>
<servlet-name>ServletAdaptor</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>mypackage.service.ApplicationConfig</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>mypackage.service</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.provider.classnames</param-name>
<param-value>org.glassfish.jersey.filter.LoggingFilter;org.glassfish.jersey.media.multipart.MultiPartFeature</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ServletAdaptor</servlet-name>
<url-pattern>/createSomething/*</url-pattern>
</servlet-mapping>
我仍然收到相同的消息:
原因:org.apache.catalina.LifecycleException:org.glassfish.jersey.server.model.ModelValidationException:应用程序初始化期间应用程序资源模型验证失败。 [[致命] 未找到类型为 public javax.ws.rs.core.Response
的参数的注入源我哪里做错了??
对我来说效果很好。尽管我会完全摆脱 Application 子类。它不是必需的,可能会导致 conflict/confusion。你的xml已经足够配置了,去掉javax.ws.rs.Application
<init-param>
就行了。我还会考虑将多部分 jar 制作成仅编译时 jar(意味着不内置到 war - 它们可能与 Glassfish 的版本冲突)。我对 Ant 的工作不多,所以我不确定你如何做到这一点,但我知道这是可能的。
以下代码对我有用:
Class ->>> 添加
Class 属性 --->> 添加
Public Class userREST () {
@POST
@Path("upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
public Response uploadImageFile(@FormDataParam("uploadFile") InputStream fileInputStream,
@FormDataParam("uploadFile") FormDataContentDisposition fileFormDataContentDisposition,
@FormDataParam("FIR_REG_NUM") String FIR_REG_NUM, @FormDataParam("LOGIN_ID") String LOGIN_ID) {
final_json_result = WriteFileInFolder.fileAnalysis(fileInputStream, fileFormDataContentDisposition, FIR_REG_NUM,
LOGIN_ID);
return Response.ok(final_json_result).build();
}// uploadImageFile
package ####.jaxrs.jwt;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import ####.helper.Common@@@;
import ####.jaxrs.jwt.filters.JWTRequestFilter;
import ####.jaxrs.jwt.filters.JWTResponseFilter;
import ####.service.FileServicesREST;
@ApplicationPath("fileservice")
public class FileJAXRSConfig extends Application {
@Override
public Set<Class<?>> getClasses() {
Common@@@.logging("@ApplicationPath@FileServicesREST...");
Set<Class<?>> clazzes = new HashSet<Class<?>>();
clazzes.add(JWTRequestFilter.class);
clazzes.add(FileServicesREST.class);
clazzes.add(JWTResponseFilter.class);
return clazzes;
}
@Override
public Map<String, Object> getProperties() {
Map<String, Object> properties = new HashMap<String, Object>();
properties.put("jersey.config.server.provider.packages", "####.service");
properties.put("jersey.config.server.provider.classnames", "org.glassfish.jersey.media.multipart.MultiPartFeature");
return properties;
}
}
不需要在web.xml中添加以下内容
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>mha.@@@.service</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.provider.classnames</param-name>
<param-value>org.glassfish.jersey.media.multipart.MultiPartFeature</param-value>
</init-param>