从网上下载后 pdf 中的空白页
Blank pages in pdf after downloading it from web
我正在尝试使用 HttpClient 下载 PDF 文件,它正在下载 PDF 文件但页面是空白的。如果我打印它们,我可以从响应中看到控制台上的字节。但是当我尝试将它写入文件时,它生成了一个空白文件。
FileUtils.writeByteArrayToFile(new File(outputFilePath), bytes);
然而,文件显示的大小正确,如预期的那样为 103KB 和 297KB,但它只是空白!!
我也尝试过输出流:
FileOutputStream fileOutputStream = new FileOutputStream(outFile);
fileOutputStream.write(bytes);
还尝试使用 UTF-8 编码编写,例如:
Writer out = new BufferedWriter( new OutputStreamWriter(
new FileOutputStream(outFile), "UTF-8"));
String str = new String(bytes, StandardCharsets.UTF_8);
try {
out.write(str);
} finally {
out.close();
}
没有什么对我有用。非常感谢任何建议..
更新:我正在使用 DefaultHttpClient。
HttpGet httpget = new HttpGet(targetURI);
HttpResponse response = null;
String htmlContents = null;
try {
httpget = new HttpGet(url);
response = httpclient.execute(httpget);
InputStreamReader dataStream=new InputStreamReader(response.getEntity().getContent());
byte[] bytes = IOUtils.toByteArray(dataStream);
...
这是我用来从特定 URL 下载 PDF 文件的方法。该方法需要两个字符串参数,一个 url 字符串(示例:"https://www.ibm.com/support/knowledgecenter/SSWRCJ_4.1.0/com.ibm.safos.doc_4.1/Planning_and_Installation.pdf"
)和一个用于下载 PDF 文件(或其他文件)的目标文件夹路径。如果本地文件系统中不存在目标路径,则会自动创建它:
public boolean downloadFile(String urlString, String destinationFolderPath) {
boolean result = false; // will turn to true if download is successful
if (!destinationFolderPath.endsWith("/") && !destinationFolderPath.endsWith("\")) {
destinationFolderPath+= "/";
}
// If the destination path does not exist then create it.
File foldersToMake = new File(destinationFolderPath);
if (!foldersToMake.exists()) {
foldersToMake.mkdirs();
}
try {
// Open Connection
URL url = new URL(urlString);
// Get just the file Name from URL
String fileName = new File(url.getPath()).getName();
// Try with Resources....
try (InputStream in = url.openStream(); FileOutputStream outStream =
new FileOutputStream(new File(destinationFolderPath + fileName))) {
// Read from resource and write to file...
int length = -1;
byte[] buffer = new byte[1024]; // buffer for portion of data from connection
while ((length = in.read(buffer)) > -1) {
outStream.write(buffer, 0, length);
}
}
// File Successfully Downloaded");
result = true;
}
catch (MalformedURLException ex) { ex.printStackTrace(); }
catch (IOException ex) { ex.printStackTrace(); }
return result;
}
你会
InputStreamReader dataStream=new InputStreamReader(response.getEntity().getContent());
byte[] bytes = IOUtils.toByteArray(dataStream);
正如评论中已经提到的,使用 Reader
class 会损坏二进制数据,例如PDF文件。因此,您不应将内容包装在 InputStreamReader
.
中
由于您的内容可用于构建 InputStreamReader
,不过,我假设 response.getEntity().getContent()
returns 和 InputStream
。这样的 InputStream
通常可以直接用作 IOUtils.toByteArray
参数。
所以:
InputStream dataStream=response.getEntity().getContent();
byte[] bytes = IOUtils.toByteArray(dataStream);
应该已经适合你了!
我正在尝试使用 HttpClient 下载 PDF 文件,它正在下载 PDF 文件但页面是空白的。如果我打印它们,我可以从响应中看到控制台上的字节。但是当我尝试将它写入文件时,它生成了一个空白文件。
FileUtils.writeByteArrayToFile(new File(outputFilePath), bytes);
然而,文件显示的大小正确,如预期的那样为 103KB 和 297KB,但它只是空白!!
我也尝试过输出流:
FileOutputStream fileOutputStream = new FileOutputStream(outFile);
fileOutputStream.write(bytes);
还尝试使用 UTF-8 编码编写,例如:
Writer out = new BufferedWriter( new OutputStreamWriter(
new FileOutputStream(outFile), "UTF-8"));
String str = new String(bytes, StandardCharsets.UTF_8);
try {
out.write(str);
} finally {
out.close();
}
没有什么对我有用。非常感谢任何建议..
更新:我正在使用 DefaultHttpClient。
HttpGet httpget = new HttpGet(targetURI);
HttpResponse response = null;
String htmlContents = null;
try {
httpget = new HttpGet(url);
response = httpclient.execute(httpget);
InputStreamReader dataStream=new InputStreamReader(response.getEntity().getContent());
byte[] bytes = IOUtils.toByteArray(dataStream);
...
这是我用来从特定 URL 下载 PDF 文件的方法。该方法需要两个字符串参数,一个 url 字符串(示例:"https://www.ibm.com/support/knowledgecenter/SSWRCJ_4.1.0/com.ibm.safos.doc_4.1/Planning_and_Installation.pdf"
)和一个用于下载 PDF 文件(或其他文件)的目标文件夹路径。如果本地文件系统中不存在目标路径,则会自动创建它:
public boolean downloadFile(String urlString, String destinationFolderPath) {
boolean result = false; // will turn to true if download is successful
if (!destinationFolderPath.endsWith("/") && !destinationFolderPath.endsWith("\")) {
destinationFolderPath+= "/";
}
// If the destination path does not exist then create it.
File foldersToMake = new File(destinationFolderPath);
if (!foldersToMake.exists()) {
foldersToMake.mkdirs();
}
try {
// Open Connection
URL url = new URL(urlString);
// Get just the file Name from URL
String fileName = new File(url.getPath()).getName();
// Try with Resources....
try (InputStream in = url.openStream(); FileOutputStream outStream =
new FileOutputStream(new File(destinationFolderPath + fileName))) {
// Read from resource and write to file...
int length = -1;
byte[] buffer = new byte[1024]; // buffer for portion of data from connection
while ((length = in.read(buffer)) > -1) {
outStream.write(buffer, 0, length);
}
}
// File Successfully Downloaded");
result = true;
}
catch (MalformedURLException ex) { ex.printStackTrace(); }
catch (IOException ex) { ex.printStackTrace(); }
return result;
}
你会
InputStreamReader dataStream=new InputStreamReader(response.getEntity().getContent());
byte[] bytes = IOUtils.toByteArray(dataStream);
正如评论中已经提到的,使用 Reader
class 会损坏二进制数据,例如PDF文件。因此,您不应将内容包装在 InputStreamReader
.
由于您的内容可用于构建 InputStreamReader
,不过,我假设 response.getEntity().getContent()
returns 和 InputStream
。这样的 InputStream
通常可以直接用作 IOUtils.toByteArray
参数。
所以:
InputStream dataStream=response.getEntity().getContent();
byte[] bytes = IOUtils.toByteArray(dataStream);
应该已经适合你了!