405 setChunkedStreamingMode错误()
问题描述:
我试图从我的服务器上下载文件。我的问题是,如果我使用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);
}
答
我终于解决了这个通过在中为客户端配置中的setChunkedStreamingMode(4096);
设置以下属性:
.property(ClientProperties.REQUEST_ENTITY_PROCESSING, "CHUNKED");
通过在调试器中运行服务器并逐步完成其请求处理程序,您可以很快诊断出这一点。你会发现它触发405的地方。它可能会被一些配置驱动的守卫包围。 – slim
此外,要获得任何答案,你应该告诉我们你正在使用的服务器。 – slim
此外,我发现一个很好的经验法则是避免'HTTPUrlConnection' - 它充满了陷阱。使用Apache HttpClient可以节省很多麻烦(但这里可能不一定是问题)。 – slim