2016-08-09 64 views
1

我想在非HTML格式的休息服務中使用非常簡單的上傳器,並從Android設備獲取文件。Java上傳(球衣)使所有服務都關閉

這是我的代碼:

@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 

原因的錯誤,當我從刪除此我的測試文件其他文件運行良好。

pleaseeeee help me。

+0

您是否閱讀過'註釋根本原因的完整堆棧跟蹤在Apache Tomcat/7.0.54日誌中可用?你可以發佈完整的堆棧跟蹤嗎? –

+0

[Why why「java.lang.IllegalStateException:資源配置在此上下文中不可修改。」似乎部署澤西島應用程序?](http://stackoverflow.com/questions/20670310/why-would-java-lang-illegalstateexception-the-resource-configuration-is-not-mo) – Gimby

回答

0

我終於解決了。 誰擁有這個問題: 問題是要在這種情況下你必須寫兩個@Path,使用@Path()

第一個上你的類和你的方法的第二個上和如果要做到這一切在一個簡單的路徑,你應該這樣做:

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