簡體   English   中英

如何使用Java流從Web獲取PDF文件

[英]How to get PDF file from web using java streams

我需要從網上下載PDF文件,例如http://www.math.uni-goettingen.de/zirkel/loesungen/blatt15/loes15.pdf此鏈接。 我必須使用Streams做到這一點。 有了圖像,我可以正常使用:

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則無法正常工作。 可能是什么問題呢 ?

我對您的代碼進行了較小的修改以修復語法錯誤,這似乎可行(如下)。 考慮將close()語句放在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();
        }
    }
}

試試這個,這完成了工作(並且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();
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM