2017-05-23 4 views
0

私はhtmlページのダウンロードのための機能を持っています。ここで コードです:BufferedInputStreamからキャッチ404エラー

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エラーを処理する方法を理解できません。 誰か考えていますか?

+0

このメソッドを呼び出す方法はありますか? –

+0

のようなページを使った例を挙げてみましょう:pageDownload.down(newPath + "\\ out.html"、links.get(i));最初のパラメータは、ページが保存され、リンクがセカンドされる場所です。リストの最初の要素(http://www.learncpp.com/cpp-tutorial/12-2a-the-override-and-final-specifiers-and-covariant-return-types/)と2番目の要素(http ://riweb.tibeica.com/tests/l1_absolute?id = 1)が動作しません。 – John

+0

最初のリンクはOKです(ページをダウンロードしてください)。このエラーは次のとおりです:** Caught IOException:http://riweb.tibeica.com/tests/l1_absolute?id=1 スレッド "main" javaの例外.lang.NullPointerException \t project.pageDownload.down(pageDownload.java:37) \t project.Main.main(Main.java:60)** – John

答えて

0

あなたはこのような何かを意味していますか?私は最終的にストリームを閉じて保存するように追加しました

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) { } 
     } 
    } 
} 
+0

はい。それが問題でした。どうもありがとうございました。 :D – John

+0

お手伝いいただきありがとうございます。また、スタックオーバーフローを歓迎します。この回答または他の誰かがあなたの問題を解決した場合は、それを合格とマークしてください。 –

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()); 
    } 
} 
関連する問題