1

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,

Dupond Durand
  • 31
  • 2
  • 7
  • Does the file f exist? You are passing in a string I notice. Also do you have to close the FTPClient. What FTPClient are you using? – RNJ Aug 08 '12 at 21:39
  • @ user846476 : to answer you about the string, yes I think it's the proper way. When I copy this in windows explorer, it opens me the right file so I guess it's the right path. Concerning the second point, how can I close the ftp client ? thanks how do I do this ? I think it's the problem, yes – Dupond Durand Aug 08 '12 at 21:55
  • I tried with "client.disconnect()" but still the same problem. Probably not the good method right ? – Dupond Durand Aug 08 '12 at 21:59

3 Answers3

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

Community
  • 1
  • 1
0

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.

mercutio
  • 1,065
  • 7
  • 18
0

Add these lines before client.login ftpClient default protocol is full text only for other files you need to specify binary mode

client.enterLocalPassiveMode()
client.setFileType(FTP.BINARY_FILE_TYPE, FTP.BINARY_FILE_TYPE);
client.setFileTransferMode(FTP.BINARY_FILE_TYPE);
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
gori
  • 1,164
  • 1
  • 11
  • 14