gpt4 book ai didi

Java IO 传输问题

转载 作者:太空宇宙 更新时间:2023-11-04 07:40:48 24 4
gpt4 key购买 nike

创建 FileUtils 类以重用所有 IO。我们通过共享将文件(随机数量)从一台 Unix 服务器传输到另一台。

我的问题:随机有一个文件会以 0kb 文件的形式出现,但无一异常(exception),并表示传输成功。

我们使用的基本方法:

resultCode = 0;
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(fileLocation + orginalFile);
fos = new FileOutputStream(toFolder + destinationFile);
byte[] buf = new byte[1024];
int i = 0;
while ((i = fis.read(buf)) != -1) {
fos.write(buf, 0, i);
}

resultCode = 1;
} catch (Exception e) {
resultCode = 2;
//our logging

} finally {
try {
fis.close();
fos.close();
} catch (Exception e) {
//our logging
}
}

return resultCode;
}

对于造成这种情况的原因有什么想法吗?每次传输单个文件时都会调用 FileUtils 类。

谢谢

最佳答案

关闭之前刷新文件输出流 fos.flush() ,我不确定它是否能解决您的问题,但值得一试:

public void flush() throws IOException

Flushes this output stream and forces any buffered output bytes to be written out. The general contract of flush is that calling it is an indication that, if any bytes previously written have been buffered by the implementation of the output stream, such bytes should immediately be written to their intended destination.

关于Java IO 传输问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16100384/

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