【问题标题】:FTP a file to server, but it the result arrives with zero byte size将文件 FTP 到服务器,但结果以零字节大小到达
【发布时间】:2011-10-06 03:59:46
【问题描述】:

我正在尝试使用 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
                }
            }
        }
    }
}

【问题讨论】:

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


【解决方案1】:

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

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

【讨论】:

  • 我使用的是 3.0.9,但同样的事情也会发生,而且只发生在 motorola defy 中。
【解决方案2】:

我删除了 client.changeWorkingDirectory("/loaddata"); 否则保持原样并获得成功。线路会不会有问题? 比我再次使用 commons 3.1

【讨论】:

    猜你喜欢
    • 2017-04-29
    • 1970-01-01
    • 1970-01-01
    • 2010-09-05
    • 1970-01-01
    • 2013-04-24
    • 1970-01-01
    • 2013-04-21
    • 1970-01-01
    相关资源
    最近更新 更多