FTP上传下载测试

来自网站:https://www.zhangjunbk.com

FTP上传下载测试

190730190137924.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3ozNjA5MDEwNjExMDA=,size_16,color_FFFFFF,t_70)

FTP测试代码

package test.ftp;

import java.io.File;
import java.util.Date;

import org.junit.Test;

import zj.date.util.DateUtil;
import zj.ftp.bean.ConstantForEnum;
import zj.ftp.bean.DownloadFile;
import zj.ftp.bean.UploadFile;
import zj.ftp.util.FTPUtil;
import zj.message.util.MessageConstantsUtil;

public class TestFTPUtil {
	String ftpHost;
	String ftpUserName;
	String ftpPassword;
	String dir = "D:/blog/java/上传下载";

	public void 设置FTP() {
		ftpHost = MessageConstantsUtil.getConstantValueByMemory("ftp.下传下载.host");
		ftpUserName = MessageConstantsUtil.getConstantValueByMemory("ftp.下传下载.userName");
		ftpPassword = MessageConstantsUtil.getConstantValueByMemory("ftp.下传下载.password");
	}

	@Test
	public void 下载指定文件() {
		try {
			System.out.println("【"+DateUtil.dateParse(new Date(), "yyyy-MM-dd HH:mm:ss:SSS (0-6=>周日-周六)w x p")+"】开始下载");
			设置FTP();
			String date = DateUtil.dateParse(new Date(), "yyyyMMdd");
			date = "20190730";
			final FTPUtil util = new FTPUtil(ftpHost, ftpUserName, ftpPassword);
			DownloadFile download = new DownloadFile();
			download.remotePath = "/all_sort_" + date + ".txt.zip";
			download.localFile = new File(dir + "/all_sort_" + date + ".txt.zip");
			download.downLoadType = ConstantForEnum.DownloadType.LOCAL_FILE.getValue();
			download.overWrite = ConstantForEnum.OverWrite.SKIP.getValue();
			util.downloadFile(download);
			download.remotePath = "/沪深L1.docx";
			download.localFile = new File(dir + "/沪深L1.docx");
			util.downloadFile(download);
			System.out.println("【"+DateUtil.dateParse(new Date(), "yyyy-MM-dd HH:mm:ss:SSS (0-6=>周日-周六)w x p")+"】下载完成");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Test
	public void 下载所有文件() {
		try {
			设置FTP();
			final FTPUtil util = new FTPUtil(ftpHost, ftpUserName, ftpPassword);
			DownloadFile download = new DownloadFile();
			download.remotePath = "/";
			download.localFile = new File(dir);
			download.downLoadType = ConstantForEnum.DownloadType.SERVER_DIRECTORY.getValue();
			download.overWrite = ConstantForEnum.OverWrite.OVER.getValue();
			util.downloadFile(download);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Test
	public void 下载文件() {
		try {
			final FTPUtil util = new FTPUtil(ftpHost, ftpUserName, ftpPassword);
			DownloadFile download = new DownloadFile();
			download.remotePath = "/tip.info.log";
			download.localFile = new File(dir + "/zhangjun/a.log");
			download.downLoadType = ConstantForEnum.DownloadType.LOCAL_FILE.getValue();
			download.overWrite = ConstantForEnum.OverWrite.OVER.getValue();
			util.downloadFile(download);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Test
	public void 上传文件夹() {
		try {
			设置FTP();
			final FTPUtil util = new FTPUtil(ftpHost, ftpUserName, ftpPassword);
			UploadFile upload = new UploadFile();
			upload.remotePath = "/";
			upload.localFile = new File(dir);
			boolean b = util.uploadFile(upload);
			System.out.println(b);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Test
	public void 上传文件() {
		try {
			设置FTP();
			final FTPUtil util = new FTPUtil(ftpHost, ftpUserName, ftpPassword);
			UploadFile upload = new UploadFile();
			upload.remotePath = "/a/b/c/";
			upload.localFile = new File(dir + "/tip.info.log");
			boolean b = util.uploadFile(upload);
			System.out.println(b);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

    原文作者:zhangjunbk.com张军个人网站
    原文地址: https://blog.csdn.net/z360901061100/article/details/97809101
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞