如何使用java流从Web获取PDF文件

时间:2016-12-17 18:31:53

标签: java pdf inputstream

我需要从网上下载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它不起作用。可能是什么问题?

2 个答案:

答案 0 :(得分: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();
        }
    }

答案 1 :(得分: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(); } } } 块中。

{{1}}