FTPClient - Java, upload file

68,717

Solution 1

It doesn't work because the default transfer mode for FTPClient is FTP.ASCII_FILE_TYPE. You just need to update the configuration to transfer in binary mode.

Solution 2

Add this to your file

ftp.setFileType(FTP.BINARY_FILE_TYPE, FTP.BINARY_FILE_TYPE);
ftp.setFileTransferMode(FTP.BINARY_FILE_TYPE);

I had the same problem with xlsx files and this was a good solution.

Solution 3

It's often forgotten that FTP has two modes of operation - one for text files and the other for binary (image) files. In the good old days, connecting from a command line ftp client, we'd carefully remember to set the transfer mode before requesting a file - or we'd run into exactly the sort of problem you seem to be having. Today a lot of situations seem to default to binary, but not apparently yours.

You probably need to tell your ftp implementation to transfer in binary/image mode.

Solution 4

Try to use BufferedInputStream, this is a (working) code sample:

BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f));
res = client.storeFile("File Name", bis);
bis.close();
client.logout();

Solution 5

From documentation

This method does NOT close the given InputStream.

So close the FileInputStream before calling logout()

Share:
68,717

Related videos on Youtube

CodeGuy
Author by

CodeGuy

Updated on May 21, 2020

Comments

  • CodeGuy
    CodeGuy almost 4 years

    I'm trying to do a VERY simple file upload. I want a Java FTPClient that can upload any file I tell it to. But the pdf always gets all messed up and my pdf editor (Adobe) won't open it, saying there is an I/O error.

    I'm using the following class:

        import org.apache.commons.net.ftp.FTPClient;
        ....
    
        FTPClient client = new FTPClient();
        FileInputStream fis = null;
    
        try {
            client.connect("mydomain.com");
            client.login("user", "password");
    
            String filename = "myPDF.pdf";
            fis = new FileInputStream(filename);
    
            client.storeFile("temp.pdf", fis);
            fis.close();
            client.logout();
        } catch (IOException e) {
            e.printStackTrace();
        }
    

    Why doesn't this work, and how do I fix it?

    • Aleadam
      Aleadam almost 13 years
      Did you try the answers in your question from half an hour ago? stackoverflow.com/questions/5925438/…
    • CodeGuy
      CodeGuy almost 13 years
      yup, I'm using an FTPClient now. The question is completely different.
  • MByD
    MByD almost 13 years
    Two short questions - have you tried to open the file on the client machine? have you tried sending a different file?
  • Bala R
    Bala R almost 13 years
    @reising1 I'm not sure then, Did you try @MByd's suggestion ?
  • CodeGuy
    CodeGuy almost 13 years
    yes, other files are fine. and what do you mean open it on the client machine? basically, after I upload it, I download it to my local machine. it's an ftp on a website I own.
  • MByD
    MByD almost 13 years
    did you have any success opening the file before uploading it? maybe you have a problem in the download mechanism? --I'm not sure about those, just trying to help :S
  • CodeGuy
    CodeGuy almost 13 years
    yeah. the original file is just fine. ugh I'm not sure why it doesn't work. It works fine for other files I've tried like txt files. something special about pdfs.
  • Peter Becker
    Peter Becker about 11 years
    I don't think you want to call setFileTransferMode like that -- according to the JavaDoc it's meant to take only the FTP.*_TRANSFER_MODE constants.
  • mk.hd
    mk.hd about 9 years
    Your comment just solved a two year problem I was having. Cheers to you.
  • amdev
    amdev almost 8 years
    -1, this answer is wrong and result in wasting my time. Only ftp.setFileType(FTP.BINARY_FILE_TYPE); is required, il you put the 2nd param ftp.setFileType(FTP.BINARY_FILE_TYPE, FTP.BINARY_FILE_TYPE) the file il corrupted ( with pdf and xls )
  • DLight
    DLight over 7 years
    To transfer in binary mode: ftp.setFileType(FTP.BINARY_FILE_TYPE)
  • sigi
    sigi over 6 years
    Has something changed at FTPClient? Because for me it is FTPClient.BINARY_FILE_TYPE.