URLConnection 的错误处理

发布于 2024-12-27 04:45:52 字数 1179 浏览 0 评论 0原文

我有这种方法可以从雅虎财经下载 .csv 文件并将其保存在本地。它是在循环期间访问的,因此它会从列表中下载许多文件。然而,有时符号输入不正确、不再存在或连接超时。如何修改此方法,以便重试连接超时并跳过不正确的符号(意味着 url 不起作用)而不结束程序?

public static void get_file(String symbol){

    OutputStream outStream = null;
    URLConnection  uCon = null;
    InputStream is = null;

    String finance_url = "http://ichart.finance.yahoo.com/table.csv?s="+symbol;
    String destination = "C:/"+symbol+"_table.csv";

    try {
        URL Url;
        byte[] buf;
        int ByteRead,ByteWritten=0;
        Url= new URL(finance_url);

        outStream = new BufferedOutputStream(new FileOutputStream(destination));

        uCon = Url.openConnection();
        is = uCon.getInputStream();         
        buf = new byte[size];

        while ((ByteRead = is.read(buf)) != -1) {
            outStream.write(buf, 0, ByteRead);
            ByteWritten += ByteRead;
        }

    }catch (Exception e) {
        System.out.println("Error while downloading "+symbol);
        e.printStackTrace();
    }finally {
        try {
            is.close();
            outStream.close();
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
}

I have this method that downloads .csv files from yahoo finance and saves them locally. It is accessed during a loop so it is downloading many files from a list. However sometimes a symbol is entered incorrectly, no longer exists, or the connection times out. How can I amend this method so that connection time outs are retried and incorrect symbols (meaning the url does not work) are just skipped over without ending the program?

public static void get_file(String symbol){

    OutputStream outStream = null;
    URLConnection  uCon = null;
    InputStream is = null;

    String finance_url = "http://ichart.finance.yahoo.com/table.csv?s="+symbol;
    String destination = "C:/"+symbol+"_table.csv";

    try {
        URL Url;
        byte[] buf;
        int ByteRead,ByteWritten=0;
        Url= new URL(finance_url);

        outStream = new BufferedOutputStream(new FileOutputStream(destination));

        uCon = Url.openConnection();
        is = uCon.getInputStream();         
        buf = new byte[size];

        while ((ByteRead = is.read(buf)) != -1) {
            outStream.write(buf, 0, ByteRead);
            ByteWritten += ByteRead;
        }

    }catch (Exception e) {
        System.out.println("Error while downloading "+symbol);
        e.printStackTrace();
    }finally {
        try {
            is.close();
            outStream.close();
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
}

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

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

发布评论

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

评论(1

别理我 2025-01-03 04:45:52

为什么不在抛出异常时再次调用该方法呢?您可以缩小异常类型的范围以指示何时应启动重试。

public static void get_file(String symbol){

    OutputStream outStream = null;
    URLConnection  uCon = null;
    InputStream is = null;

    String finance_url = "http://ichart.finance.yahoo.com/table.csv?s="+symbol;
    String destination = "C:/"+symbol+"_table.csv";

    try {
        URL Url;
        byte[] buf;
        int ByteRead,ByteWritten=0;
        Url= new URL(finance_url);

        outStream = new BufferedOutputStream(new FileOutputStream(destination));

        uCon = Url.openConnection();
        is = uCon.getInputStream();         
        buf = new byte[size];

        while ((ByteRead = is.read(buf)) != -1) {
            outStream.write(buf, 0, ByteRead);
            ByteWritten += ByteRead;
        }

    }catch (Exception e) {
      getFile(symbol);
    }finally {
        try {
            is.close();
            outStream.close();
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Why not call the method again when an exception is thrown. You can narrow down the exception type to indicate when a retry should be initiated.

public static void get_file(String symbol){

    OutputStream outStream = null;
    URLConnection  uCon = null;
    InputStream is = null;

    String finance_url = "http://ichart.finance.yahoo.com/table.csv?s="+symbol;
    String destination = "C:/"+symbol+"_table.csv";

    try {
        URL Url;
        byte[] buf;
        int ByteRead,ByteWritten=0;
        Url= new URL(finance_url);

        outStream = new BufferedOutputStream(new FileOutputStream(destination));

        uCon = Url.openConnection();
        is = uCon.getInputStream();         
        buf = new byte[size];

        while ((ByteRead = is.read(buf)) != -1) {
            outStream.write(buf, 0, ByteRead);
            ByteWritten += ByteRead;
        }

    }catch (Exception e) {
      getFile(symbol);
    }finally {
        try {
            is.close();
            outStream.close();
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文