FTPUtil

public class FTPUtil {
	private static String encoding = System.getProperty("file.encoding");
	
	private FTPClient ftpClient;
	
	public FTPUtil(String url,int port,String username,String password) {
		ftpClient = new FTPClient();
		try {
			ftpClient.connect(url,port);
			ftpClient.login(username, password);
			ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
			ftpClient.setControlEncoding(encoding);
			ftpClient.enterLocalPassiveMode();
			ftpClient.setBufferSize(1024);
			FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);//解决中文乱码   
			conf.setServerLanguageCode("zh"); 
			ftpClient.configure(conf);
		} catch (SocketException e) {
			e.printStackTrace();
			throw new RuntimeException("FTP连接失败!", e);
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException("FTP客户端出错!", e);
		}
	}
	public FTPClient connect(String url,int port,String username,String password) {
		ftpClient = new FTPClient();
		try {
			ftpClient.connect(url,port);
			ftpClient.login(username, password);
			ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
			ftpClient.setControlEncoding(encoding);
			ftpClient.enterLocalPassiveMode();
			ftpClient.setBufferSize(1024);
			FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);   
			conf.setServerLanguageCode("zh"); 
			ftpClient.configure(conf);
		} catch (SocketException e) {
			e.printStackTrace();
			throw new RuntimeException("FTP连接失败!", e);
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException("FTP客户端出错!", e);
		}
        return ftpClient;
	}
	
	public void uploadFile(String src,String des) {
		File srcFile = new File(src);
		FileInputStream fis = null;
		try {
			fis = new FileInputStream(srcFile);
			ftpClient.storeFile(des, fis);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			throw new RuntimeException("文件未找到!", e);
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException("FTP客户端出错!", e);
		} finally {
            IOUtils.closeQuietly(fis);
        }
	}
	
	public void downLoadFile(String remote,String local) {
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(local);
			FTPFile[] files = ftpClient.listFiles();
			System.out.println(files[0].getName());
			ftpClient.retrieveFile(remote, fos);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			throw new RuntimeException("文件未找到!", e);
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException("FTP客户端出错!", e);
		} finally {
            IOUtils.closeQuietly(fos);
        }
	}
	
	public boolean isDirExists(String name) {
		boolean exist = false;
		if(StringUtils.isBlank(name)) {
			return exist;
		}
		try {
			FTPFile[] files = ftpClient.listDirectories();
			for(FTPFile file : files) {
				if(name.equals(file.getName())) {
					exist = true;
					return exist;
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException("FTP客户端出错!", e);
		}
		return exist;
	}
	public void closeConnect() {
		try {
			ftpClient.disconnect();
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException("关闭FTP连接发生异常!", e);
		}
	}
	
	public void cd(String dir) {
		try {
			ftpClient.changeWorkingDirectory(dir);
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException("FTP发生异常!", e);
		}
	}
	
	public void mkdir(String dir) {
		try {
			ftpClient.makeDirectory(dir);
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException("FTP发生异常!", e);
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值