FTP上传错误“553无法创建文件” [英] Error in FTP upload "553 Could not create file"

查看:1110
本文介绍了FTP上传错误“553无法创建文件”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

导入org.apache.commons.net.ftp.FTPReply;

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

尝试{

int回复;


ftp.connect(服务器);

ftp.login(用户名,密码);
System.out.println(Connected to+ server +。);

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


reply = ftp.getReplyCode();


if(!FTPReply.isPositiveCompletion(reply)){

ftp.disconnect();

System.err.println(FTP服务器拒绝连接。);

返回false;

}

System.out.println(FTP服务器连接。);

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

返回false;
$ b $ finally {

if(ftp.isConnected()){

try {

ftp.disconnect( );

} catch(Exception ioe){

}

}

}

返回true;




$ b public static void main(String [] args){

FTPUpload upload = new FTPUpload );

尝试{

upload.uploadfile(192.168.0.210,muruganp,vm4snk,/ home / media / Desktop / FTP Upload / data.doc , /文件服务器/ filesbackup / EMAC /);

} catch(Exception e){

e.printStackTrace();

}

}

}

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



但我最终收到错误553无法创建文件,尽管服务器成功连接。我怀疑我以错误的方式给出目的地格式。请让我知道要解决该问题需要做些什么? 解决方案

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

在另一个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){
抛出新的IOException(FTP错误:+ ftp.getReplyString());



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

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

FTPClient ftp = new FTPClient() ;
ftp.connect(服务器);
尝试{
check(ftp,login,ftp.login(username,Password));

System.out.println(连接到+服务器+。);

InputStream input = new FileInputStream(sourcePath);
尝试{
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();


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

}


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?

解决方案

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

}

这篇关于FTP上传错误“553无法创建文件”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆