写一个根据pdfurl,把pdf下载到本地的java代码
时间: 2024-01-23 10:03:42 浏览: 154
可以参考以下代码,使用Java中的URLConnection类来实现:
import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
public class PdfDownloader {
public static void main(String[] args) {
String pdfUrl = "https://example.com/example.pdf"; // 替换为具体的PDF文件链接
String pdfSavePath = "C:\\example.pdf"; // 替换为具体的本地保存路径
try {
URL url = new URL(pdfUrl);
URLConnection connection = url.openConnection();
BufferedInputStream bis = new BufferedInputStream(connection.getInputStream());
FileOutputStream fos = new FileOutputStream(pdfSavePath);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = bis.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
bis.close();
fos.close();
System.out.println("PDF文件下载成功!");
} catch (IOException e) {
e.printStackTrace();
System.out.println("PDF文件下载失败!");
}
}
}