【问题标题】:How to get PDF file from web using java streams如何使用 Java 流从 Web 获取 PDF 文件
【发布时间】:2016-12-17 18:31:52
【问题描述】:

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

【问题讨论】:

  • 请澄清“不起作用”是什么意思。代码是否抛出异常?它是否会生成不可读的 pdf 文件?还有什么?
  • @mangotang,它正在创建一个不可读的 PDF 文件
  • 我修复了您代码中的语法错误,并使用此 url 获取了示例 pdf,您的代码运行良好:pdf995.com/samples/pdf.pdf
  • 清理try-statements 后为我工作。看起来还不错,但缺少 doc cmets,没有使用 try-with-resource 和过度缓冲。

标签: java pdf inputstream


【解决方案1】:

我对您的代码进行了少量编辑以修复语法错误,这似乎有效(如下)。考虑将您的 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();
        }
    }
}

【讨论】:

    【解决方案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();
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-07
      • 2013-10-13
      • 1970-01-01
      • 2011-06-14
      • 2011-04-30
      • 2020-05-17
      • 1970-01-01
      相关资源
      最近更新 更多