2016-04-19 75 views
0

我試圖將臨時文件發送到REST風格的Web服務。我使用下面的函數來做到這一點:Javax ClientBuilder post()在發送到Web服務之前刪除文件

private static String nlpFileUpload(String filePath) throws IOException { 
    // Check is file exists 
    File file = new File(filePath); 
    if(!file.isFile()) { 
     throw new FileNotFoundException(); 
    } 

    // Upload file to web service 
    return ClientBuilder.newClient() 
     .target(nlprestURL + "upload") 
     .request() 
     .post(
       Entity.entity(file, MediaType.APPLICATION_OCTET_STREAM) 
     ).readEntity(String.class); 
} 

的問題是,在方法.post()文件有時會從磁盤中刪除,並返回FileNotFound例外:

[ERROR] [04/18/2016 19:04:35.945] [application-akka.actor.default-dispatcher-2] [TaskInvocation] java.io.FileNotFoundException: /tmp/multipartBody3655134388737861177asTemporaryFile (No such file or directory) 
javax.ws.rs.ProcessingException: java.io.FileNotFoundException: /tmp/multipartBody3655134388737861177asTemporaryFile (No such file or directory) 
at org.glassfish.jersey.client.internal.HttpUrlConnector.apply(HttpUrlConnector.java:287) 
at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:255) 
at org.glassfish.jersey.client.JerseyInvocation$1.call(JerseyInvocation.java:684) 
at org.glassfish.jersey.client.JerseyInvocation$1.call(JerseyInvocation.java:681) 
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:228) 
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:444) 
at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:681) 
at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:437) 
at org.glassfish.jersey.client.JerseyInvocation$Builder.post(JerseyInvocation.java:343) 
at pl.edu.pwr.services.serel.SerelServiceRs.nlpFileUpload(SerelServiceRs.java:97) 
at pl.edu.pwr.services.serel.SerelServiceRs.processFile(SerelServiceRs.java:181) 
at jobs.ProcessDocuments.run(ProcessDocuments.java:54) 
at akka.dispatch.TaskInvocation.run(AbstractDispatcher.scala:41) 
at akka.dispatch.ForkJoinExecutorConfigurator$AkkaForkJoinTask.exec(AbstractDispatcher.scala:393) 
at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260) 
at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339) 
at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979) 
at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107) 

Caused by: java.io.FileNotFoundException: /tmp/multipartBody3655134388737861177asTemporaryFile (No such file or directory) 
at java.io.FileInputStream.open0(Native Method) 
at java.io.FileInputStream.open(FileInputStream.java:195) 
at java.io.FileInputStream.<init>(FileInputStream.java:138) 
at org.glassfish.jersey.message.internal.FileProvider.writeTo(FileProvider.java:115) 
at org.glassfish.jersey.message.internal.FileProvider.writeTo(FileProvider.java:67) 
at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.invokeWriteTo(WriterInterceptorExecutor.java:265) 
at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.aroundWriteTo(WriterInterceptorExecutor.java:250) 
at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162) 
at org.glassfish.jersey.message.internal.MessageBodyFactory.writeTo(MessageBodyFactory.java:1130) 
at org.glassfish.jersey.client.ClientRequest.writeEntity(ClientRequest.java:502) 
at org.glassfish.jersey.client.internal.HttpUrlConnector._apply(HttpUrlConnector.java:388) 
at org.glassfish.jersey.client.internal.HttpUrlConnector.apply(HttpUrlConnector.java:285) 
... 19 more 

但這種異常不會總是出現。在調試過程中,我發現有時文件被髮送到正確的服務,但在上面的其他時間出現異常。 我不知道這裏有什麼問題。該服務能夠讀取文件爲MediaType.APPLICATION_OCTET_STREAM,但我認爲問題出在客戶端。爲什麼.post()方法在將文件發送到Web服務之前刪除我的文件?

我在我的應用程序中使用Play框架2.3.9。函數由Akka調度程序調用。

回答

0

播放臨時文件的問題在於,它們在被垃圾收集時被刪除。臨時文件在客戶端上傳文件時創建,並作爲MultipartFormData處理。這一定是你的代碼有時候工作的原因。

要解決這個問題,您可以嘗試保留對該文件的引用,以免它被垃圾收集器清理。你可以通過改變函數的定義來使用這個引用:

static String nlpFileUpload(File file) throws IOException 
+0

我用System.gc()調用垃圾收集器,事實上我的臨時文件被刪除了。我通過將內容保存到常規文件來解決此問題。仍然不知道爲什麼臨時文件在'.post()'方法中完全刪除。 – koralgoll

相關問題