Yahya Arshad
Yahya Arshad

Reputation: 1626

Downloading a file from HTTP connection which redirect to HTTPS connection

I am using Dropbox in my project to get tiny url from dropbox which is like http://www.db.tt/xyzabc.

When I try to download the file in HTC My touch my code works fine, but if I try in Motorola Atrix it throws exception unknown host db.tt.

Actually first I have url like http://www.db.tt/xyzabc which is HTTP url I open it than I get exception and in exception I get actual url to file which contain file and is HTTPS url in exception. I start downloading file here is my code which work for me:

public static void fileUrl(String fAddress, String localFileName,
        String destinationDir) {
    OutputStream outStream = null;
    URLConnection uCon = null;

    InputStream is = null;
    try {
        URL url;
        byte[] buf;
        int ByteRead, ByteWritten = 0;
        url = new URL(fAddress);
        outStream = new BufferedOutputStream(new FileOutputStream(
                destinationDir + localFileName));

        try {
            // Here i have "http://www.db.tt/xyzabc"
                       // after i hit url i get exception and in exception that
                       // FileNotFoundException at https://www.dropbox.com/abcxyz
                     // i get actual actual url i parse that exception and 
                     //retrive https://www.dropbox.com/xyzabc(actual url)
                      // but in motorolla atrix instead of that url i get
                     // unknownhost exception "db.tt"




            uCon = url.openConnection();   
        //  uCon.connect();

            is = uCon.getInputStream();
        } catch (Exception e) {
            url = new URL(e.getMessage().substring(
                    e.getMessage().indexOf("https"),
                    e.getMessage().length()));
            outStream = new BufferedOutputStream(new FileOutputStream(
                    destinationDir + localFileName));

            uCon = url.openConnection();
            is = uCon.getInputStream();
        }

        buf = new byte[size];
        while ((ByteRead = is.read(buf)) != -1) {
            outStream.write(buf, 0, ByteRead);
            ByteWritten += ByteRead;
        }
        System.out.println("Downloaded Successfully.");
        System.out.println("File name:\"" + localFileName
                + "\"\nNo ofbytes :" + ByteWritten);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
            outStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Upvotes: 1

Views: 3869

Answers (2)

loser114491
loser114491

Reputation: 131

This answer works - to an extent. I have a similar solution here

There is still a problem with Dropbox short hyperlinks on Atrix. They redirect from http to https but NOT to the required file, instead I get a whole lot of html from inside Dropbox.

Upvotes: 1

Yahya Arshad
Yahya Arshad

Reputation: 1626

ok after few attempt i made it solve my self and here is the solution will be helpfull if someone got same problem it requires some error handling and modification according to need

After seeing class heirarchy of Connection i found that HttpsURLConnection is child of HttpURLConnection and HttpURLConnection is child of UrlConnection so i i used HTTPConnection instead of UrlConnection and as HttpsUrlConnection is concrete for HttpsUrlConnection it solved my problem i continue iterating till i get Https url after redirect

public static void fileUrl(String fAddress, String localFileName,
        String destinationDir) {
    OutputStream outStream = null;
    URLConnection uCon = null;
    HttpURLConnection mHttpCon;

    InputStream is = null;
    try {

        URL url;
        byte[] buf;
        int ByteRead, ByteWritten = 0;
        url = new URL(fAddress);
        outStream = new BufferedOutputStream(new FileOutputStream(
                destinationDir + localFileName));

        try {

            mHttpCon = (HttpURLConnection) url.openConnection();

            while (!url.toString().startsWith("https")) {
                mHttpCon.getResponseCode();
                url = mHttpCon.getURL();
                mHttpCon = (HttpURLConnection) url.openConnection();

            }

            is = mHttpCon.getInputStream();
        } catch (Exception e) {
            e.printStackTrace();
            // url = new URL(e.getMessage().substring(
            // e.getMessage().indexOf("https"),
            // e.getMessage().length()));
            // outStream = new BufferedOutputStream(new FileOutputStream(
            // destinationDir + localFileName));
            //
            // uCon = url.openConnection();
            // is = uCon.getInputStream();
        }

        buf = new byte[size];
        while ((ByteRead = is.read(buf)) != -1) {
            outStream.write(buf, 0, ByteRead);
            ByteWritten += ByteRead;
        }
        System.out.println("Downloaded Successfully.");
        System.out.println("File name:\"" + localFileName
                + "\"\nNo ofbytes :" + ByteWritten);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
            outStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

public static void fileDownload(String fAddress, String destinationDir) {

    int slashIndex = fAddress.lastIndexOf('/');
    int periodIndex = fAddress.lastIndexOf('.');

    String fileName = fAddress.substring(slashIndex + 1);

    if (periodIndex >= 1 && slashIndex >= 0
            && slashIndex < fAddress.length() - 1) {
        fileUrl(fAddress, fileName, destinationDir);
    } else {
        System.err.println("path or file name.");
    }
}

Upvotes: 1

Related Questions