检查 FTPClient 下载是否已收到所有字节
我使用 FTPClient
从 FTP 服务器
下载图像文件。我的外观如下:
public class FtpDownloadDemo
{
public static void main(String[] args)
{
FTPClient client = new FTPClient();
FileOutputStream fos = null;
try
{
client.connect("ftp.domain.com");
client.login("user", "pass");
//
// The remote filename to be downloaded.
//
String filename = "side.jpg";
fos = new FileOutputStream(filename);
//
// Download file from FTP server
//
client.retrieveFile("/" + filename, fos);
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if (fos != null)
{
fos.close();
}
client.disconnect();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
下载文件后,输出看起来扭曲,如下所示;
有没有办法检查是否已收到所有字节,否则要等到所有字节都收到为止?
I download an image file from an FTP Server
using FTPClient
. My look like this:
public class FtpDownloadDemo
{
public static void main(String[] args)
{
FTPClient client = new FTPClient();
FileOutputStream fos = null;
try
{
client.connect("ftp.domain.com");
client.login("user", "pass");
//
// The remote filename to be downloaded.
//
String filename = "side.jpg";
fos = new FileOutputStream(filename);
//
// Download file from FTP server
//
client.retrieveFile("/" + filename, fos);
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if (fos != null)
{
fos.close();
}
client.disconnect();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
When the file is downloaded the output looks distorted as shown below;
Is there a way to check if all the bytes has been received else wait till all is received?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我强烈怀疑真正的问题是传输是以 ASCII 模式而不是二进制模式完成的。
尝试调用
client.setFileType(FTP.BINARY_FILE_TYPE);
在启动传输之前。I strongly suspect that the real problem is that the transfer is done in ASCII, rather than binary, mode.
Try calling
client.setFileType(FTP.BINARY_FILE_TYPE);
before initiating the transfer.