Java 上传者(球衣)关闭所有服务

Java uploader (jersey) make all service down

我想在不使用 html 表单的休息服务中使用非常非常简单的上传器,并从 android 设备获取文件。

这是我的代码:

@Path("test")
@POST
@Consumes({MediaType.MULTIPART_FORM_DATA})
public String testing(@FormDataParam("img") InputStream fileInputStream,
        @FormDataParam("img") FormDataContentDisposition contentDispositionHeader ){
    String SERVER_UPLOAD_LOCATION_FOLDER = "/var/lib/openshift/57a83e6f0c1e66920000005a/"
                + "app-root/runtime/repo/downloads/users/";
    String filePath = SERVER_UPLOAD_LOCATION_FOLDER + contentDispositionHeader.getFileName();
saveFile(fileInputStream, filePath);
String output = "File saved to server location : " + filePath;
return output;
}
private void saveFile(InputStream uploadedInputStream,
        String serverLocation) {

    try {
    OutputStream outpuStream = new FileOutputStream(new File(serverLocation));
        int read = 0;
        byte[] bytes = new byte[1024];

        outpuStream = new FileOutputStream(new File(serverLocation));
        while ((read = uploadedInputStream.read(bytes)) != -1) {
            outpuStream.write(bytes, 0, read);
        }
        outpuStream.flush();
        outpuStream.close();
    } catch (IOException e) {

        e.printStackTrace();
    }

}

但是当我推送此代码时,我所有的休息服务都会关闭并给出错误 500。(我的意思是其他休息 uri 路径)。例如,如果我想访问 myserviceuri/resturi/login,它也会给出错误 500。

HTTP Status 500 - Servlet.init() for servlet restService.restStarter threw exception

type Exception report

message Servlet.init() for servlet restService.restStarter threw exception

description The server encountered an internal error that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Servlet.init() for servlet restService.restStarter threw exception
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    org.apache.catalina.valves.RemoteIpValve.invoke(RemoteIpValve.java:683)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1042)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
    org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Thread.java:745)
root cause

java.lang.IllegalStateException: The resource configuration is not modifiable in this context.
    org.glassfish.jersey.server.ResourceConfig$ImmutableState.register(ResourceConfig.java:274)
    org.glassfish.jersey.server.ResourceConfig$ImmutableState.register(ResourceConfig.java:221)
    org.glassfish.jersey.server.ResourceConfig.register(ResourceConfig.java:453)
    org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:387)
    org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:177)
    org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:369)
    javax.servlet.GenericServlet.init(GenericServlet.java:158)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    org.apache.catalina.valves.RemoteIpValve.invoke(RemoteIpValve.java:683)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1042)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
    org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Thread.java:745)

note The full stack trace of the root cause is available in the Apache Tomcat/7.0.54 logs.
Apache Tomcat/7.0.54

注意:restService.restStarter 是我的第一个文件:

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

/**
 *
 * @author Seyed Ali
 */
@ApplicationPath("RestApi")
public class restStarter extends Application {

}

我意识到这两行:

@FormDataParam("img") InputStream fileInputStream,
@FormDataParam("img") FormDataContentDisposition contentDispositionHeader

导致错误,当我从我的测试文件中删除它时,其他文件工作正常。

请帮帮我。

我终于解决了。 对于谁有这个问题: 问题是关于使用@Path()

在这种情况下,你必须写两个@Path,第一个在你的 class 上面,第二个在你的方法 上面,如果你想这样做所有这些都在一个简单的路径中,您应该这样做:

@Path("/")
public Class classname {
     @Path("yourPath")
     @Consumes("multipart/form-data")
     public method(){
          .
          .
          .
     }
}

这是解决方案:How to write Jersey Multipart webapp, Tomcat Server

  1. 添加

     <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-multipart</artifactId>
        <version>RELEASE</version>
     </dependency>
    
  2. 通过 ResourceConfig or web.xml or Application 配置 MultiPartFeature