无法从返回重定向的URL下载文件

时间:2018-06-28 17:45:20

标签: java download

我正在尝试将一组文件从远程服务器下载到本地文件。

这应该非常简单,我尝试了2种不同的方法,但是每种方法都有其优缺点。

第一种方法-Apache HttpGet / CloseableHttpResponse

CloseableHttpClient closeableHttpClient = HttpClients.createDefault();

URL encodeUrl = new URL(UriUtils.encodePath(documentUrl, StandardCharsets.UTF_8.name()));

File file = new File(tmpdir + File.separator + FilenameUtils.getName(doc.getDocumentURL()));

HttpGet httpget = new HttpGet(encodeUrl.toString());

HttpEntity entity = null;

try(CloseableHttpResponse response = closeableHttpClient.execute(httpget) {

    entity = response.getEntity();

    FileUtils.copyInputStreamToFile(entity.getContent(), file);

} finally {
    EntityUtils.consumeQuietly(entity);
}

第二种方法-FileUtils.copyURLToFile

URL encodeUrl = new URL(UriUtils.encodePath(documentUrl, StandardCharsets.UTF_8.name()));

String filename = FilenameUtils.getName(documentUrl);

File file = new File(tmpdir + File.separator + filename);

FileUtils.copyURLToFile(encodeUrl, file, 10000, 30000);

两种方法都遇到问题。

1。使用Apache HttpGet / CloseableHttpResponse:

某些URL名称中包含空格或什至是奇怪的字符,例如:

http://download-service/Document Number 2015 .pdf

正确下载了所有名称正确的文件,但名称错误的文件出现此错误:

org.apache.http.client.ClientProtocolException: null
        ...
Caused by: org.apache.http.ProtocolException: Invalid redirect URI: http://download-service/Document Number 2015 .pdf
        at org.apache.http.impl.client.DefaultRedirectStrategy.createLocationURI(DefaultRedirectStrategy.java:200) ~[httpclient-4.5.5.jar:4.5.5]
        at org.apache.http.impl.client.DefaultRedirectStrategy.getLocationURI(DefaultRedirectStrategy.java:148) ~[httpclient-4.5.5.jar:4.5.5]
        at org.apache.http.impl.client.DefaultRedirectStrategy.getRedirect(DefaultRedirectStrategy.java:221) ~[httpclient-4.5.5.jar:4.5.5]
        at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:122) ~[httpclient-4.5.5.jar:4.5.5]
        at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185) ~[httpclient-4.5.5.jar:4.5.5]
        ... 108 common frames omitted
Caused by: java.net.URISyntaxException: Illegal character in path at index 48: http://download-service/Document Number 2015 .pdf
        at java.net.URI$Parser.fail(URI.java:2848) ~[na:1.8.0_131]
        at java.net.URI$Parser.checkChars(URI.java:3021) ~[na:1.8.0_131]
        at java.net.URI$Parser.parseHierarchical(URI.java:3105) ~[na:1.8.0_131]
        at java.net.URI$Parser.parse(URI.java:3053) ~[na:1.8.0_131]
        at java.net.URI.<init>(URI.java:588) ~[na:1.8.0_131]
        at org.apache.http.impl.client.DefaultRedirectStrategy.createLocationURI(DefaultRedirectStrategy.java:189) ~[httpclient-4.5.5.jar:4.5.5]
        ... 112 common frames omitted

2。使用FileUtils.copyURLToFile:

在这种情况下,不会发生上述错误,所有文件似乎都已正确下载,但所有文件均为空。

我已经阅读了这两种方法的文档,但是我无法理解这两种方法的失败之处。

2 个答案:

答案 0 :(得分:1)

我已经研究了以下错误,并开始怀疑该问题可能与服务器执行的重定向有关:

Caused by: org.apache.http.ProtocolException: Invalid redirect URI:

这样,我创建了一个CustomRedirectStrategy,它将对重定向执行URI编码:

public class CustomRedirectStrategy extends DefaultRedirectStrategy {

    @Override
    public URI getLocationURI(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException {
        try {
            String uri = response.getFirstHeader("location").getValue();
            URL url = new URL(uri);
            return new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
        } catch (Exception ex){
            throw new ProtocolException(ex.getMessage());
        }
    }
}

此策略是将它们应用于CloseableHttpClient:

CloseableHttpClient client = HttpClients.custom().setRedirectStrategy(new CustomRedirectStrategy()).build();

通过此更改,所有文件现在都已正确下载。

答案 1 :(得分:0)

我认为您应该尝试通过以下方式对您的网址进行编码:Encode URL

似乎,在两种情况下,您都输入了错误的URL。