如何从Java中的给定URL下载PDF?

2022-09-02 12:22:53

我想创建一个Java应用程序,在执行时从URL下载文件。有什么功能可以使用来执行此操作吗?

这段代码仅适用于文件:.txt

URL url= new URL("http://cgi.di.uoa.gr/~std10108/a.txt");
BufferedReader in = new BufferedReader(
new InputStreamReader(url.openStream()));
PrintWriter writer = new PrintWriter("file.txt", "UTF-8");

String inputLine;
while ((inputLine = in.readLine()) != null){
   writer.write(inputLine+ System.getProperty( "line.separator" ));               
   System.out.println(inputLine);
}
writer.close();
in.close();

答案 1

不要在这里使用阅读器和作家,因为它们旨在处理PDF不是的原始文本文件(因为它还包含许多其他信息,如有关字体的信息,甚至图像)。请改用流来复制所有原始字节

所以使用类打开连接。然后只需从其输入流中读取并将原始字节写入文件即可。URL

(这是简化的示例,您仍然需要处理异常并确保在正确的位置关闭流)

System.out.println("opening connection");
URL url = new URL("https://upload.wikimedia.org/wikipedia/en/8/87/Example.JPG");
InputStream in = url.openStream();
FileOutputStream fos = new FileOutputStream(new File("yourFile.jpg"));

System.out.println("reading from resource and writing to file...");
int length = -1;
byte[] buffer = new byte[1024];// buffer for portion of data from connection
while ((length = in.read(buffer)) > -1) {
    fos.write(buffer, 0, length);
}
fos.close();
in.close();
System.out.println("File downloaded");

从Java 7开始,我们还可以使用和try-with-with-resources来自动关闭InputStream(在这种情况不必手动关闭):Files.copy

URL url = new URL("https://upload.wikimedia.org/wikipedia/en/8/87/Example.JPG");
try (InputStream in = url.openStream()) {
   Files.copy(in, Paths.get("someFile.jpg"), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
   // handle exception
}

答案 2