java - 将文件通过 FTP 传输到服务器,但结果以零字节大小到达

标签 java ftp ftp-client apache-commons-net

我正在尝试使用 FTPClient 将文件上传到数据库服务器。显示文件传输成功,但文件为空(大小为0字节)。

下面是我用来构建的源代码。谁能解决这个问题?

package Examples;

import org.apache.commons.net.ftp.*;
import java.io.FileInputStream;
import java.io.IOException;

public class Main {

    public static void main(String[] args) {

        FTPClient client = new FTPClient();
        FileInputStream fis = null;

        try {

            client.connect("server");
            client.login("userid", "password");
            System.out.print("Message : " + client.getReplyString());

            client.changeWorkingDirectory("/loaddata");
            System.out.println("Working Directory" + client.printWorkingDirectory());

            client.setDefaultPort(22);
            int f1 = client.getDefaultPort();
            boolean f2 = client.setFileType(FTPClient.BINARY_FILE_TYPE);
            System.out.println("File transfer port no  " + f1);
            System.out.println("FTP server reply ." + client.getReplyString());

            String localfile = "c:/Touch.txt";
            fis = new FileInputStream(localfile);
            int lastSlash = localfile.lastIndexOf('/');
            String filename = localfile.substring(lastSlash+1);
            System.out.println("file : "+fis);

            client.setFileTransferMode(2);
            System.out.println("Flag reply ." + client.getReplyString());

            boolean flag = client.storeFile(filename,fis);

            System.out.println("Flag reply ." + client.getReplyString());

            if (flag) {
                System.out.println("Successfully uploaded the file");
            } else {
                System.out.println("Not able to upload the file");
            }

            fis.close();
            client.logout();
            System.out.println("Logout ." + client.getReplyString());

        } catch (Exception e) {
            System.out.println("Exception " + e);
        } finally {
            if (client.isConnected()) {
                try {
                    client.disconnect();
                    System.out.println("Server Disconnected." + client.getReplyString());
                } catch (IOException ioe) {
                    // do nothing
                }
            }
        }
    }
}

最佳答案

查看其他一些 FTPClient 问题,我认为原因是 bug in the Apache Commons-NET library (其中 FTPClient 是一个组件)在 3.0 版中。

安装更新版本(3.0.1 修复了错误)。

关于java - 将文件通过 FTP 传输到服务器,但结果以零字节大小到达,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6707682/

相关文章:

php - FTP 连接太多,无法接受更多

java - 使用 jni.h 在 C++ 中编译 java 方法时生成错误

java - 使用 Java API 对数据集中所有列进行数据操作

java - eclipselink JPA : combining @MappedSuperclass with @Cacheable

c# - 使用 ftpwebrequest 上传到 ftp 时图像损坏

c++ - 在 FTP 服务器中正确实现 LIST 命令

azure - 为 Azure 应用服务 WebApp 实现 HTTPS

java - 使用pagemap如何生成随机数

java - 文件被复制到FTP服务器上而不是在java中移动

java 。用于解析 FTP 服务器列表的正则表达式