簡體   English   中英

apache.commons.net.ftp.FTPClient未將文件上傳到所需的文件夾

[英]apache.commons.net.ftp.FTPClient not uploading the file to the required folder

我正在使用以下代碼將xml文件上傳到服務器中的目錄/ home / domainname / public_html / guest。 但是,該文件僅上傳到位置/ home / domainname。 它沒有上載到子目錄。 請指教。

FTPClient客戶端=新的FTPClient(); FileInputStream fis = null;

    try {
        client.connect(Util.getProductsXMLFTPServer());
        client.login(Util.getProductsXMLFTPUser(), Util.getProductsXMLFTPPassword());

        //
        // Create an InputStream of the file to be uploaded
        //

        fis = new FileInputStream(new File(Util.getProductsXMLFTPInputFilePath(), Util.getProductsXMLFTPOutputFileName()));
        client.changeWorkingDirectory(Util.getProductsXMLFTPUploadPath());


        client.storeFile(Util.getProductsXMLFTPOutputFileName(), fis);
        client.logout();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (fis != null) {
                fis.close();
            }
            client.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

我檢查了您的代碼,它可以正常工作。 我只將文件類型聲明更改為二進制,而XML文件可能不需要。 這是我完整的代碼供參考:

package apachenet.ftp;

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

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

public class App {
    public static void main( String[] args ) {
        FTPClient client = new FTPClient(); 
        FileInputStream fis = null;
        try {
            client.connect(/*Util.getProductsXMLFTPServer()*/"127.0.0.1");
            client.login(/*Util.getProductsXMLFTPUser()*/"pwyrwinski", 
                    /*Util.getProductsXMLFTPPassword()*/"secret");
            client.setFileType(FTP.BINARY_FILE_TYPE); // optional
            fis = new FileInputStream(
                    new File(/* Util.getProductsXMLFTPInputFilePath() */"/home/pwyrwinski", 
                            /* Util.getProductsXMLFTPOutputFileName() */"img.png"));
            client.changeWorkingDirectory(/*Util.getProductsXMLFTPUploadPath()*/ "someDir");


            client.storeFile(/*Util.getProductsXMLFTPOutputFileName()*/"img_bis.png", fis);
            client.logout();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
                client.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

如您所見,它與您的大致相同。 Util類調用將替換為原始數據。

當我運行它時,文件/home/pwyrwinski/img.png被上傳到ftp服務器上的{FTP_USER_ROOT}/someDir目錄,名稱更改為img_bis.png 我認為這正是您想要實現的目標。

讓我們回到您的問題。

  1. 嘗試檢查Util.getProductsXMLFTPUploadPath()調用返回的Util.getProductsXMLFTPUploadPath() 我的猜測不是您所期望的-因此請在IDE中對其進行調試或將其打印到控制台。
  2. 檢查從Util.getProductsXMLFTPUploadPath()調用返回的路徑是否以斜杠開頭,而不應該以斜杠開頭。

更新1.服務器上是否存在direcory /home/domainname/public_html/guest

將以下方法添加到您的班級:

private static void showServerReply(FTPClient ftpClient) {
    String[] replies = ftpClient.getReplyStrings();
    if (replies != null && replies.length > 0) {
        for (String aReply : replies) {
            System.out.println("SERVER: " + aReply);
        }
    }
}

並在每個 ftp客戶端的方法調用之后調用它。 這將為您提供每個命令結果的代碼和描述。 我懷疑client.changeWorkingDirectory(...)以錯誤結尾,可能是: 550 Permission Denied (or No such file or folder)

下一個修改將是:

client.login(Util.getProductsXMLFTPUser(), Util.getProductsXMLFTPPassword());
System.out.println(client.printWorkingDirectory()); // added this line!

登錄后,這將告訴我們當前的工作目錄是什么。

請發布您的結果。

FTPClient ftpClient = new FTPClient();
        try {
              System.out.println("before server connection");
            ftpClient.connect(server, port);
            System.out.println("before user name and passwod");
            ftpClient.login(user, pass);
            ftpClient.enterLocalActiveMode();

            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

            System.out.println("connection sucess");


            // windows working fine
            File secondLocalFile = new File("/home/aims/archived_reports/tes_S_000000123/test.pdf");
//            String secondRemoteFile = "/archived_reports/PermanentRecord.pdf";

//linux
          //  File secondLocalFile = new File("/archived_reports/tes_S_000009123/test.pdf");

            String secondRemoteFile = "remotefilename.pdf";
            InputStream  inputStream = new FileInputStream(secondLocalFile);

            System.out.println("Start uploading second file");

                    ftpClient.changeWorkingDirectory("/reports");// home/ftp.test/reports folder


          System.out.println("Prasent Working Directory :"+ftpClient.printWorkingDirectory());


            OutputStream outputStream = ftpClient.storeFileStream(secondRemoteFile);
                 int returnCode = ftpClient.getReplyCode();
                 System.out.println(returnCode);
            byte[] bytesIn = new byte[4096];
            int read = 1;

            while ((read = inputStream.read(bytesIn)) != -1) {
                outputStream.write(bytesIn, 0, read);
            }

System.out.println();           
            inputStream.close();
            outputStream.close();
                     boolean completed = ftpClient.completePendingCommand();
            if (completed) {
                System.out.println("The second file is uploaded successfully.");
            }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM