Java 根据pdf下载地址下载pdf到本地
时间: 2023-07-28 10:54:10 浏览: 58
你可以使用Java的URLConnection和InputStream来下载PDF文件。以下是示例代码:
```java
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
public class PdfDownloader {
public static void main(String[] args) throws Exception {
String pdfUrl = "http://example.com/example.pdf"; // 替换成实际的PDF下载地址
URL url = new URL(pdfUrl);
URLConnection conn = url.openConnection();
InputStream inStream = conn.getInputStream();
FileOutputStream outStream = new FileOutputStream("example.pdf"); // 替换成实际的文件名
byte[] buffer = new byte[4096];
int bytesRead = -1;
while ((bytesRead = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, bytesRead);
}
outStream.close();
inStream.close();
}
}
```
以上代码会将PDF文件下载到当前目录下,并保存为`example.pdf`文件。你可以根据需要修改PDF下载地址和保存的文件名。