【发布时间】:2016-09-30 12:24:52
【问题描述】:
我正在尝试从我的服务器下载文件。我的问题是,如果我使用chunkedStreamingMode,服务器会返回一个405 - Method not Allowed 错误。如果我不设置chunkedStreamingMode,下载将完美运行。
我不明白为什么启用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);
}
【问题讨论】:
-
您可以通过在调试器中运行您的服务器并单步执行其请求处理程序来快速诊断此问题。你会找到它触发 405 的地方。可能它会被一些配置驱动的守卫包围。
-
另外,要获得任何答案,您应该告诉我们您使用的服务器。
-
另外,我发现一个好的经验法则是避免
HTTPUrlConnection——它充满了陷阱。使用 Apache HttpClient 可以省去很多麻烦(但不一定是这里的问题)。 -
@slim 谢谢你的建议,我会看看他们。我正在使用 glassfish 顺便说一句(我更新了标签)。
标签: java glassfish jersey-2.0