FTPClient - Java, upload file

2019-01-17 11:15发布

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?

8条回答
神经病院院长
2楼-- · 2019-01-17 11:29

Try this.

objFtpClient.setFileType(FTP.BINARY_FILE_TYPE);

objFtpClient.setFileTransferMode(FTP.BINARY_FILE_TYPE);

objFtpClient.enterLocalPassiveMode();
查看更多
何必那么认真
3楼-- · 2019-01-17 11:30

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();
查看更多
兄弟一词,经得起流年.
4楼-- · 2019-01-17 11:35

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.

查看更多
等我变得足够好
5楼-- · 2019-01-17 11:36

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.

查看更多
放我归山
6楼-- · 2019-01-17 11:36

From documentation

This method does NOT close the given InputStream.

So close the FileInputStream before calling logout()

查看更多
相关推荐>>
7楼-- · 2019-01-17 11:37

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.

查看更多
登录 后发表回答