FTPClient - Java, upload file
Asked Answered
A

8

27

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?

Amorita answered 8/5, 2011 at 4:21 Comment(2)
Did you try the answers in your question from half an hour ago? #5925938Cryptozoite
yup, I'm using an FTPClient now. The question is completely different.Amorita
D
32

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.

Diadiabase answered 29/8, 2011 at 19:45 Comment(1)
To transfer in binary mode: ftp.setFileType(FTP.BINARY_FILE_TYPE)Martyr
T
24

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.

Tiflis answered 15/11, 2011 at 8:58 Comment(3)
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.Braunite
-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 )Dimpledimwit
Has something changed at FTPClient? Because for me it is FTPClient.BINARY_FILE_TYPE.Kowalski
S
10

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.

Suited answered 8/5, 2011 at 5:52 Comment(1)
Your comment just solved a two year problem I was having. Cheers to you.Herold
C
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();
Comines answered 8/5, 2011 at 4:26 Comment(4)
Two short questions - have you tried to open the file on the client machine? have you tried sending a different file?Comines
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.Amorita
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 :SComines
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.Amorita
A
2

From documentation

This method does NOT close the given InputStream.

So close the FileInputStream before calling logout()

Applause answered 8/5, 2011 at 4:27 Comment(1)
@reising1 I'm not sure then, Did you try @MByd's suggestion ?Applause
A
1

Try this.

objFtpClient.setFileType(FTP.BINARY_FILE_TYPE);

objFtpClient.setFileTransferMode(FTP.BINARY_FILE_TYPE);

objFtpClient.enterLocalPassiveMode();
Aureaaureate answered 14/10, 2013 at 13:43 Comment(0)
K
1

For Me only ftp.setFileType(FTP.BINARY_FILE_TYPE, FTP.BINARY_FILE_TYPE) worked, while when I was using ftp.setFileTransferMode(FTP.BINARY_FILE_TYPE) File was getting corrupt.

Kasi answered 12/10, 2014 at 14:52 Comment(0)
S
0

This looks like a bug in the Commons NET library, which affected version 3.0. Try a newer version (3.0.1), which fixed the bug.

Sterilize answered 17/7, 2011 at 1:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.