Java Jetty 9客户端设置内容范围值不正常
我正在使用Jetty 9.4 HttpClient尝试重新启动一个大文件下载。我正在使用Jetty 9.4服务器Java Jetty 9客户端设置内容范围值不正常,java,http,jetty,Java,Http,Jetty,我正在使用Jetty 9.4 HttpClient尝试重新启动一个大文件下载。我正在使用Jetty 9.4服务器 final InputStreamResponseListener listener = new InputStreamResponseListener(); final Request request = httpClient.newRequest(urlString); request.
final InputStreamResponseListener listener = new
InputStreamResponseListener();
final Request request = httpClient.newRequest(urlString); request.scheme(getUriScheme(urlString)).method(HttpMethod.GET).version(HttpVersion.HTTP_1_1).send(listener);
if (range > 0){
request.header("Range", "bytes=" + file.length() + "-")
}
final Response response = listener.get(getHttpConnTimeout(), TimeUnit.SECONDS);
// Get content length from response header
contentLength = response.getHeaders().getField(HttpHeader.CONTENT_LENGTH).getLongValue();
if (response.getStatus() == HttpStatus.PARTIAL_CONTENT_206) {
return listener.getInputStream();
}
if (response.getStatus() == HttpStatus.OK_200) {
return listener.getInputStream();
}
if (httpURLConnection != null) {
// This works
httpURLConnection.setRequestProperty("Range", "bytes=" + file.length() + "-");
}
if (httpsURLConnection != null) {
// This works
httpsURLConnection.setRequestProperty("Range", "bytes=" + file.length() + "-");
}
然而,当我使用Java的实现时,一切都很好(意味着不使用Jetty 9 HttpClient)
if (httpURLConnection != null) {
// This works
httpURLConnection.setRequestProperty("Range", "bytes=" + file.length() + "-");
}
if (httpsURLConnection != null) {
// This works
httpsURLConnection.setRequestProperty("Range", "bytes=" + file.length() + "-");
}
我做错了什么?有人有一个工作示例吗?它是(AFAIU)“header()”而不是“param()”
if (httpURLConnection != null) {
// This works
httpURLConnection.setRequestProperty("Range", "bytes=" + file.length() + "-");
}
if (httpsURLConnection != null) {
// This works
httpsURLConnection.setRequestProperty("Range", "bytes=" + file.length() + "-");
}
此外,“内容范围”不属于请求。朱利安是正确的。它帮助我消除了Jetty客户端的代码问题。事实证明,Jetty server需要“部分”(也称为接受范围)下载才能执行以下操作(否则Jetty HttpClient将无法使用部分):
if (httpURLConnection != null) {
// This works
httpURLConnection.setRequestProperty("Range", "bytes=" + file.length() + "-");
}
if (httpsURLConnection != null) {
// This works
httpsURLConnection.setRequestProperty("Range", "bytes=" + file.length() + "-");
}
它现在运行得很好。我的代码片段已被修改,希望将来能帮助其他人。默认情况下,Microsoft IIS接受http请求中的范围,但Jetty不接受。
if (httpURLConnection != null) {
// This works
httpURLConnection.setRequestProperty("Range", "bytes=" + file.length() + "-");
}
if (httpsURLConnection != null) {
// This works
httpsURLConnection.setRequestProperty("Range", "bytes=" + file.length() + "-");
}
实际上,服务器管理员应该按照此链接中的描述配置jetty:
if (httpURLConnection != null) {
// This works
httpURLConnection.setRequestProperty("Range", "bytes=" + file.length() + "-");
}
if (httpsURLConnection != null) {
// This works
httpsURLConnection.setRequestProperty("Range", "bytes=" + file.length() + "-");
}
我尝试了request.header(“范围”,“字节=”+Range+“-”)。它不起作用。我有一个NPE。我修改了代码以反映这个答案。
if (httpURLConnection != null) {
// This works
httpURLConnection.setRequestProperty("Range", "bytes=" + file.length() + "-");
}
if (httpsURLConnection != null) {
// This works
httpsURLConnection.setRequestProperty("Range", "bytes=" + file.length() + "-");
}