ftp工具类

每回用到总去网上找一通,还是自已总结下比较好
package com.hjb.transForm.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Vector;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
/**
*
* @Class : FtpUtil
* @Description : ftp工具
* @since : v2.0
* @ModificationHistory
* Who When What
* ------- --------- -----------------------------------
* zhangshoulei 2014年9月19日 创建
*
* @version : TODO 最后修改时项目版本
*/
public class FtpUtil {
public static FTPClient getFtpClient(String address, int port, String uname, String pwd, String encoding)
throws Exception {
FTPClient client = null;

client = new FTPClient();
client.connect(address, port);
boolean islogin = client.login(uname, pwd);
if (!islogin) {
throw new Exception("帐号或者密码错误");
}
//client.setControlEncoding(encoding);

return client;
}

/**
* FTP文件递归
* @throws Exception
* */
public static List<String> ftpRecursive(FTPClient client, String dir, String localDir) throws Exception {
List<String> fileNames = new Vector<String>();
FTPFile[] ftpFiles = client.listFiles(dir);
for (FTPFile file : ftpFiles) {
String name = dir + "/" + file.getName();
if (file.isDirectory()) {
ftpRecursive(client, dir, name);
} else {
File localFile = new File(localDir + name);
if (!localFile.exists()) {
downFile(client, name, localFile);
}
}
}
return fileNames;
}

public static void downFile(FTPClient client, String fileName, File localFile) {
try {
System.out.println("正在下载文件:" + fileName);
localFile.getParentFile().mkdirs();
InputStream is = client.retrieveFileStream(fileName);
if (is == null) {
System.out.println("下载失败" + fileName);
return;
}
OutputStream os = new FileOutputStream(localFile);
int len = -1;
byte[] buffer = new byte[1024];
int all = 0;
while ((len = is.read(buffer)) != -1) {
all += len;
System.out.println("下载:" + all);
os.write(buffer, 0, len);
}
os.flush();
os.close();
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}

/**javaftp上传*/
public static boolean upload(FTPClient client, String localFile, String remoteName) {
try {
File file = new File(localFile);
if (file.exists()) {
InputStream local = new FileInputStream(file);

return client.appendFile(remoteName, local);
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**
* 断开ftp连接
*
* @throws
*/
public static void disconnect(FTPClient ftpClient) {
try {
if (ftpClient != null) {
ftpClient.logout();
if (ftpClient.isConnected()) {
ftpClient.disconnect();
ftpClient = null;
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
}
}

public static void main(String[] args) throws Exception {
FTPClient client = FtpUtil.getFtpClient("192.168.39.89", 21, "admin", "123", "UTF-8");
System.out.println(FtpUtil.upload(client, "E:/HJB01_file_index_20140723_1.zip", "/aa.zip"));
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值