java ftp上传下载(包含删除操作)

废话不多说,直接上代码


import java.io.File;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;


import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.tomcat.util.http.fileupload.IOUtils;


/**
 * FTP上传下载工具类
 * 
 * @ClassName: FtpUtil
 * @Description: FTP上传下载工具类
 * @author: zhaohaidong
 * @date: 2017年6月19日 上午11:40:58
 */
public class FtpUtil {


/**
* FTP上传单个文件后全部文件

* @Title: upload
* @Description: FTP上传单个文件后全部文件
* @param host
*            服务器地址
* @param port
*            服务器端口号
* @param username
*            用户名
* @param password
*            密码
* @param filepath1
*            待上传文件路径:如/home/viong/Dns/
* @param filepath2
*            上传到服务器路径:如/home/ftpuser/Dns/
* @param filename1
*            待上传文件名称:如bengdi.txt
* @param filepath2
*            上传到服务器文件名称:如test.txt
* @return void
* @throws Exception
*/
public static void upload(String host, int port, String username, String password, String filepath1,
String filepath2, String filename1, String filename2,boolean delete) throws Exception {
FTPClient ftpClient = new FTPClient();
FileInputStream fis = null;
try {
// 建立FTP连接
ftpClient.connect(host, port);
ftpClient.login(username, password);
// 设置linux环境
FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_UNIX);
ftpClient.configure(conf);
// 判断服务器是否连接
int reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
System.out.println("FTP server refused connection.");
throw new Exception("202");
}
// 设置访问被动模式
ftpClient.setRemoteVerificationEnabled(false);
ftpClient.enterLocalPassiveMode();
// 设置文件类型(二进制)
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
// 设置上传目录
ftpClient.changeWorkingDirectory(filepath2);
// 设置缓冲大小和编码
ftpClient.setBufferSize(1024);
ftpClient.setControlEncoding("UTF-8");
// 建立from文件流
File srcFileDir = new File(filepath1);
File[] files = srcFileDir.listFiles();
for (File f : files) {
if(f.getName().toUpperCase().endsWith(".TXT")){
fis = new FileInputStream(f);
ftpClient.storeFile(f.getName(), fis);
fis.close();
if(delete){
f.delete();
}else{
f.renameTo(new File(f.getAbsolutePath()+".processed"));
}
}
}
} catch (IOException e) {
e.printStackTrace();
throw new Exception("204", e);
} finally {
IOUtils.closeQuietly(fis);
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
throw new Exception("206", e);
}
}
}


/**
* FTP下载单个或全部文件

* @Title: download
* @Description: FTP下载单个或全部文件
* @param host
*            服务器地址
* @param port
*            端口
* @param username
*            用户名
* @param password
*            密码
* @param filepath1
*            源文件存储路径:如/home/ftppath/Dns/
* @param filepath2
*            目标文件存储路径:如/home/temp/Dns/
* @param filename1
*            源文件名称:如test.tar.gz
* @param filename2
*            目标文件名称:如dns.tar.gz
* @param delete
*            下载完是否删除源文件
* @param wildcard
*            文件通配符
* @return void
* @throws Exception
*/
public static void download(String host, int port, String username, String password, String filepath1,
String filepath2, String filename1, String filename2, boolean delete, String wildcard) throws Exception {
FTPClient ftpClient = new FTPClient();
FileOutputStream fos = null;
try {
// 建立FTP连接
ftpClient.connect(host, port);
ftpClient.login(username, password);
// 设置linux环境
FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_UNIX);
ftpClient.configure(conf);
// 判断服务器是否连接
int reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
System.out.println("FTP server refused connection.");
throw new Exception("202");
}
// 设置访问被动模式
ftpClient.setRemoteVerificationEnabled(false);
ftpClient.enterLocalPassiveMode();
// 设置文件类型(二进制)
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
// 设置上传目录
boolean dir = ftpClient.changeWorkingDirectory(filepath1);
// 设置缓冲大小和编码
ftpClient.setBufferSize(1024);
ftpClient.setControlEncoding("UTF-8");
// 建立to文件流
if (dir) {
FTPFile[] fs = ftpClient.listFiles();
for (FTPFile f : fs) {
if (f.getName().endsWith(wildcard)) {
File localFile = new File(filepath2 + f.getName());
OutputStream ios = new FileOutputStream(localFile);
ftpClient.retrieveFile(f.getName(), ios);
ios.close();
// 下载成功,判断是否删除原文件
if (delete) {
ftpClient.deleteFile(f.getName());
} else {
ftpClient.rename(f.getName(), f.getName() + ".processed");
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
throw new Exception("204", e);
} finally {
IOUtils.closeQuietly(fos);
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
throw new Exception("206", e);
}
}


}
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值