Apache Commons FTP问题

5
我希望使用Apache Commons Net实现一个FTP客户端,仅用于上传数据。 连接和登录到FTP服务器正常工作。 但是上传不正确。 文件比原始文件略大。 并且文件已损坏。 我尝试了图像、视频和文本文件。只有文本文件是正确的。 现在我在调试时发现
boolean tmp=client.setFileTransferMode(FTPClient.BINARY_FILE_TYPE);

给我返回了false,所以它无法设置。为什么呢? (也许这不是问题的原因?)

这里是我的其他代码:

client=new FTPClient();

    try {           
        int reply;
        client.connect(url, port);
        reply = client.getReplyCode();

        if (!FTPReply.isPositiveCompletion(reply))
        {
            client.disconnect();
            System.err.println("FTP server refused connection.");
            System.exit(1);
        }


        client.login(user, pw);
        boolean xxx=client.setFileTransferMode(FTPClient.BINARY_FILE_TYPE);
        client.setControlKeepAliveTimeout(300);
        client.enterLocalPassiveMode();

if (client.isConnected())
    {
    try {
        File file=new File(<FILE>);
        FileInputStream inputStream = new FileInputStream(file);
        OutputStream outputStream = client.storeFileStream(file.getName());

          byte[] buffer = new byte[4096];
          int l;
       while((l = inputStream.read(buffer))!=-1)
               {
                outputStream.write(buffer, 0, l);
            }

          inputStream.close();
          outputStream.flush();
          outputStream.close();}
3个回答

12

修改以下内容:

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

应该是:

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

你混淆了文件传输模式和文件类型。
可用的文件类型有: 可用的文件传输模式有:

我想,如果Apache为这些常量类型引入枚举,那么这种问题就可以避免,但是这个库将无法在Java 5之前的版本中使用。
我想知道Java 1.4的兼容性问题有多大。


2
如果只有文本文件成功传输,我怀疑你需要设置二进制传输文件类型。
请参阅setFileType方法以了解如何进行此操作。 commons-net wiki提到这是大多数文件损坏问题的原因。

嗨,我尝试了一下,但是这个方法给了我错误的结果,所以模式无法设置。 - Igor
你是在调用 connect 方法之后的某个地方尝试了这个吗? - Steve K
并且在登录方法之后。 - Steve K
他尝试了setFileTransferMode方法,而不是你建议的setFileType方法。这是一个正确的答案。 - Jesse Webb

0

这对我有用,上传图片并下载后一切正常:使用

    FTP.LOCAL_FILE_TYPE

这段代码使用了日志记录器,您可以替换为您自己的日志记录器或使用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();

    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接