【问题标题】:405 Error with setChunkedStreamingMode()405 错误与 setChunkedStreamingMode()
【发布时间】: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


【解决方案1】:

我终于通过在HTTPUrlConnection 中为setChunkedStreamingMode(4096); 的客户端配置设置以下属性来解决这个问题:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-24
    • 2011-10-13
    • 2017-11-13
    • 2014-01-09
    • 2011-04-10
    • 2010-11-04
    • 2016-03-04
    • 2018-06-16
    相关资源
    最近更新 更多