gpt4 book ai didi

java - 如何从网站下载整个文件

转载 作者:行者123 更新时间:2023-12-02 07:15:55 27 4
gpt4 key购买 nike

我按照这个例子:http://www.mkyong.com/java/how-to-download-file-from-website-java-jsp

File file = new File("path/to/file/test.txt");
FileInputStream fis= new FileInputStream(file);
ServletOutputStream out = response.getOutputStream();

byte[] outputByte = new byte[4096];
//copy binary contect to output stream
while(fis.read(outputByte, 0, 4096) != -1)
{
out.write(outputByte, 0, 4096);
}
fis.close();
out.flush();
out.close();

问题是下载文件仍然不完整。文件末尾仍然缺少一些字符

所以我尝试另一个例子:

File file = new File("path/to/file/test.txt");
FileInputStream fis= new FileInputStream(file);


IOUtils.copy(fis,response.getOutputStream());
fis.close();

文件下载完成。所以我的问题是为什么第一个例子不起作用而第二个例子是正确的

最佳答案

不确定这是否是原因,但你可以更换这个

//copy binary contect to output stream
while(fis.read(outputByte, 0, 4096) != -1)
{
out.write(outputByte, 0, 4096);
}

int length=-1;
while ( (length = fis.read(outputByte, 0, 4096)) != -1) {
out.write(outputByte, 0, length);
}

让我们知道进展如何?

关于java - 如何从网站下载整个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14947413/

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