Commons-Net FTPClient 无法正确下载文件

发布于 2024-11-24 00:07:26 字数 874 浏览 5 评论 0原文

解决

了事实证明我误解了 available() 方法的实际作用,所以我只是将写入数据的代码更改为:

int b = input.read();
while (b != -1) {
    output.write(b);
    b = input.read();
}

我将再次研究更有效的读/写方法,现在我很高兴我已经它工作了。感谢您的帮助!

原始帖子

我正在使用 Commons-Net 库通过 FTP 与服务器交互,当我尝试将文件从服务器下载到本地计算机时遇到问题。我不知道这是否是 Comments-Net.FTPClient 中的错误,但更有可能是我没有正确使用该库。也就是说,当我用谷歌搜索问题时,我一直无法找到解决方案,并且我看到的示例代码似乎使用与我相同的方法来下载文件。

无论如何,实际的问题是,某些下载的文件本应包含数据,但完全是空的。它是随机发生的,所以每次我下载所有文件时,每次都会有不同的文件最终为空。我假设我从服务器读取数据或将其写入文件的方式存在问题。

代码如下:

// For each file
InputStream input = ftp.retrieveFileStream(sourcePath);
ftp.completePendingCommand();

OutputStream output = new FileOutputStream(new File(destinationPath));
while (input.available() > 0) {
    output.write(input.read());
}

input.close();
output.close();

我对网络还很陌生,所以我只是愚蠢地做了一些非常简单的错误吗?

Solved

It turned out I misunderstood what the available() method actually does, so I just altered the code that wrote the data to this:

int b = input.read();
while (b != -1) {
    output.write(b);
    b = input.read();
}

I'll work on a more efficient reading/writing method another time, for now I'm just glad I've it working. Thanks for the help!

Original Post

I'm using the Commons-Net library to interact with a server via FTP and I've come across a problem when I try downloading files from the server to the local machine. I don't know if this is a bug in Comments-Net.FTPClient, but it's infinitely more likely I'm just not using the library correctly. That said, I haven't been able to find a solution when I googled the problem and example code I have seen appears to use the same method for downloading files as I do.

Anyway, the actual problem is that some of the files downloaded are completely empty, when they should contain data. It happens at random, so every time I download all the files, different files end up being empty each time. I'm assuming there is a problem with the way I'm reading the data from the server or writing it to a file.

Here's the code:

// For each file
InputStream input = ftp.retrieveFileStream(sourcePath);
ftp.completePendingCommand();

OutputStream output = new FileOutputStream(new File(destinationPath));
while (input.available() > 0) {
    output.write(input.read());
}

input.close();
output.close();

I'm pretty new to networking, so am I just being silly and doing something really simple wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

記憶穿過時間隧道 2024-12-01 00:07:26

您不应该使用 input.available() 因为它只是告诉您有多少输入可用而不阻塞。循环直到文件末尾(即直到 read() 返回 -1)。

http://download.oracle.com/javase /6/docs/api/java/io/InputStream.html#read()

如果效率是一个问题,您也可以尝试一次读取多个字节。

You shouldn't be using input.available() as that just tells you how much input is available without blocking. Loop until end of file instead (i.e. until read() returns -1).

http://download.oracle.com/javase/6/docs/api/java/io/InputStream.html#read()

If efficiency is a concern you could also try reading more than one byte at a time.

绝影如岚 2024-12-01 00:07:26

使用 IOUtilsFileUtils 查看 commons-io 以实现更简单的流处理

Check out commons-io with IOUtils and FileUtils for simpler stream processing

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文