当我们尝试使用FTPClient类和Selenium下载它们时,如何在FTP上保留文件的原始上次修改/创建日期

时间:2018-03-23 15:55:52

标签: selenium selenium-webdriver ftp automated-tests ftp-client

使用此代码,我能够下载该文件,但每当我运行脚本时,文件的上次修改日期都会设置为值。如何在不更改修改日期的情况下下载文件,因为验证需要它。

package oops;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

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


public class FTPDownloadFileDemo {

    public static void main(String[] args) {
        String server = "test.rebex.net";
        int port = 21;
        String user = "demo";
        String pass = "password";

        FTPClient ftpClient = new FTPClient();
        try {

            ftpClient.connect(server, port);
            ftpClient.login(user, pass);
            ftpClient.enterLocalPassiveMode();
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);


            String remoteFile1 = "/readme.txt";
            File downloadFile1 = new File("C:\\project\\readme1.txt");
            OutputStream outputStream1 = new BufferedOutputStream(new FileOutputStream(downloadFile1));
            boolean success = ftpClient.retrieveFile(remoteFile1, outputStream1);
            outputStream1.close();

            if (success) {
                System.out.println("File #1 has been downloaded successfully.");
            }

        } catch (IOException ex) {
            System.out.println("Error: " + ex.getMessage());
            ex.printStackTrace();
        } finally {
            try {
                if (ftpClient.isConnected()) {
                    ftpClient.logout();
                    ftpClient.disconnect();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}

0 个答案:

没有答案