3
import org.apache.commons.net.ftp.FTPClient;

import org.apache.commons.net.ftp.FTPReply;

import org.apache.commons.net.ftp.FTPFile;
import java.io.*;

public class FTPUpload{


public static boolean uploadfile(String server,String username,String Password,String source_file_path,String dest_dir){

FTPClient ftp=new FTPClient();

try {

 int reply;


 ftp.connect(server);

   ftp.login(username, Password);
 System.out.println("Connected to " + server + ".");

 System.out.print(ftp.getReplyString());


 reply = ftp.getReplyCode();


 if(!FTPReply.isPositiveCompletion(reply)) {

ftp.disconnect();

System.err.println("FTP server refused connection.");

return false;

 }

 System.out.println("FTP server connected.");

                    InputStream input= new FileInputStream(source_file_path);


 ftp.storeFile(dest_dir, input);

  System.out.println( ftp.getReplyString() );

                    input.close();

                    ftp.logout();

 } catch(Exception e) {

                    System.out.println("err");

   e.printStackTrace();

                    return false;

  } finally {

   if(ftp.isConnected()) {

    try {

    ftp.disconnect();

    } catch(Exception ioe) {

    }

   }

  }

   return true;

   }



  public static void main(String[] args) {

   FTPUpload upload = new FTPUpload();

   try {

    upload.uploadfile("192.168.0.210","muruganp","vm4snk","/home/media/Desktop/FTP Upload/data.doc","/fileserver/filesbackup/Emac/");

   } catch (Exception e) {

   e.printStackTrace();

  }

  }

   }

我正在使用上面的代码在服务器位置 192.168.0.210 上传一个名为“data.doc”的文件。我的服务器的目标位置是 fileserver/filesbackup/Emac/。

但我最终收到错误“553 无法创建文件”,尽管服务器连接成功。我怀疑我以错误的方式给出了目标格式。请让我知道必须做些什么来解决这个问题?

4

1 回答 1

9

问题是您尝试将文件上传到目录。您应该指定目标文件名,而不是目标目录

当您在另一个 FTP 客户端中尝试相同的操作时,它是否有效?

[更新]

这是一些(未经测试,因为我没有 FTP 服务器)代码,它们可以更好地处理错误并且形式更短。

package so3972768;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.net.ftp.FTPClient;

public class FtpUpload {

  private static void check(FTPClient ftp, String cmd, boolean succeeded) throws IOException {
    if (!succeeded) {
      throw new IOException("FTP error: " + ftp.getReplyString());
    }
  }

  private static String today() {
    return new SimpleDateFormat("yyyy-MM-dd").format(new Date());
  }

  public void uploadfile(String server, String username, String Password, String sourcePath, String destDir) throws IOException {

    FTPClient ftp = new FTPClient();
    ftp.connect(server);
    try {
      check(ftp, "login", ftp.login(username, Password));

      System.out.println("Connected to " + server + ".");

      InputStream input = new FileInputStream(sourcePath);
      try {
        String destination = destDir;
        if (destination.endsWith("/")) {
          destination += today() + "-" + new File(sourcePath).getName();
        }
        check(ftp, "store", ftp.storeFile(destination, input));
        System.out.println("Stored " + sourcePath + " to " + destination + ".");
      } finally {
        input.close();
      }

      check(ftp, "logout", ftp.logout());

    } finally {
      ftp.disconnect();
    }
  }

  public static void main(String[] args) throws IOException {
    FtpUpload upload = new FtpUpload();
    upload.uploadfile("192.168.0.210", "muruganp", "vm4snk", "/home/media/Desktop/FTP Upload/data.doc", "/fileserver/filesbackup/Emac/");
  }

}
于 2010-10-19T21:22:34.637 回答