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);
}
}
}
FTPUtil
最新推荐文章于 2021-05-06 22:19:28 发布