FTP 上传错误“553 无法创建文件”

发布于 2024-09-27 20:24:43 字数 1740 浏览 7 评论 0原文

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 无法创建文件”。我怀疑我以错误的方式给出了目标格式。请让我知道必须采取什么措施才能解决该问题?

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();

  }

  }

   }

Am using the above code to upload a file named "data.doc" in the server location 192.168.0.210.
The destination location of my server is fileserver/filesbackup/Emac/.

But I end up receiving the error "553 Could not create file" although the server gets connected successfully. I suspect that I am giving the destination format in a wrong way. Kindly let me know what has to be done to resolve the issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾其所爱 2024-10-04 20:24:43

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

当您在另一个 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/");
  }

}

The problem is that you try to upload the file to a directory. You should rather specifiy the destination filename, not the destination directory.

Does it work when you try the same in another FTP client?

[Update]

Here is some (untested, since I don't have an FTP server) code that does the error handling better and in a shorter form.

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/");
  }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文