Transfer raw binary with apache commons-net FTPClient?

为君一笑 提交于 2019-11-29 05:27:02
Sven

After login to the ftp server

ftp.setFileType(FTP.BINARY_FILE_TYPE);

The line below doesn't solve it:

//ftp.setFileTransferMode(org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE);

It sounds to me as if your application code might have got the selection of ASCII and BINARY mode inverted. ASCII is coming through unchanged, BINARY performing end-of-line character translations is the exact opposite of how FTP is supposed to work.

If that is not the problem, please edit your question to add the relevant part of your code.

EDIT

A couple of other possible (but IMO unlikely) explanations:

  • The FTP server is broken / misconfigured. (Can you successfully download the file in ASCII / BINARY mode using a non-Java command-line FTP utility?)
  • You are talking to the FTP server via a proxy that is broken or misconfigured.
  • You've somehow managed to get hold of a dodgy (hacked) copy of the Apache FTP client JAR file. (Yea, yea, very unlikely ...)

I found that Apache retrieveFile(...) sometimes did not work with File Sizes exceeding a certain limit. To overcome that I would used retrieveFileStream() instead. Prior to download I have set the Correct FileType and set the Mode to PassiveMode

So the code will look like

    ....
    ftpClientConnection.setFileType(FTP.BINARY_FILE_TYPE);
    ftpClientConnection.enterLocalPassiveMode();
    ftpClientConnection.setAutodetectUTF8(true);

    //Create an InputStream to the File Data and use FileOutputStream to write it
    InputStream inputStream = ftpClientConnection.retrieveFileStream(ftpFile.getName());
    FileOutputStream fileOutputStream = new FileOutputStream(directoryName + "/" + ftpFile.getName());
    //Using org.apache.commons.io.IOUtils
    IOUtils.copy(inputStream, fileOutputStream);
    fileOutputStream.flush();
    IOUtils.closeQuietly(fileOutputStream);
    IOUtils.closeQuietly(inputStream);
    boolean commandOK = ftpClientConnection.completePendingCommand();
    ....
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!