package com.whyc.dto;
|
|
import com.whyc.constant.YamlProperties;
|
import lombok.extern.slf4j.Slf4j;
|
import org.apache.commons.net.ftp.FTPClient;
|
import org.springframework.stereotype.Component;
|
|
import java.io.File;
|
import java.io.FileInputStream;
|
import java.io.IOException;
|
|
@Component
|
@Slf4j
|
public class FTPClientUtil {
|
|
public static FTPClient connect(){
|
FTPClient ftpClient = new FTPClient();
|
try {
|
ftpClient.connect(YamlProperties.ftpIp,YamlProperties.ftpPort);
|
ftpClient.login(YamlProperties.ftpUserName,YamlProperties.ftpPassword);
|
ftpClient.enterLocalPassiveMode();
|
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
|
final int bufferSize = 20*1024 * 1024;
|
ftpClient.setBufferSize(bufferSize);
|
log.info("---------------FTP Client 连接成功------------------");
|
return ftpClient;
|
|
|
} catch (IOException e) {
|
e.printStackTrace();
|
log.error("---------------FTP Client 连接失败:"+e.toString()+"------------------");
|
}
|
return null;
|
}
|
|
public static void transferFile(File originalFile, String descFilePath){
|
FTPClient ftpClient = connect();
|
if(ftpClient !=null) {
|
FileInputStream fileInputStream = null;
|
try {
|
fileInputStream = new FileInputStream(originalFile);
|
ftpClient.storeFile(descFilePath, fileInputStream);
|
|
} catch (IOException e) {
|
log.error("---------------文件"+ originalFile.getName() +" FTP 传输异常:"+e.toString()+"------------------");
|
} finally {
|
try {
|
fileInputStream.close();
|
if (ftpClient.isConnected()) {
|
ftpClient.logout();
|
ftpClient.disconnect();
|
}
|
} catch (IOException ex) {
|
log.error("---------------关流异常:"+ex.toString()+"------------------");
|
}
|
}
|
}
|
|
}
|
|
|
}
|