使用 java 下载 pdf
download a pdf using java
我正在编写一个从服务器下载 PDF 文件的程序。我正在使用此处给出的一些程序 Download file by passing URL using java code,此解决方案适用于第一个答案中提供的示例 URL,但不适用于 PDF,我仅替换 URL。下面是我的代码。
import java.io.*;
import java.net.*;
public class FileDownloadTest {
final static int size = 1024;
public static void fileUrl(String fAddress, String localFileName, String destinationDir) {
// localFileName = "Hello World";
OutputStream outStream = null;
URLConnection uCon = null;
InputStream is = null;
try {
URL url;
byte[] buf;
int byteRead, byteWritten = 0;
url = new URL(fAddress);
outStream = new BufferedOutputStream(new FileOutputStream(destinationDir + "\" + localFileName));
uCon = url.openConnection();
is = uCon.getInputStream();
buf = new byte[size];
while ((byteRead = is.read(buf)) != -1) {
outStream.write(buf, 0, byteRead);
byteWritten += byteRead;
}
System.out.println("Downloaded Successfully.");
System.out.println("File name:\"" + localFileName + "\"\nNo ofbytes :" + byteWritten);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
is.close();
outStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void fileDownload(String fAddress, String destinationDir) {
int slashIndex = fAddress.lastIndexOf('/');
int periodIndex = fAddress.lastIndexOf('.');
String fileName = fAddress.substring(slashIndex + 1);
if (periodIndex >= 1 && slashIndex >= 0 && slashIndex < fAddress.length() - 1) {
fileUrl(fAddress, fileName, destinationDir);
} else {
System.err.println("path or file name.");
}
}
public static void main(String[] args) {
String fAddress = "http://singztechmusings.files.wordpress.com/2011/09/maven_eclipse_and_osgi_working_together.pdf";
String destinationDir = "D:\FileDownload";
fileDownload(fAddress, destinationDir);
}
}
这里,这个pdf有73页,在我的文件夹里,下载为1KB的PDF,用Acrobat打开时Reader,提示文件可能已损坏
我也试过这里提供的代码https://dzone.com/articles/java-how-save-download-file,但结果是一样的。
请告诉我如何解决这个问题。
谢谢
如果查看下载的文件内容,可以看到是html。服务器正在将原始请求重定向到 https url。使用 url https://singztechmusings.files.wordpress.com/2011/09/maven_eclipse_and_osgi_working_together.pdf 代替。
或者使用具有自动重定向处理功能的 http 客户端,ala http-commons
您定义一个变量 size = 1024
并使用它来定义您的缓冲区。
所以从逻辑上讲,您只能向其中写入 1 KB。
但是如果输入 Stream 一次读取更多,它将丢失......因此将您的缓冲区大小更改为能够包含大多数文档的值或尝试确定必要的大小
我正在编写一个从服务器下载 PDF 文件的程序。我正在使用此处给出的一些程序 Download file by passing URL using java code,此解决方案适用于第一个答案中提供的示例 URL,但不适用于 PDF,我仅替换 URL。下面是我的代码。
import java.io.*;
import java.net.*;
public class FileDownloadTest {
final static int size = 1024;
public static void fileUrl(String fAddress, String localFileName, String destinationDir) {
// localFileName = "Hello World";
OutputStream outStream = null;
URLConnection uCon = null;
InputStream is = null;
try {
URL url;
byte[] buf;
int byteRead, byteWritten = 0;
url = new URL(fAddress);
outStream = new BufferedOutputStream(new FileOutputStream(destinationDir + "\" + localFileName));
uCon = url.openConnection();
is = uCon.getInputStream();
buf = new byte[size];
while ((byteRead = is.read(buf)) != -1) {
outStream.write(buf, 0, byteRead);
byteWritten += byteRead;
}
System.out.println("Downloaded Successfully.");
System.out.println("File name:\"" + localFileName + "\"\nNo ofbytes :" + byteWritten);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
is.close();
outStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void fileDownload(String fAddress, String destinationDir) {
int slashIndex = fAddress.lastIndexOf('/');
int periodIndex = fAddress.lastIndexOf('.');
String fileName = fAddress.substring(slashIndex + 1);
if (periodIndex >= 1 && slashIndex >= 0 && slashIndex < fAddress.length() - 1) {
fileUrl(fAddress, fileName, destinationDir);
} else {
System.err.println("path or file name.");
}
}
public static void main(String[] args) {
String fAddress = "http://singztechmusings.files.wordpress.com/2011/09/maven_eclipse_and_osgi_working_together.pdf";
String destinationDir = "D:\FileDownload";
fileDownload(fAddress, destinationDir);
}
}
这里,这个pdf有73页,在我的文件夹里,下载为1KB的PDF,用Acrobat打开时Reader,提示文件可能已损坏
我也试过这里提供的代码https://dzone.com/articles/java-how-save-download-file,但结果是一样的。
请告诉我如何解决这个问题。
谢谢
如果查看下载的文件内容,可以看到是html。服务器正在将原始请求重定向到 https url。使用 url https://singztechmusings.files.wordpress.com/2011/09/maven_eclipse_and_osgi_working_together.pdf 代替。
或者使用具有自动重定向处理功能的 http 客户端,ala http-commons
您定义一个变量 size = 1024
并使用它来定义您的缓冲区。
所以从逻辑上讲,您只能向其中写入 1 KB。
但是如果输入 Stream 一次读取更多,它将丢失......因此将您的缓冲区大小更改为能够包含大多数文档的值或尝试确定必要的大小