1

I am trying to download a pdf file on this url: http://hitbullseye.com/includes/testmaster_pdffiles/CAT 2013.pdf using the following code:

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();

It is throwing FileNotFound Exception at InputStream is = ucon.getInputStream(); I have given Internet permission in my manifest. I am downloading other files too, but this one is not downloading.

My 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)

I encoded the url also by following code:

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();
    }

I am not posting any duplicate question. I already read HTTP URL Address Encoding in Java, but I guess its some different issue. Please help! Even DownloadManager is not downloading it, returning HTTP_DATA_ERROR.

Community
  • 1
  • 1
berserk
  • 2,690
  • 3
  • 32
  • 63
  • some times browser url not allow to download file from server programmatically.so download this pdf manually and place it on other ftp server or your server and pass that url for downloading it will work for you. – chet's Jun 12 '14 at 13:38
  • I would start by checking the `getResponseCode` value (need to cast to `HttpUrlConnection` first) – njzk2 Jun 12 '14 at 13:51

3 Answers3

1

The page you provided seems to give a 301 - Permanently moved response. Try using HttpURLConnection instead.

HttpURLConnection ucon = url.openConnection();

Also try setInstanceFollowRedirects to follow redirection just in case redirection is disabled.

ucon.setInstanceFollowRedirects(true);
Sreekumar R
  • 194
  • 1
  • 10
  • followRedirects is enabled by default and follow up to 5 redirects. – Emanuel Jun 12 '14 at 13:48
  • 1
    `setFollowRedirects` is a static method. there is no need to cast `openConnection` to an `HttpUrlConnection`, as you can directly call `HttpUrlConnection.setFollowRedirects(true)`. Also, I would have though this to be enabled by default. – njzk2 Jun 12 '14 at 13:49
  • Getting this on url.openConnection() Type mismatch: cannot convert from URLConnection to HttpURLConnection – berserk Jun 12 '14 at 14:02
  • True. That is why I added it separate just in case it was disabled somewhere else in code. Even then the non-static setInstanceFollowRedirects makes more sense. Updated the answer. – Sreekumar R Jun 12 '14 at 14:04
  • Do you mean it is redirecting to other url? – berserk Jun 12 '14 at 14:19
  • As mentioned in another answer, the URL with www is the correct one. As far as I understood when given otherwise, the site is redirecting you to a URL with www – Sreekumar R Jun 12 '14 at 14:24
  • But I am downloading other files with same type of url(no www) and they working fine. Example: http://hitbullseye.com/includes/testmaster_pdffiles/installation.pdf – berserk Jun 12 '14 at 14:55
  • On checking in a browser with developer options, both these URLs get a 301 response redirecting to one with www. Somehow the URLConnection does not seem to handle the space in the redirection URL. As seen in logs, it shows the www version of URL. [See a discussion on the same](https://community.oracle.com/thread/1147444?start=0&tstart=0) – Sreekumar R Jun 13 '14 at 07:49
1

add www. to the link it worked for me,i counted the bytes and the output was exactly the file's size.

edit: as you can see it worked for me

            URL url=null;
    try {
        url = new URL("http://www.hitbullseye.com/includes/
                                      testmaster_pdffiles/CAT%202013.pdf");
    } catch (MalformedURLException e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
    }
    URLConnection ucon=null;
    try {
        ucon = url.openConnection();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    ucon.setReadTimeout(40000);
    ucon.setConnectTimeout(40000);
    try {


        InputStream is = ucon.getInputStream();
        DataOutputStream in2=new DataOutputStream(new FileOutputStream(new File("D:\\site\\data.pdf")));

        int count=0;
        int datar=is.read();
        while(datar!=-1){
            in2.write(datar);
            count++;
            datar=is.read();
        }
        in2.close();
        System.out.println(count);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

answer to your last comment: i was interested too why it worked only with www and i checked with whireshark and i tried with few links,the link that you gave in the question without www is the only link that responded with 301 moved permenantly ,could be that is the problem im not sure 100%. i watched further and after the 301 response the url gets cut of after the space

user1779374
  • 334
  • 1
  • 4
  • 15
  • But in the error, it is showing the exactly same path. It adds www itself. – berserk Jun 12 '14 at 13:58
  • i see that too but i don't know why, i tried things with 'URI' still nothing,just added www from habit and suddenly worked,it displayed the exact length 629450 bytes – user1779374 Jun 12 '14 at 14:03
0

First try to encode the url (and yes, with whitespaces).

URL url = new URL(URLEncoder.encode("http://hitbullseye.com/includes/testmaster_pdffiles/CAT 2013.pdf", "UTF-8"));

and of course check for MalformedURLException

try {
   URL url = ....
} catch (MalformedURLException e) {
   e.printStackTrace();
}

it it's still not reachable try if your network on your device/testing device let you reach the file with the androids default browser. Maybe it's firewalled and/or the dns do not match.

Emanuel
  • 8,027
  • 2
  • 37
  • 56
  • If I do this, thenthe url formed will be http%3A%2F%2Fhitbullseye.com%2Fincludes%2Ftestmaster_pdffiles%2FCAT+2013.pdf and it will not work. – berserk Jun 12 '14 at 13:55