2016-12-17 1 views
0

내가 예를 http://www.math.uni-goettingen.de/zirkel/loesungen/blatt15/loes15.pdf이 링크, 웹에서 PDF 파일을 다운로드해야 스트림. 나는 Streams를 사용하여 그것을해야만한다. 이미지가 나와 잘 작동합니다.자바를 사용하여 웹에서 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, 그것이 unreadble PDF 파일 – Vladislav

+0

을 만드는 난 당신의 코드에서 구문 오류를 수정하고, 샘플 PDF를 얻기 위해이 URL을 사용하고, 코드가 잘 작동 : http://www.pdf995.com/samples/pdf .pdf – mangotang

답변

2

구문 오류를 수정하기 위해 코드를 약간 수정했으며, 아래에서 작동하는 것 같습니다. 문을 finally 블록에 삽입하는 것이 좋습니다.

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

관련 문제