@Path("file.upload")
public class UploadFileService {
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(
@FormDataParam("file") InputStream uploadedInputStream,
@FormDataParam("file") FormDataContentDisposition fileDetail) {
System.out.println("-----------------UploadFileService------------------1.");
// Should we use a disk or DB? Decided to use DISK
// Path should be read from properties-files
String uploadedFileLocation = "//uploaded/" + fileDetail.getFileName();
// save it
writeToFile(uploadedInputStream, uploadedFileLocation);
String output = "File uploaded to : " + uploadedFileLocation;
// All went OK
return Response.status(200).entity(output).build();
}
WARNING: No injection source found for a parameter of type public javax.ws.rs.core.Response com.insame.service.UploadFileService.uploadFile(java.io.InputStream,com.sun.jersey.core.header.FormDataContentDisposition) at index 0.
SEVERE: WebModule[/insame]StandardWrapper.Throwable
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 javax.ws.rs.core.Response com.insame.service.UploadFileService.uploadFile(java.io.InputStream,com.sun.jersey.core.header.FormDataContentDisposition) at index 0.; source='ResourceMethod{httpMethod=POST, consumedTypes=[multipart/form-data], producedTypes=[], suspended=false, suspendTimeout=0, suspendTimeoutUnit=MILLISECONDS, invocable=Invocable{handler=MethodHandler{handlerClass=class com.insame.service.UploadFileService, handlerConstructors=[org.glassfish.jersey.server.model.HandlerConstructor@47bee27a]}, handlingMethod=public javax.ws.rs.core.Response com.insame.service.UploadFileService.uploadFile(java.io.InputStream,com.sun.jersey.core.header.FormDataContentDisposition), parameters=[Parameter [type=class java.io.InputStream, source=file, defaultValue=null], Parameter [type=class com.sun.jersey.core.header.FormDataContentDisposition, source=file, defaultValue=null]], responseType=class javax.ws.rs.core.Response}, nameBindings=[]}']
at org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:410)
at org.glassfish.jersey.server.ApplicationHandler.access$500(ApplicationHandler.java:157)
at org.glassfish.jersey.server.ApplicationHandler$3.run(ApplicationHandler.java:280)
at org.glassfish.jersey.internal.Errors$2.call(Errors.java:289)
at org.glassfish.jersey.internal.Errors$2.call(Errors.java:286)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.processWithException(Errors.java:286)
at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:277)
at org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:262)
at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:167)
我在UploadFileService
:
@GET
@Path("count")
@Produces("text/plain")
public String countREST() {
return "1 one 1";
}
我记录了这个例外:
FINE: [Web-Security] hasResource perm: ("javax.security.jacc.WebResourcePermission"
"/webresources/file.upload/count" "GET")
SEVERE: WebModule[/insame]StandardWrapper.Throwable
java.lang.IllegalStateException: The resource configuration is not modifiable in this context.
at org.glassfish.jersey.server.ResourceConfig$ImmutableState.register(ResourceConfig.java:257)
WARNING: StandardWrapperValve[com.insame.service.ApplicationConfig]: Allocate exception for servlet com.insame.service.ApplicationConfig
java.lang.IllegalStateException: The resource configuration is not modifiable in this context.
at org.glassfish.jersey.server.ResourceConfig$ImmutableState.register(ResourceConfig.java:257)
at org.glassfish.jersey.server.ResourceConfig$ImmutableState.register(ResourceConfig.java:205)
at org.glassfish.jersey.server.ResourceConfig.register(ResourceConfig.java:435)
at org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:261)
at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:167)
at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:349)
环境
答案 0 :(得分:45)
要在Jersey应用程序中使用multipart,您需要在应用程序中注册MultiPartFeature,即:
public class ApplicationConfig extends Application {
public Set<Class<?>> getClasses() {
final Set<Class<?>> resources = new HashSet<Class<?>>();
// Add your resources.
resources.add(UploadFileService.class);
// Add additional features such as support for Multipart.
resources.add(MultiPartFeature.class);
return resources;
}
}
有关详细信息,请参阅“泽西用户指南”中的Multipart部分。
对于您正面临的第二个问题,尝试重新启动GlassFish服务器,我不确定NetBeans在更改后如何重新加载Jersey应用程序(如果这没有帮助,请发布您的ApplicationConfig
)。
答案 1 :(得分:32)
我遇到了同样的问题,并希望避免创建自定义应用程序类。它没有很好的文档记录,但是如果你想添加Multipart功能,你所要做的就是将它添加到你的web.xml jersey servlet config:
<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>
我还添加了一个loggingfilter。
答案 2 :(得分:2)
如果您正在使用jetty服务器和jersey servlet,那么您可以通过在启动了jetty服务器的主类中添加以下代码来解决此问题,
ServletHolder jerseyServlet = context.addServlet( org.glassfish.jersey.servlet.ServletContainer.class,“/ *”); jerseyServlet.setInitOrder(0);
// Tells the Jersey Servlet which REST service/classes to load. jerseyServlet .setInitParameter( "jersey.config.server.provider.classnames", <Your entry point class's canonical name> + ";org.glassfish.jersey.media.multipart.MultiPartFeature");
答案 3 :(得分:0)
稍作澄清
使用
import org.glassfish.jersey.media.multipart.MultiPartFeature
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataParam;
不
com.sun.jersey.*
仅在使用org.glassfish.jersey.media.multipart.*
在ApplicationConfig中,只需将MultiPartFeature注册为
import org.glassfish.jersey.media.multipart.MultiPartFeature;
@javax.ws.rs.ApplicationPath("webresources")
public class ApplicationConfig extends Application {
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> resources = new java.util.HashSet<>();
resources.add(UploadFileService.class);
resources.add(MultiPartFeature.class);
return resources;
}
}
答案 4 :(得分:0)
我使用Jersey 1.9.1。 org.glassfish ......适用于Jersey 2.对于Jersey 1,你最好使用com.sun ... classes。
答案 5 :(得分:-1)
如果您希望使用注释,可以使用与FormDataMultiPart等效的@FormDataParam(&#34; file&#34;)。
使用以下给出的示例代码提取:
public Response uploadFile( **@FormDataParam("file")** InputStream fileInputStream,
@FormDataParam("file") FormDataContentDisposition contentDispositionHeader) {