You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
93 lines
2.6 KiB
93 lines
2.6 KiB
package com.eco.plugin.xx.jlyhsjtb.utils; |
|
|
|
import com.fr.ftp.client.FTPClient; |
|
import com.fr.ftp.client.FTPClientConfig; |
|
import com.fr.ftp.client.FTPFile; |
|
import com.fr.ftp.client.FTPReply; |
|
|
|
import java.io.File; |
|
import java.io.FileOutputStream; |
|
import java.io.OutputStream; |
|
|
|
public class FtpUtils { |
|
/** |
|
* 从linux下载ftp文件 |
|
* @param host |
|
* @param port |
|
* @param user |
|
* @param password |
|
* @param dir |
|
* @param filename |
|
* @return |
|
*/ |
|
public static File downloadFromLinux(String host,int port,String user,String password,String dir,String filename) { |
|
File templateFile = null; |
|
FTPClient ftp=null; |
|
try |
|
{ |
|
//设置ftp连接 |
|
ftp=new FTPClient(); |
|
ftp.connect(host,port); |
|
ftp.login(user, password); |
|
ftp.setFileType(FTPClient.BINARY_FILE_TYPE); |
|
|
|
//设置linux环境 |
|
FTPClientConfig conf = new FTPClientConfig( FTPClientConfig.SYST_UNIX); |
|
ftp.configure(conf); |
|
|
|
//判断是否连接成功 |
|
int reply = ftp.getReplyCode(); |
|
if (!FTPReply.isPositiveCompletion(reply)) |
|
{ |
|
ftp.disconnect(); |
|
FRUtils.FRLogError("ftp连接失败replyCode:"+reply); |
|
return null; |
|
} |
|
|
|
//设置访问被动模式 |
|
ftp.setRemoteVerificationEnabled(false); |
|
ftp.enterLocalPassiveMode(); |
|
|
|
//检索ftp目录下所有的文件,利用时间字符串进行过滤 |
|
boolean hasdir = ftp.changeWorkingDirectory(dir); |
|
|
|
if(!hasdir){ |
|
return null; |
|
} |
|
|
|
FTPFile[]fs = ftp.listFiles(); |
|
for(FTPFile f:fs) |
|
{ |
|
String ftpFilename =f.getName(); |
|
if(ftpFilename.equals(filename)) |
|
{ |
|
templateFile = File.createTempFile("test","txt"); |
|
OutputStream ios = new FileOutputStream(templateFile); |
|
ftp.retrieveFile(f.getName(), ios); |
|
ios.close(); |
|
break; |
|
} |
|
} |
|
|
|
} |
|
catch (Exception e) |
|
{ |
|
FRUtils.FRLogError("ftp下载失败:"+e.getMessage()); |
|
} |
|
finally |
|
{ |
|
if(ftp != null){ |
|
try |
|
{ |
|
ftp.disconnect(); |
|
} catch (Exception ioe) |
|
{ |
|
FRUtils.FRLogError("释放ftp连接失败"); |
|
} |
|
} |
|
} |
|
|
|
return templateFile; |
|
} |
|
|
|
}
|
|
|