从重定向到 HTTPS 连接的 HTTP 连接下载文件

发布于 2024-12-25 19:47:22 字数 2469 浏览 0 评论 0原文

我在项目中使用 Dropbox 从 dropbox 获取小网址,类似于 http://www.db.tt/xyzabc

当我尝试在 HTC My touch 中下载文件时,我的代码工作正常,但如果我在 Motorola Atrix 中尝试,则会抛出异常未知主机 db.tt.

实际上,首先我有像 http://www.db.tt/xyzabc 这样的 url,这是 HTTP url,我打开它,然后得到 例外,在例外情况下,我得到实际的 url包含 file 且为 HTTPS url 的文件例外。我开始下载文件,这是我的代码,对我有用:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浮生面具三千个 2025-01-01 19:47:22

好的,经过几次尝试,我让它解决了我自己的问题,如果有人遇到同样的问题,这个解决方案将会很有帮助,它需要一些错误处理和根据需要进行修改

在看到 Connection 的类层次结构之后,我发现 HttpsURLConnection 是 HttpURLConnection 的子级,而 HttpURLConnection 是UrlConnection 的子级,因此我使用 HTTPConnection 而不是 UrlConnection,并且由于 HttpsUrlConnection 对于 HttpsUrlConnection 来说是具体的,它解决了我的问题
我继续迭代,直到重定向后获得 Https url

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.");
    }
}

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.");
    }
}
素罗衫 2025-01-01 19:47:22

这个答案在一定程度上是有效的。我有一个类似的解决方案这里

Atrix 上的 Dropbox 短超链接仍然存在问题。它们从 http 重定向到 https,但没有重定向到所需的文件,而是从 Dropbox 内部获取大量 html。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文