Java uploader ( Jersey )使所有服务中断

标签 java rest tomcat jakarta-ee uploader

我想在不使用 html 表单的休息服务中拥有非常非常简单的 uploader ,并从 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,第一个是你类的上层,第二个是你方法的上层,如果你想在一个简单的路径中完成所有这些,你应该这样做:

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

关于Java uploader ( Jersey )使所有服务中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38843153/

相关文章:

java - 下个月的第一天使用 java Joda-Time

java - Vaadin 覆盖表内按钮的列排序

Django REST Facebook 身份验证

python - 如何在 python 中使用 BitBucket API 发布拉取请求评论?

用于 haskell 中 ECG/EEG 传感器数据的 RESTful 接口(interface)

java - 如何在包含 3 个 war 文件的 Eclipse 中调试应用程序?

java - fragment 内的 WebView 返回按钮

java - Ant无法开启tomcat

java - eclipse & Tomcat : How to deploy a project to differently named directory

Java jooq 插入查询不起作用