berserk
berserk

Reputation: 2728

Issue in downloading file android

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.

Upvotes: 1

Views: 312

Answers (3)

RustyHearth
RustyHearth

Reputation: 344

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

Upvotes: 1

Sreekumar R
Sreekumar R

Reputation: 194

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

Upvotes: 1

Emanuel
Emanuel

Reputation: 8106

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.

Upvotes: 0

Related Questions