我在网址上有一些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
我做错了什么?
提前致谢
答案 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();