最近小编遇到这样一个需求:
客户数据存储在ftp服务器上,需要将数据从ftp上下载下来并导入到数据库中,并与数据库中现有的数据进行对比,筛选出有差异的数据。
积累了如下(读取/下载 ftp服务器文件)工具类,希望能帮助到有需要的童鞋:
方式一:使用FTPClient
·用到的jar包:
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.4</version>
<classifier>ftp</classifier>
</dependency>
·FTPUtil工具类:
public class FTPUtils {
private FTPClient ftpClient = null;
private String server;
private int port;
private String userName;
private String userPassword;
public FTPUtils(String server, int port, String userName,
String userPassword) {
this.server = server;
this.port = port;
this.userName = userName;
this.userPassword = userPassword;
}
/**
* 链接到服务器
*
* @return
* @throws Exception
*/
public boolean open() {
if (ftpClient != null && ftpClient.isConnected()) {
return true;
}
try {
ftpClient = new FTPClient();
// 连接
ftpClient.connect(this.server, this.port);
ftpClient.login(this.userName, this.userPassword);
// 检测连接是否成功
int reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
this.close();
System.err.println("FTP server refused connection.");
System.exit(1);
}
System.out.println("open FTP success:" + this.server+";port:"+this.port + ";name:"
+ this.userName + ";pwd:" + this.userPassword);
ftpClient.setFileType(ftpClient.BINARY_FILE_TYPE);
// or ascii
return true;
} catch (Exception ex) {
// 关闭
this.close();
ex.printStackTrace();
return false;
}
}
/**
* 获取目录下所有的文件名称
*
* @param filePath
* @return
* @throws IOException
*/
public String[] getFileList(String filePath) throws IOException {
FTPFile[] list = ftpClient.listFiles();
String[] fileNameList=new String[list.length];
if(list.length>0){
for (int i=0;i<list.length;i++){
fileNameList[i]=list[i].getName();
}
}
return fileNameList;
}
public String readFile(String fileName) throws ParseException {
InputStream ins = null;
StringBuilder builder = null;
try {
// 从服务器上读取指定的文件
ins = ftpClient.retrieveFileStream(fileName);
System.out.println(ins);
BufferedReader reader = new BufferedReader(new InputStreamReader(ins));
String line;
builder = new StringBuilder(150);
while ((line = reader.readLine()) != null) {
// System.out.println(line);
builder.append(line);
}
reader.close();
if (ins != null) {
ins.close();
}
ftpClient.getReply();
} catch (Exception e) {
e.printStackTrace();
}
return builder.toString();
}
/**
* 关闭链接
*/
public void close() {
try {
if (ftpClient != null && ftpClient.isConnected())
ftpClient.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Close Server Success :"+this.server+";port:"+this.port);
}
方式二:使用ChannelSftp
·用到的jar包
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.51</version>
</dependency>
<!-- jsch 所依赖的jar包 -->
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jzlib</artifactId>
<version>1.0.7</version>
</dependency>
·FtpJSchUtil工具类
private static ChannelSftp sftp = null;
//账号
private static String user = "***";
//主机ip
private static String host = "***";;
//密码
private static String password = "***";;
//端口
private static int port = 0000;
//上传地址
private static String directory = "/";
//下载目录
private static String saveFile = "E:/ftpdata";
public static FtpJSch getConnect(){
FtpJSch ftp = new FtpJSch();
try {
JSch jsch = new JSch();
//获取sshSession 账号-ip-端口
Session sshSession =jsch.getSession(user, host,port);
//添加密码
sshSession.setPassword(password);
Properties sshConfig = new Properties();
//严格主机密钥检查
sshConfig.put("StrictHostKeyChecking", "no");
sshSession.setConfig(sshConfig);
//开启sshSession链接
sshSession.connect();
//获取sftp通道
Channel channel = sshSession.openChannel("sftp");
//开启
channel.connect();
sftp = (ChannelSftp) channel;
} catch (Exception e) {
e.printStackTrace();
}
return ftp;
}
/**
*
* @param uploadFile 上传文件的路径
* @return 服务器上文件名
*/
public String upload(String uploadFile) {
File file = null;
String fileName = null;
try {
sftp.cd(directory);
file = new File(uploadFile);
//获取随机文件名
fileName = UUID.randomUUID().toString() + file.getName().substring(file.getName().length()-5);
//文件名是 随机数加文件名的后5位
sftp.put(new FileInputStream(file), fileName);
} catch (Exception e) {
e.printStackTrace();
}
return file == null ? null : fileName;
}
/**
* 下载文件
*
// * @param directory
// * 下载目录
// * @param downloadFile
// * 下载的文件名
// * @param saveFile
// * 存在本地的路径
// * @param sftp
*/
public static void download(String downloadFileName) {
try {
sftp.cd(directory);
File file = new File(saveFile+"/"+downloadFileName);
sftp.get(downloadFileName, new FileOutputStream(file));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 删除文件
*
* @param deleteFile
* 要删除的文件名字
// * @param sftp
*/
public void delete(String deleteFile) {
try {
sftp.cd(directory);
sftp.rm(deleteFile);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 列出目录下的文件
*
* @param directory
* 要列出的目录
// * @param sftp
* @return
* @throws SftpException
*/
public static Vector listFiles(String directory)
throws SftpException {
System.out.println(sftp.ls(directory));
return sftp.ls(directory);
}
}