在FTP服务器的特定路径中上载文件

时间:2015-02-19 15:52:43

标签: java ftp

我想在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.

2 个答案:

答案 0 :(得分:2)

您只能访问相对于ftp服务器根文件夹的文件。您需要配置ftp服务器以添加指向所需路径的虚拟文件夹。

答案 1 :(得分:1)

我传递给PassiveMode,现在可以使用