0

我正在使用以下代码将 xml 文件上传到服务器中的目录 /home/domainname/public_html/guest 。但是,该文件仅上传到位置 /home/domainname。它没有上传到子目录。请指教。

FTPClient 客户端 = new 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();
        }
    }
4

2 回答 2

2

我检查了你的代码,它有效。我只是将文件类型声明更改为二进制,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_USER_ROOT}/someDirftp 服务器上的目录,名称更改为img_bis.png. 我认为这正是您想要实现的目标。

让我们回到你的问题。

  1. 尝试检查 Util.getProductsXMLFTPUploadPath()调用返回的内容。我的猜测是这不是你所期望的 - 所以在你的 IDE 中调试它或将它打印到控制台。
  2. 检查调用返回的路径是否Util.getProductsXMLFTPUploadPath() 以斜杠开头,它不应该。

更新 1.服务器上 是否/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!

这将告诉我们登录后当前的工作目录是什么。

请发布您的结果。

于 2013-03-02T21:05:09.160 回答
1
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.");
            }
于 2013-05-17T07:13:33.090 回答