Question

I'm having some problems with uploading a file to a FTP server.

I wrote this code that should connect to a FTP server, login, and upload a file using 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());

The output to the terminal is:

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

The problem is that if i go to my user.dir and open current_690.jpg it displays me the image correctly, but if i download the image i just uploaded with my FTPClient, when i open it, the os says Unable to preview the image, probabily it's corrupted or it's too big.

In fact i noticed that on my pc my image size is 32708 bytes, but on the server it shows me 32615 bytes, so i think that the last part of the image is not being uploaded.

Why? Am i missing something?

Était-ce utile?

La solution

The default file type is ASCII so you need to tell it to tranfer it as binary, by setting client.setFileType(FTPClient.BINARY_FILE_TYPE); before your storefile call.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top