PDF存在于URL;但下载错误

时间:2011-12-21 07:26:07

标签: java url pdf

我在网址上有一些PDF文件。我可以在浏览器中查看文件。 示例网址:http://www.ncbi.nlm.nih.gov/pmc/articles/PMC109492/pdf/pgen.1000626.pdf?tool=pmcentrez

但是当我做的时候

URL url = new URL("http://www.ncbi.nlm.nih.gov/pmc/articles/PMC" + pmcid + "/pdf/pgen.1000626.pdf?tool=pmcentrez");

URLConnection con = url.openConnection();
InputStream urlIn = con.getInputStream();

我得到java.io.IOException: Server returned HTTP response code: 403

我做错了什么?

提前致谢

2 个答案:

答案 0 :(得分:2)

我可以做很多事情。仅举几例:

  • 您应该验证
  • 您应该设置User-Agent标题
  • 您应该设置Accept标题

答案 1 :(得分:1)

        URL url = new URL("http://www.ncbi.nlm.nih.gov/pmc/articles/PMC109492/pdf/pgen.1000626.pdf?tool=pmcentrez");


        //for pdf
        response.setContentType("application/pdf");
        response.setHeader("Content-Disposition", "attachment; filename=report" + ".pdf");

        URLConnection connection = url.openConnection();
        InputStream stream = connection.getInputStream();

        BufferedOutputStream outs = new BufferedOutputStream(response.getOutputStream());
        int len;
        byte[] buf = new byte[1024];
        while ((len = stream.read(buf)) > 0) {
            outs.write(buf, 0, len);
        }
        outs.close();