package com.jiayue.ipfcst.ftpsftp.service; import lombok.extern.slf4j.Slf4j; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPFile; import org.springframework.stereotype.Service; import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; @Service @Slf4j public class ContinueFTPService { public FTPClient client; /** * 登录 * * @param host ip地址 * @param port 远程端口 * @param userName 用户名 * @param password 密码 * @throws IOException 抛出异常 */ public void initFtpClient(String host, int port, String userName, String password) throws IOException { client = new FTPClient();//s生成一个新的client client.connect(host, port);//链接 client.login(userName, password);//通过用户名和密码登录 } /** * 得到所有目录 * * @param remotepath 远程文件地址 * @return 返回FTPFile[] */ public FTPFile[] listFiles(String remotepath) throws IOException { client.enterLocalPassiveMode(); remotepath = new String(remotepath.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1); client.changeWorkingDirectory(remotepath);//获取所有的目录 return client.listFiles();//返回获取到的目录信息 } /** * 上传 * * @param remotepath (ftp的路径) * @return boolean 上传是否成功 * @throws IOException */ public boolean upload(String remotepath, String filename, InputStream inputStream) throws IOException { if (null == client) {//如果获取到的信息为空则返回false return false; } client.setFileType(FTPClient.BINARY_FILE_TYPE);//设置上传文件的类型 client.changeWorkingDirectory(remotepath);//获取上传的路径地址 String charSet = System.getProperties().get("file.encoding").toString(); String name = new String(filename.getBytes(charSet), StandardCharsets.ISO_8859_1); return client.storeFile(name, inputStream); } /** * 下载 * * @param name 文件名称 * @return 下载是否成功 */ public InputStream downLoad(String name) throws IOException { InputStream ins = null; try { client.setFileType(FTPClient.BINARY_FILE_TYPE);//设置下载文件的类型 //获取系统字符集 String charSet = System.getProperties().get("file.encoding").toString(); String fileName = new String(name.getBytes(charSet), StandardCharsets.ISO_8859_1); ins = client.retrieveFileStream(fileName); } catch (Exception e) { log.error("FTP下载错误"); } return ins; } /** * 删除文件 * * @param remotepath ftp服务端路径 * @return boolean */ public boolean delete(String remotepath, String name) throws IOException { client.changeWorkingDirectory(remotepath); String charSet = System.getProperties().get("file.encoding").toString(); String fileName = new String(name.getBytes(charSet), StandardCharsets.ISO_8859_1); return client.deleteFile(remotepath + fileName);//删除文件是否成功 } /** * 创建目录 * * @param remotepath ftp服务端路径 * @return boolean */ public boolean makeDirectory(String remotepath) throws IOException { if (null == client) { return false; } String[] item = remotepath.split("/");//以‘/’分割成字符串数组 String currentPath = ""; for (int i = 0; i < item.length - 1; i++) { currentPath = currentPath + "/" + item[i];//创建目录 client.makeDirectory(currentPath); } return client.makeDirectory(remotepath); } /** * 删除文件 * * @param remotepath ftp端路径 * @return boolean */ private boolean deleteDirectory(String remotepath) throws IOException { FTPFile[] files = listFiles(remotepath);//获取文件数组 for (FTPFile file : files) { if (file.isDirectory()) { //如果是删除目录 deleteDirectory(remotepath + "/" + file.getName());//删除目录 } else { client.deleteFile(remotepath + "/" + file.getName());//删除文件 } } return client.removeDirectory(remotepath); } /** * 重命名 * * @param remoteOldPath ftp旧名字文件 * @param remoteNewPath ftp新名字文件 * @return 是否修改名字成功 */ public boolean replaceName(String remoteOldPath, String remoteNewPath) throws IOException { if (null == client) { return false; } return client.rename(remoteOldPath, remoteNewPath); } /** * 退出登录 * * @throws IOException 异常 */ public void close() throws IOException { if (null != client) client.logout(); } }