问题
I am facing some problem while trying to upload a file on my ftp server with java. here is my code function :
public static void upload_files(String un, String pw, String ip, String f){
try
{
FTPClient client = new FTPClient();
client.connect(ip);
client.login(un,pw);
InputStream is = new FileInputStream(f);
client.storeFile("test2",is);
is.close();
} catch(Exception e) {
e.printStackTrace();
}
}
"f" is the path of the file I want to upload (ie "C:\myfile"). No error during the compilation, one file is well sent to the server, but sadly empty (0 byte).
I also noticed that it takes quite long for a simple upload of some text (around 40s) even thought I got a really good ISP.
I thank in advance all people who will help me.
Regards,
回答1:
I think it could be something to do with the mode of file transfer . Can you set the trandfer mode to Binary. Some times when you sent data in ASCII it goes corrupted . Refer to this https://superuser.com/questions/82726/how-to-set-binary-mode-by-default-when-ftping-to-a-remote-site
It tells how to set binary mode in FTP Command client. You will have a similar provision in FTPClient class also.
Just found I think similar question is answered here FTPClient - Java, upload file
回答2:
It seems like the problem has to do with the implementation of the FTPClient. Are you sure that the class works like that?
Try uploading to a different server or using a different class to find the reason for the failure.
Also: Are you sure that it is your responsibility to close the FileInputStream? I could imagine that the .storeFile(…) method will close it itself, when it is done.
回答3:
Add these lines before client.login ftpClient default protocol is full text only for other files you nead to specifie binnary mode
client.enterLocalPassiveMode()
client.setFileType(FTP.BINARY_FILE_TYPE, FTP.BINARY_FILE_TYPE);
client.setFileTransferMode(FTP.BINARY_FILE_TYPE);
来源:https://stackoverflow.com/questions/11873353/empty-file-after-upload-with-java-on-my-ftp-server