2016-09-30 96 views
0

我試圖從我的服務器上下載文件。我的問題是,如果我使用chunkedStreamingMode服務器返回給我一個405 - Method not Allowed錯誤。如果我沒有設置chunkedStreamingMode,則下載完美。405 setChunkedStreamingMode錯誤()

我不明白爲什麼啓用chunkedStreamingMode的文件下載被我的服務器拒絕。有什麼我在這裏做錯了嗎?或者我需要在服務器上設置一些東西來獲得chunkedStreamingMode的工作?

在我的服務器我有:

@GET 
@Path("/{fileId}") 
@Produces(MediaType.APPLICATION_OCTET_STREAM) 
public Response downloadFile(@PathParam("fileId") long fileId, @Context HttpServletRequest req, 
       @Context HttpServletResponse response) throws IOException { 
.. 
} 

在客戶端上使用:

WebTarget target = rootTarget.path(uri);        
Invocation.Builder builder = target.request(MediaType.APPLICATION_OCTET_STREAM_TYPE); 
response = builder.get(); 

我用下面的HttpUrlConnectorProvider我的客戶端上:

private static HttpUrlConnectorProvider buildHttpUrlConnectorProvider(){ 
    HttpUrlConnectorProvider.ConnectionFactory factory = new HttpUrlConnectorProvider.ConnectionFactory() { 
      @Override 
      public HttpURLConnection getConnection(URL url) throws IOException {      
       HttpURLConnection result = (HttpURLConnection) url.openConnection(); 
       result.setChunkedStreamingMode(4096);       
       result.setDoOutput(true); 
       return result; 
      } 
     }; 
    return new HttpUrlConnectorProvider().connectionFactory(factory); 
} 
+0

通過在調試器中運行服務器並逐步完成其請求處理程序,您可以很快診斷出這一點。你會發現它觸發405的地方。它可能會被一些配置驅動的守衛包圍。 – slim

+0

此外,要獲得任何答案,你應該告訴我們你正在使用的服務器。 – slim

+0

此外,我發現一個很好的經驗法則是避免'HTTPUrlConnection' - 它充滿了陷阱。使用Apache HttpClient可以節省很多麻煩(但這裏可能不一定是問題)。 – slim

回答

0

我終於解決了這個通過在中爲客戶端配置中的setChunkedStreamingMode(4096);設置以下屬性:

.property(ClientProperties.REQUEST_ENTITY_PROCESSING, "CHUNKED");