【问题标题】:Uploading a file in a specific path of an FTP server [duplicate]在FTP服务器的特定路径中上传文件[重复]
【发布时间】:2015-04-21 01:23:01
【问题描述】:

我想在ftp服务器的特定路径上传文件,代码很简单:

public static void main(String[] args) {
String server = "xx.xx.xx.xx";
String user = "xxx";
String pass = "xxx";

FTPClient ftpClient = new FTPClient();
try {

    ftpClient.connect(server);
    System.out.println("Connected to " + server + ".");
    System.out.print(ftpClient.getReplyString());

    ftpClient.login(user, pass);
    ftpClient.enterLocalPassiveMode();

    ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

    // uploads first file using an InputStream
    File firstLocalFile = new File("/tmp/PAR.TXT");

    String firstRemoteFile = "/DATA/OUTFILES/PAR.TXT";
    InputStream inputStream = new FileInputStream(firstLocalFile);

    System.out.println("Start uploading first file");
    boolean done = ftpClient.storeFile(firstRemoteFile, inputStream);
    System.out.println("done:"+done);

    inputStream.close();
    if (done) {
        System.out.println("The file is uploaded successfully.");
    }


} catch (IOException ex) {
    System.out.println("Error: " + ex.getMessage());
    ex.printStackTrace();
} finally {
    try {
        if (ftpClient.isConnected()) {
            ftpClient.logout();
            ftpClient.disconnect();
        }
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

}

我总是完成 = 错误。

结果如下:

Connected to xx.xx.xx.xx.
220 "Welcome (logging activated)"
Start uploading file
done:false

我打印了 FtpClient#getReplyCode()。我明白了:

500 Illegal PORT command.

【问题讨论】:

  • ftp服务器是windows还是linux?
  • @Tarik linux,谢谢
  • 您可以使用 GUI FTP 客户端将同一个文件上传到同一个文件夹吗?

标签: java ftp


【解决方案1】:

您只能访问与 ftp 服务器的根文件夹相关的文件。您需要配置您的 ftp 服务器以添加一个指向您想要的路径的虚拟文件夹。

【讨论】:

    【解决方案2】:

    我通过了 PassiveMode,它现在可以工作了

    【讨论】:

    • 只是为了澄清其他用户,进入被动模式这样做:ftpClient.enterLocalPassiveMode();
    猜你喜欢
    • 1970-01-01
    • 2013-01-23
    • 1970-01-01
    • 2017-04-30
    • 1970-01-01
    • 2016-10-09
    • 1970-01-01
    • 2021-06-30
    • 2012-08-05
    相关资源
    最近更新 更多