【问题标题】:Set file path on FTPClient在 FTPClient 上设置文件路径
【发布时间】:2015-08-16 15:17:20
【问题描述】:

我是 java 编程新手,我正在使用 Apache commons net ftp 将文本文件上传到我的 ftp 服务器。

但是,我似乎只能将文件上传到与我的程序相同的目录中。 txt" ,它不会抛出任何错误,但是当我检查 ftp 时,什么都没有,就像它没有上传一样。

这是我正在使用的代码:

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

public class ftp{
    private final String host = "ftp.address.com";
    private final String user = "user";
    private final String pass = "pass";

    public static void main(String[] args) {
        ftp client = new ftp();
        client.FtpUpload("C:\\Users\\Packard\\Documents\\ProjectsJava\\FugeLessons\\outputFile.txt");
    }

    public String FtpUpload(String filename){
       FTPClient client = new FTPClient();
       FileInputStream fis = null;

       try {
           client.connect(this.host);
           client.login(this.user, this.pass);
           fis = new FileInputStream(filename);
           client.storeFile(filename, fis);
           client.logout();
           System.out.println("File " + filename + "\t uploaded successfully!");
       } catch(IOException e){
           error error = new error();
           error.setVisible(true);
           e.printStackTrace();
       } finally {
           try {
               if ( fis != null) {
                   fis.close();
               }
               client.disconnect();
           } catch(IOException e){
               e.printStackTrace();
           }
       }

       String ret = "success";
       return ret;
    }
}

我做错了什么? 感谢您的帮助!

【问题讨论】:

    标签: java apache-commons-net


    【解决方案1】:

    您可以使用 ftpClient 方法 getReplyString() 来获取错误消息。下面的代码可以提供帮助。

    boolean isSendSucces = ftpClient.storeFile(fileName, input );
    if( isSendSuccess )
    {
        System.out.println("Sent File: " + fileName);                   
    }
    else
    {
        System.out.println("Problem is sending File: " + ftpClient.getReplyString());
    }
    

    【讨论】:

    • 谢谢,但它仍然没有上传文件。我只收到了 ftp 输出消息:221-再见。您上传了 0 并下载了 0 KB。 221 注销。
    • 检查您尝试上传文件的文件夹的用户权限。
    猜你喜欢
    • 1970-01-01
    • 2016-02-17
    • 2011-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-13
    相关资源
    最近更新 更多