Apache Commons FTP problems

半城伤御伤魂 提交于 2019-11-29 10:59:21

Change the following:

boolean xxx=client.setFileTransferMode(FTPClient.BINARY_FILE_TYPE);

Should be:

boolean xxx=client.setFileType(FTP.BINARY_FILE_TYPE);

You have confused FileTransferModes with FileTypes.

The available FileTypes are:

The available FileTransferModes are:

I suppose if apache introduced enums for these constant types, then this kind of problem could be avoided, but then the library would not be available to pre-java-5 runtimes.
I wonder how much of an issue java 1.4 compatibility really is.

If only the text file was transferred successfully, I suspect you need to set the binary transfer file type.

See the setFileType method to see how to do this.

The commons-net wiki mentions this is the cause of most file corruption issues.

This work for me, Uploading Image and download after It´s Ok: Using

    FTP.LOCAL_FILE_TYPE

this code using logger, replace for you logger or use System.out.println("");

    private void cargarData(File filelocal) {
    FTPClient client = new FTPClient();

    try {

        client.connect("URLHOSTFTP", "PORT: DEFAULT 21");
        if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
            client.disconnect();
            logger.error("FTP server refused connection.");
            System.exit(1);
        }
        client.login("USER FTP", "PASS FTP");
        boolean type = client.setFileType(FTP.LOCAL_FILE_TYPE);

        logger.info("Tipo Aceptado:" + type);
        client.setControlKeepAliveTimeout(300);
        client.enterLocalPassiveMode();
        if (client.isConnected()) {
            FileInputStream fis = null;
            fis = new FileInputStream(filelocal);
            client.storeFile(filelocal.getName(), fis);
            client.logout();
            if (fis != null) {
                fis.close();
            }
        }
        logger.info(client.getReplyString());
    } catch (IOException e) {
        logger.error("error" + e.getMessage());
        e.printStackTrace();

    } catch (Exception e) {
        logger.error("error" + e.getMessage());
        e.printStackTrace();

    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!