从BufferedInputStream捕获404错误

时间:2017-05-23 11:21:46

标签: java html error-handling

我有一个html页面下载功能。 这是代码:

public class pageDownload {
    public static void down(final String filename, final String urlString)
            throws MalformedURLException, IOException {
        BufferedInputStream in = null;
        FileOutputStream fout = null;
        try {
            in = new BufferedInputStream(new URL(urlString).openStream());
            fout = new FileOutputStream(new File(filename));
            final byte data[] = new byte[1024];
            int count;
            while ((count = in.read(data, 0, 1024)) != -1) {
                fout.write(data, 0, count);
            }
        } catch (IOException e) {
            System.err.println("Caught IOException: " + e.getMessage());
        } catch (IndexOutOfBoundsException e) {
            System.err.println("IndexOutOfBoundsException: " + e.getMessage());
        }
        in.close();
        fout.close();
    }
}

正常工作,当我尝试下载不存在的页面时出现问题。在这种情况下,我无法弄清楚如何处理404错误。 有人有点想法吗?

2 个答案:

答案 0 :(得分:1)

你是说这样的意思吗?我添加了一个最终保存关闭Streams

public class pageDownload {
    public static void down(final String filename, final String urlString)
    throws MalformedURLException, IOException {

        BufferedInputStream in = null;
        FileOutputStream fout = null;
        try {
            in = new BufferedInputStream(new URL(urlString).openStream());
            fout = new FileOutputStream(new File(filename));

            final byte data[] = new byte[1024];
            int count;
            while ((count = in.read(data, 0, 1024)) != -1) {
                fout.write(data, 0, count);
            }
        } catch(FileNotFoundException ex)
        {
            System.err.println("Caught 404: " + e.getMessage());
        }
        catch(IOException ex)
        {
            System.err.println("Caught IOException: " + e.getMessage());
        }
        catch(IndexOutOfBoundsException e)
        {
            System.err.println("IndexOutOfBoundsException: " + e.getMessage());
        }
        finally{
            if(in != null)
                try { in.close(); } catch ( IOException e ) { }
            if(fout != null)
                try { fout.close(); } catch ( IOException e ) { }
        }
    } 
}

答案 1 :(得分:0)

您的问题是,当您尝试关闭流时,您会收到NullPointerException。你应该在finally子句中关闭它们或使用try with resources

public static void down(final String filename, final String urlString)
        throws IOException {
    try (BufferedInputStream in = new BufferedInputStream(new URL(urlString)
            .openStream());
         FileOutputStream fout = new FileOutputStream(new File(filename))) {
        final byte data[] = new byte[1024];
        int count;
        while ((count = in.read(data, 0, 1024)) != -1) {
            fout.write(data, 0, count);
        }
    } catch (IOException e) {
        System.err.println("Caught IOException: " + e.getMessage());
    } catch (IndexOutOfBoundsException e) {
        System.err.println("IndexOutOfBoundsException: " + e.getMessage());
    }
}