2

我在将文件上传到 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 上传的图像,当我打开它时,操作系统会说Unable to preview the image, probabily it's corrupted or it's too big.

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

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

4

1 回答 1

9

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

于 2013-05-04T18:28:24.167 回答