我已经在这个论坛上查看了所有类似的questions(不仅仅是这个!),并尝试了所有这些方法,但仍然无法以编程方式下载测试文件:http://pdfobject.com/markup/examples/full-browser-window.html
以下是我试图下载的测试文件的direct link.这是一个具有开放访问权限的测试pdf文件,因此任何人都可以使用它来测试下载方法.
如何下载此特定文件以使其具有pdf扩展名?
解决方法:
要下载文件,也许您可以尝试这样的事情:
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
public final class FileDownloader {
private FileDownloader(){}
public static void main(String args[]) throws IOException{
download("http://pdfobject.com/pdf/sample.pdf", new File("sample.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 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();
}
}