2016-12-17 10 views
0

WebからPDFファイルをダウンロードする必要があります。たとえば、http://www.math.uni-goettingen.de/zirkel/loesungen/blatt15/loes15.pdfこのリンクをクリックします。私はStreamsを使用してそれを行う必要があります。画像では、私によってうまく動作します:Javaストリームを使用してWebからPDFファイルを取得する方法

public static void main(String[] args) { 
     try {   
       //get the url page from the arguments array 
       String arg = args[0]; 
       URL url = new URL("https://cs7065.vk.me/c637923/v637923205/25608/AD8WhOSx1ic.jpg"); 

       try{ 
        //jpg 
        InputStream in = new BufferedInputStream(url.openStream()); 
        ByteArrayOutputStream out = new ByteArrayOutputStream(); 
        byte[] buf = new byte[131072]; 
        int n = 0; 
        while (-1!=(n=in.read(buf))) 
        { 
         out.write(buf, 0, n); 
        } 
        out.close(); 
        in.close(); 
        byte[] response = out.toByteArray(); 
        FileOutputStream fos = new FileOutputStream("borrowed_image.jpg"); 
        fos.write(response); 
        fos.close(); 
       } 
     catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

しかし、PDfでは動作しません。何が問題なの?

+2

「うまくいかない」ということを明確にしてください。コードが例外をスローしていますか?読めないpdfファイルが生成されていますか?他に何か? – mangotang

+0

@mangotang、それは未読のPDFファイルを作成しています – Vladislav

+0

あなたのコードに構文エラーを修正し、このurlを使用してサンプルpdfを入手し、あなたのコードは正常に動作しました:http://www.pdf995.com/samples/pdf .pdf – mangotang

答えて

2

構文エラーを修正するためにコードを少し編集しましたが、これは以下のように動作します。ブロックfinallyclose()ステートメントを配置することを検討してください。

package org.snb; 

import java.io.BufferedInputStream; 
import java.io.ByteArrayOutputStream; 
import java.io.FileOutputStream; 
import java.io.InputStream; 
import java.net.URL; 

public class PdfTester { 
    public static void main(String[] args) { 
     //get the url page from the arguments array 

     try{ 
      //String arg = args[0]; 
      URL url = new URL("http://www.pdf995.com/samples/pdf.pdf"); 
      //jpg 
      InputStream in = new BufferedInputStream(url.openStream()); 
      ByteArrayOutputStream out = new ByteArrayOutputStream(); 
      byte[] buf = new byte[131072]; 
      int n = 0; 
      while (-1!=(n=in.read(buf))) 
      { 
       out.write(buf, 0, n); 
      } 
      out.close(); 
      in.close(); 
      byte[] response = out.toByteArray(); 
      FileOutputStream fos = new FileOutputStream("/tmp/bart.pdf"); 
      fos.write(response); 
      fos.close(); 
     } 
     catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 
+0

ありがとう! – Vladislav

2

これを試してみると、これで作業が完了しました(そしてpdfは読み込み可能です)。 URLを要求する際に例外がスローされるかどうかを確認してください。

public static void main(String[] args) { 
     try { 
      //get the url page from the arguments array 
      URL url = new URL("http://www.math.uni-goettingen.de/zirkel/loesungen/blatt15/loes15.pdf"); 

      try { 
       InputStream in = new BufferedInputStream(url.openStream()); 
       ByteArrayOutputStream out = new ByteArrayOutputStream(); 
       byte[] buf = new byte[131072]; 
       int n = 0; 
       while (-1 != (n = in.read(buf))) { 
        out.write(buf, 0, n); 
       } 
       out.close(); 
       in.close(); 
       byte[] response = out.toByteArray(); 
       FileOutputStream fos = new FileOutputStream("loes15.pdf"); 
       fos.write(response); 
       fos.close(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
+0

あなたの返事をありがとう、本当にありがとう – Vladislav

関連する問題