FTP転送ファイルUtils
4468 ワード
import com.lenovo.vpe.common.utils.ftp.FtpUtil;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPReply;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.TimeZone;
public class FTPUtil {
private FTPClient ftpClient;
private boolean isLogin;
private static Logger logger = LoggerFactory.getLogger(FtpUtil.class);
/**
* @Description: ftp
* @param url ftp
* @param port ftp
* @param userName ftp
* @param passWord ftp
* @return
* @throws IOException
*/
public boolean connectFTP(String url,int port,String userName,String passWord)throws IOException {
this.ftpClient=new FTPClient();
FTPClientConfig ftpClientConfig=new FTPClientConfig();
ftpClientConfig.setServerTimeZoneId(TimeZone.getDefault().getID());
this.ftpClient.setControlEncoding("UTF-8");
this.ftpClient.configure(ftpClientConfig);
this.ftpClient.connect(url.trim(),port);
//ftp
int reply=this.ftpClient.getReplyCode();
if(!FTPReply.isPositiveCompletion(reply)){
this.ftpClient.disconnect();
logger.info(" ftp ,code:\t{}",reply);
return this.isLogin;
}
this.ftpClient.login(userName.trim(),passWord.trim());
//
this.ftpClient.enterLocalPassiveMode();
this.ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
logger.info(" FTP ,userName:{},URL:{}",userName,url);
this.isLogin=true;
this.ftpClient.setBufferSize(1024*1024);
this.ftpClient.setDataTimeout(60*1000);
return this.isLogin;
}
/**
* @Description
* @param localFile
* @param remoteUploadPath ftp
* @return
*/
public boolean uploadFile(File localFile,String remoteUploadPath){
BufferedInputStream inputStream=null;
boolean success =false;
try{
this.ftpClient.changeWorkingDirectory(remoteUploadPath.trim());
inputStream=new BufferedInputStream(new FileInputStream(localFile));
logger.info(" :{}",localFile.getName());
success=this.ftpClient.storeFile(localFile.getName(),inputStream);
if(success){
logger.info(" , :{}",localFile.getName());
return success;
}
}catch (IOException e){
logger.error("FTP :{}",e.getMessage());
}finally {
if(null!=inputStream){
try{
inputStream.close();
}catch (Exception e){
logger.error("FTP :{}",e.getMessage());
}
}
}
return success;
}
/**
* @Description / FTP
*/
public void loginOut(){
if(null!=this.ftpClient && this.ftpClient.isConnected()){
try{
boolean logout=this.ftpClient.logout();
if(logout){
logger.info(" FTP ");
}
}catch (Exception e){
logger.error(" FTP :{}",e.getMessage());
}finally {
try{
this.ftpClient.disconnect();// ftp
}catch (Exception e){
logger.error("FTP :{}",e.getMessage());
}
}
}
}
}
=================================================================
:
// ftp
try{
boolean isLogin=ftpUtil.connectFTP(host,port,userName,password);
if(isLogin){
boolean isSuccess=ftpUtil.uploadFile(file,filePath);
}
}catch (IOException e){
}finally {
ftpUtil.loginOut();
if (file.exists()) {
file.delete();
}
logger.info("endTime:{}",DateUtil.getDateFormat(new Date(),"yyyyMMdd_HH:mm:ss"));
}