我正在尝试使用以下代码在此URL:http://hitbullseye.com/includes/testmaster_pdffiles/CAT 2013.pdf上下载pdf文件:

URL url = new URL("http://hitbullseye.com/includes/testmaster_pdffiles/CAT%202013.pdf");
URLConnection ucon = url.openConnection();
ucon.setReadTimeout(40000);
ucon.setConnectTimeout(40000);
InputStream is = ucon.getInputStream();


它在InputStream上抛出FileNotFound异常= = ucon.getInputStream();
我已在清单中授予Internet许可。我也正在下载其他文件,但是这个文件没有下载。

我的Logcat:

06-12 15:59:50.091: E/Note:(28745): file url: http://hitbullseye.com/includes/testmaster_pdffiles/CAT%202013.pdf

06-12 15:59:50.251: D/libc(28745): [NET] getaddrinfo  hn 19, servname NULL, ai_family 0+

06-12 15:59:50.251: D/libc(28745): [SMD][Mode1]: Screen on and original TTL is not expired,bl_level=131

06-12 15:59:50.251: D/libc(28745): FOUND IN CACHE entry=0x52788a30

06-12 15:59:50.541: W/System.err(28745): java.io.FileNotFoundException: http://www.hitbullseye.com/includes/testmaster_pdffiles/CAT 2013.pdf

06-12 15:59:50.551: W/System.err(28745):    at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)


我还通过以下代码对网址进行了编码:

URI urii;
    try {
        urll = new URL(downloadUrl);
        urii = new URI(urll.getProtocol(), urll.getUserInfo(),
                urll.getHost(), urll.getPort(), urll.getPath(),
                urll.getQuery(), urll.getRef());
        urll = urii.toURL();
        downloadUrl = urll.toString();
    } catch (Exception e1) {
        e1.printStackTrace();
    }


我没有发布任何重复的问题。我已经读过HTTP URL Address Encoding in Java,但是我猜它有些不同的问题。请帮忙!甚至DownloadManager也没有下载它,返回HTTP_DATA_ERROR。

最佳答案

您提供的页面似乎给出了301-永久移动的响应。尝试改用HttpURLConnection。

HttpURLConnection ucon = url.openConnection();


还要尝试setInstanceFollowRedirects遵循重定向,以防万一禁用重定向。

ucon.setInstanceFollowRedirects(true);

10-06 13:56
查看更多