Question

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 en utilisant le code ci-dessus pour télécharger un fichier nommé « Data.doc » dans l'emplacement du serveur 192.168.0.210. L'emplacement de destination de mon serveur est fileserver / filesbackup / Emac /.

Mais je finis par recevoir l'erreur « 553 Impossible de créer le fichier » bien que le serveur se connecté avec succès. Je pense que je donne le format de destination d'une mauvaise façon. Veuillez me faire savoir ce qui doit être fait pour résoudre le problème?

Était-ce utile?

La solution

Le problème est que vous essayez de télécharger le fichier dans un répertoire. Vous devriez plutôt le spécifier nom de fichier destination , pas le répertoire de destination .

fonctionne-t-il lorsque vous essayez la même chose dans un autre client FTP?

[Mise à jour]

Voici (non testé, puisque je n'ai pas un serveur FTP) du code qui fait l'erreur de manipulation mieux et sous une forme plus courte.

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

}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top