【问题标题】:Strange FTPClient.storeFile behaviour奇怪的 FTPClient.storeFile 行为
【发布时间】:2013-04-28 22:07:58
【问题描述】:

我在将文件上传到 FTP 服务器时遇到了一些问题。

我编写的这段代码应该连接到 FTP 服务器、登录并使用 Apache Commons Net FTPClient 上传文件:

FTPClient client = new FTPClient();
client.connect("somesite.com");
client.login("user", "password");
System.out.println("connected");

client.cwd("images/bar");
System.out.println("cwd succesful. Full reply: "+client.getReplyString());

FileInputStream fis = new FileInputStream(new File(System.getProperty("user.dir")+File.separator+"current_690.jpg"));
client.storeFile(image_name, fis);
System.out.println("Succesful store. Full reply: "+client.getReplyString());

到终端的输出是:

connected
cwd succesful. Full reply: 250 OK. Current directory is /images/bar

Succesful store. Full reply: 226-File successfully transferred
226 3.190 seconds (measured here), 9.99 Kbytes per second

问题是,如果我去我的user.dir 并打开current_690.jpg,它会正确显示图像,但是如果我下载我刚刚用我的 FTPClient 上传的图像,当我打开它时,操作系统会显示@987654325 @

事实上,我注意到在我的电脑上我的图像大小是32708 字节,但在服务器上它显示我32615 字节,所以我认为图像的最后一部分没有被上传。

为什么?我错过了什么吗?

【问题讨论】:

    标签: java ftp apache-commons-net


    【解决方案1】:

    默认文件类型是 ASCII,所以你需要通过设置client.setFileType(FTPClient.BINARY_FILE_TYPE); 告诉它以二进制形式传输它 在您的 storefile 调用之前。

    【讨论】:

    • 这个解决方案对我有用。我的问题是额外的字节被添加到上传的文件中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-08
    • 2015-07-20
    • 2010-10-03
    • 2021-07-12
    • 2013-10-04
    • 2019-01-23
    相关资源
    最近更新 更多