FTPClient - Java, upload file

后端 未结 8 933
遇见更好的自我
遇见更好的自我 2020-12-08 19:58

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) w

相关标签:
8条回答
  • 2020-12-08 20:24

    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.

    0 讨论(0)
  • 2020-12-08 20:32

    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.

    0 讨论(0)
  • 2020-12-08 20:32

    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.

    0 讨论(0)
  • 2020-12-08 20:34

    From documentation

    This method does NOT close the given InputStream.

    So close the FileInputStream before calling logout()

    0 讨论(0)
  • 2020-12-08 20:35

    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.

    0 讨论(0)
  • 2020-12-08 20:46

    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.

    0 讨论(0)
提交回复
热议问题