gpt4 book ai didi

java - 为什么下载的 pdf 文件已损坏?

转载 作者:行者123 更新时间:2023-12-01 15:11:23 24 4
gpt4 key购买 nike

我编写了一个小程序,用于通过 url 下载文件。我可以正确打开所有其他文件格式,但对于下载的 pdf 则不可能。

public static void saveFile(String fileUrl, String destinationFile) throws IOException {
URL url = new URL(fileUrl);
InputStream is = url.openStream();
OutputStream os = new FileOutputStream(destinationFile);

byte[] b = new byte[2048];
int length;

while ((length = is.read(b)) != -1) {
os.write(b, 0, length);
}
os.flush();
is.close();
os.close();
}

我需要特殊的方式来处理 pdf 下载吗?

当我尝试通过浏览器中的 URL 获取选定的 pdf 时,它会正确显示

编辑

在代码中添加了flush(),仍然没有成功

尝试在浏览器 (FF) 中打开损坏的 pdf 会返回错误:

File does not begin with '%PDF-'

Adobe Reader 返回:

File could not be open because it is either not a supported file type or because file has been damaged.

损坏 pdf 的尺寸比原始文件小(约 80%)

最佳答案

损坏的pdf文件内有网站html代码。

关于java - 为什么下载的 pdf 文件已损坏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12304004/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com