Error in FTP upload "553 Could not create file"

LGAP picture LGAP · Oct 19, 2010 · Viewed 10.9k times · Source
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?

Answer

Roland Illig picture Roland Illig · Oct 19, 2010

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

}