2

I have few excel files locally on my machine. I want to upload this excel file to ftp. I m using apache commons FTPClinet. But when it uploads the file, it is corrupted and size of the is zero. Here is the sample program

Can anyone point me where am i doing wrong?

public class App {
    private static final String server = "localhost";
    private static final String username = "test";
    private static final String password = "test";
    private static final String directory = "/home/files";

    public static void main(String[] args) throws SocketException, IOException {
        FTPClient f = new FTPClient();
        f.connect(server);
        f.login(username, password);
        f.setFileType(FTPClient.BINARY_FILE_TYPE);
        InputStream is = null;

        is = new FileInputStream("c:\\tmp\\output.xls");
        Boolean isStored = f.storeFile("status.xls", is);
        is.close();
    }
}
Pool
  • 11,999
  • 14
  • 68
  • 78
user509755
  • 2,941
  • 10
  • 48
  • 82

1 Answers1

0

Don't use the f.setFileType(FTPClient.BINARY_FILE_TYPE); Remove that line.

Excel files are not binary, they should be transferred as ascii, which is the default for apache commons FTPClient I think.

I have't tested this. Give it a try.

Also change is from InputStream to FileInputStream

roymustang86
  • 8,054
  • 22
  • 70
  • 101
  • No Success my friend. This time i didnt see the unreadable content but I still see complete data in the copied file. – user509755 Mar 01 '12 at 23:25
  • just making sure, in your code, the server name is not localhost, right? – roymustang86 Mar 02 '12 at 13:56
  • and if it s linux server you are trying to upload it to, make sure the folder has sufficient privileges (777) – roymustang86 Mar 02 '12 at 14:20
  • correct,i m using actual host name. And answer to roymustang86, i gave this persmission, so it is allowing to create file but it is corrupted – user509755 Mar 02 '12 at 19:49
  • is it because excel file was created in windows os and then it copied via ftp client to linux, it is getting corrupted? – user509755 Mar 02 '12 at 20:11
  • No, I don't think that is the cause. Try sending a normal .txt file via FTPClient and see if that gets corrupted too. Unlike xls file, you can do a `more test.txt` to check the contents to the file. If the text file is not getting corrupted, we can rule out that FTPClient is not responsible for the corruption. – roymustang86 Mar 02 '12 at 21:13
  • Text files are working fine, no issues. It is csv and xls files are causing me an issue. Tried all possible solution recommended above, but no success. – user509755 Mar 03 '12 at 15:27