Search code examples
javaresttomcatjakarta-eeuploader

Java uploader (jersey) make all service down


I want to have very very simple uploader in a rest service which not working with html form, and get files from an android device.

this is my code:

@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();
    }

}

but when I push this code all of my rest services take down and give error 500.(I mean other rest uri path). for example if I want to access myserviceuri/resturi/login it gives error 500 too.

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

note: restService.restStarter is my first file that have:

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

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

}

I relized that this this two line:

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

cause the error and when I remove this from my test file other files work well.

pleaseeeee help me.


Solution

  • I finally solved it. for who have this problem: the problem is about to using @Path()

    in this case you have to write two @Path, first one upper of your class and the second one upper of your method and if you want to do all of this in a one simple path you should do this:

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