我一直在尝试从以下网址下载pdf文件:http://pdfobject.com/markup/examples/full-browser-window.html
Josh M建议以下解决方案适用于他的计算机。但是,我无法让它发挥作用。我的意思是以下代码将文件保存到目标,但是,下载文件的权重仅为984字节(通常应为18 Kb)。因此文件已损坏。我想不出为什么会发生这种情况的任何原因?
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
public final class FileDownloader {
private FileDownloader(){}
public static void main(String args[]) throws IOException{
download("http://pdfobject.com/markup/examples/full-browser-window.html", new File("C:\\Users\\Owner\\Desktop\\temporary\\myFile.pdf"));
download2("http://pdfobject.com/markup/examples/full-browser-window.html", new File("C:\\Users\\Owner\\Desktop\\temporary\\myFile2.pdf"));
}
public static void download(final String url, final File destination) throws IOException {
final URLConnection connection = new URL(url).openConnection();
connection.setConnectTimeout(60000);
connection.setReadTimeout(60000);
connection.addRequestProperty("User-Agent", "Mozilla/5.0");
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final byte[] buffer = new byte[2048];
int read;
final InputStream input = connection.getInputStream();
while((read = input.read(buffer)) > -1)
baos.write(buffer, 0, read);
baos.flush();
Files.write(destination.toPath(), baos.toByteArray(), StandardOpenOption.WRITE);
input.close();
}
public static void download2(final String url, final File destination) throws IOException {
final URLConnection connection = new URL(url).openConnection();
connection.setConnectTimeout(60000);
connection.setReadTimeout(60000);
connection.addRequestProperty("User-Agent", "Mozilla/5.0");
final FileOutputStream output = new FileOutputStream(destination, false);
final byte[] buffer = new byte[2048];
int read;
final InputStream input = connection.getInputStream();
while((read = input.read(buffer)) > -1)
output.write(buffer, 0, read);
output.flush();
output.close();
input.close();
}
}
答案 0 :(得分:3)
您正在下载.html网址,包含引用的PDF作为嵌入对象。与浏览器不同,Java不会处理它,因此您要保存HTML,而不是PDF。看看里面。如需帮助,请点击此处:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Embedding a PDF using static HTML markup: Full-browser window (100% width/height)</title>
<!-- This example created for PDFObject.com by Philip Hutchison (www.pipwerks.com) -->
<style type="text/css">
<!--
html {
height: 100%;
}
body {
margin: 0;
padding: 0;
height: 100%;
}
p {
padding: 1em;
}
object {
display: block;
}
-->
</style>
</head>
<body>
<object data="/pdf/sample.pdf#toolbar=1&navpanes=0&scrollbar=1&page=1&view=FitH"
type="application/pdf"
width="100%"
height="100%">
<p>It appears you don't have a PDF plugin for this browser. No biggie... you can <a href="/pdf/sample.pdf">click here to download the PDF file.</a></p>
</object>
</body>
</html>