これは私がこれまでに持っているものです:
これは私のRESTサービスを初期化します
package com.dothatapp.web.rest;
import javax.servlet.annotation.MultipartConfig;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
@ApplicationPath("/authed/rest")
public class JaxRsActivator extends Application {
}
そしてこれは私のサービスがどのように見えるか:
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriInfo;
import org.json.JSONObject;
@Path("fileupload")
public class FileUpload {
@Context
private UriInfo context;
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
public JSONObject doUpload(@Context HttpServletRequest obj) {
try {
obj.getParts();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(us);
System.out.println(es);
System.out.println(obj);
return null;
}
}
@MultipartConfig
をJaxRsActivator
に追加しようとしましたが、それでも例外が発生します:Request.getPartsはマルチパート構成なしで呼び出されます。@ MultipartConfigをサーブレットに追加するか、multipart-configを追加してくださいweb.xmlへの要素。
3-12-08T17:03:05.013+0000|SEVERE: Java.lang.IllegalStateException: Request.getParts is called without multipart configuration. Either add a @MultipartConfig to the servlet, or a multipart-config element to web.xml
at org.Apache.catalina.connector.Request.checkMultipartConfiguration(Request.Java:4522)
at org.Apache.catalina.connector.Request.getParts(Request.Java:4528)
at org.Apache.catalina.connector.RequestFacade.getParts(RequestFacade.Java:1086)
at com.dothatapp.web.rest.FileUpload.doUpload(FileUpload.Java:36)
at Sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at Sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.Java:57)
at Sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.Java:43)
at Java.lang.reflect.Method.invoke(Method.Java:606)
at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.Java:81)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.Java:125)
at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$TypeOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.Java:195)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.Java:91)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.Java:346)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.Java:341)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.Java:101)
at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.Java:224)
at org.glassfish.jersey.internal.Errors$1.call(Errors.Java:271)
at org.glassfish.jersey.internal.Errors$1.call(Errors.Java:267)
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.process(Errors.Java:267)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.Java:317)
at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.Java:198)
at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.Java:946)
at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.Java:323)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.Java:372)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.Java:335)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.Java:218)
at org.Apache.catalina.core.StandardWrapper.service(StandardWrapper.Java:1682)
at org.Apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.Java:344)
at org.Apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.Java:214)
at com.dothatapp.web.filter.DoThatAppFilter.doFilter(DoThatAppFilter.Java:27)
at org.Apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.Java:256)
at org.Apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.Java:214)
at com.dothatapp.web.filter.AuthFilter.doFilter(AuthFilter.Java:113)
at org.Apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.Java:256)
at org.Apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.Java:214)
at org.Apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.Java:316)
at org.Apache.catalina.core.StandardContextValve.invoke(StandardContextValve.Java:160)
at org.Apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.Java:734)
at org.Apache.catalina.core.StandardPipeline.invoke(StandardPipeline.Java:673)
at com.Sun.enterprise.web.WebPipeline.invoke(WebPipeline.Java:99)
at org.Apache.catalina.core.StandardHostValve.invoke(StandardHostValve.Java:174)
at org.Apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.Java:357)
at org.Apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.Java:260)
at com.Sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.Java:188)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.Java:191)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.Java:168)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.Java:189)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.Java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.Java:288)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.Java:206)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.Java:136)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.Java:114)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.Java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.Java:838)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.Java:113)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.Java:115)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.Java:55)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.Java:135)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.Java:564)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.Java:544)
at Java.lang.Thread.run(Thread.Java:724)
ありがとう
ついに私はジャージーカップリングなしでこれをうまく動かすことができました。問題はそれです @Multipart
アノテーションはApplication
では機能しないため、web.xml内で定義し、Application
クラス内で提供されるサービスを定義する必要があります。サービス内では、通常どおりアノテーションを使用できます。また、リクエストからParts
を抽出していますが、これは非常に簡単です。
PS。これは実際に bluimp JQueryファイルのアップロード のバックエンドを実装します
web.xml
<servlet>
<servlet-name>com.web.rest.JaxRsActivator</servlet-name>
<multipart-config>
<location>c:\dotmp</location>
<max-file-size>35000000</max-file-size>
<max-request-size>218018841</max-request-size>
<file-size-threshold>0</file-size-threshold>
</multipart-config>
</servlet>
<servlet-mapping>
<servlet-name>com.dothatapp.web.rest.JaxRsActivator</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
JaxRsActivator.Java
import Java.util.HashSet;
import Java.util.Set;
import javax.ws.rs.core.Application;
public class JaxRsActivator extends Application {
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> s = new HashSet<Class<?>>();
s.add(FileUpload.class);
return s;
}
}
FileUpload.Java
import Java.io.IOException;
import javax.json.Json;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
@Path("/")
public class FileUpload {
@POST
@Produces(MediaType.APPLICATION_JSON)
@Path("fileupload")
public Response doUpload(@Context HttpServletRequest request) {
JsonArrayBuilder array = Json.createArrayBuilder();
try {
for (Part part : request.getParts()) {
String name = null;
long size = 0;
try {
if (part.getContentType() == null
|| !part.getContentType().toLowerCase()
.startsWith("image/"))
continue;
name = part.getSubmittedFileName();
size = part.getSize();
array.add(addFile(name, size, "anId"));
part.delete();
} catch (Exception e) {
array.add(addError(name, size, "ERROR"));
}
}
} catch (IOException | ServletException e) {
e.printStackTrace();
}
JsonObject ret = Json.createObjectBuilder().add("files", array).build();
return Response.status(201).entity(ret).build();
}
private JsonObjectBuilder addFile(String name, long size, String url) {
return Json.createObjectBuilder().add("name", name).add("size", size)
.add("lid", url);
}
private JsonObjectBuilder addError(String name, long size, String error) {
return Json.createObjectBuilder().add("name", name).add("size", size)
.add("error", error);
}
}