package com.fengstingworld.framework.ftp;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.io.IOUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
public class FtpHelper
{
/**
* 向FTP服务器上传、下载、删除文件
* @param flag
* @param fileName
* @param remotePathName
* @param localPathName
* @param input
* @throws Exception
*/
public static void handleFtpFiles(String flag, String fileName, String remotePath, String localPathName, InputStream input){
ApplicationUtils applicationUtils = new ApplicationUtils();
String ip = StringUtils.toString(applicationUtils.getCo("FTPSERVER"));//FTP服务器IP地址
String username = StringUtils.toString(applicationUtils.getCo("FTPUSERNAME"));//FTP服务器用户名
String password = StringUtils.toString(applicationUtils.getCo("FTPPASSWORD"));//FTP服务器密码
int port;//FTP服务器端口
try{
port = Integer.parseInt("".equals(StringUtils.toString(StringUtils.toString(applicationUtils.getCo("FTPPORT")))) ? "0" : StringUtils.toString(StringUtils.toString(applicationUtils.getCo("FTPPORT"))));
}
catch (Exception e){
e.printStackTrace();
throw new RuntimeException("解析端口时出错了,请查系统参数配置的FTP端口是否合法!");
}
if("".equals(ip) || "".equals(username) || "".equals(password) || 0 == port)
return;
if("upload".equals(flag)){
uploadFile(ip, port, username, password, remotePath, fileName, input);
}
else if("download".equals(flag)){
downFile(ip, port, username, password, remotePath, fileName, localPathName);
}
else if("delete".equals(flag)){
deleteFile(ip, port, username, password, remotePath, fileName);
}
}
/**
* 文件上传源代码
* Description: 向FTP服务器上传文件
* @Version1.0
* @param url FTP服务器hostname
* @param port FTP服务器端口
* @param username FTP登录账号
* @param password FTP登录密码
* @param path FTP服务器保存目录
* @param filename 上传到FTP服务器上的文件名
* @param input 输入流
* @return 成功返回true,否则返回false
*/
public static void uploadFile(String url,int port,String username,String password,String path,String filename,InputStream input){
FTPClient ftp = loginFtpServer(url, port, username, password);
try{
for(String str : path.split("/")){
ftp.makeDirectory(str);
ftp.changeWorkingDirectory(str);
}
ftp.setBufferSize(2048);
ftp.setControlEncoding("UTF8");
//设置文件类型
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
ftp.storeFile(filename, input);
IOUtils.closeQuietly(input);
}catch(Exception ex){
ex.printStackTrace();
throw new RuntimeException("上传文件到FTP服务器出现异常");
}finally{
closeFtpServer(ftp);
}
}
/**
* 删除Ftp服务器上的某个文件
* @param url
* @param port
* @param username
* @param password
* @param path
* @param filename
* @return
*/
public static void deleteFile(String url,int port,String username,String password,String path,String filename){
FTPClient ftp = loginFtpServer(url, port, username, password);
try{
ftp.changeWorkingDirectory(path);
ftp.deleteFile(filename);
}catch(Exception ex){
ex.printStackTrace();
throw new RuntimeException("删除Ftp服务器上的文件异常");
}finally{
closeFtpServer(ftp);
}
}
/**
* 文件下载源代码
* Description: 从FTP服务器下载文件
* @Version1.0
* @param url FTP服务器hostname
* @param port FTP服务器端口
* @param username FTP登录账号
* @param password FTP登录密码
* @param remotePath FTP服务器上的相对路径
* @param fileName 要下载的文件名
* @param localPath 下载后保存到本地的路径
* @return
*/
public static boolean downFile(String url,int port,String username,String password,String remotePath,String fileName,String localPath) {
boolean success = false;
FTPClient ftp = loginFtpServer(url, port, username, password);
try {
ftp.changeWorkingDirectory(remotePath);//转移到FTP服务器目录
FTPFile[] fs = ftp.listFiles();
for(FTPFile ff:fs){
if(ff.getName().equals(fileName)){
File localFile = new File(localPath+"/"+ff.getName());
if (!localFile.exists() && !localFile.getParentFile().exists()) {
localFile.getParentFile().mkdirs();
}
OutputStream is = new FileOutputStream(localFile);
ftp.retrieveFile(ff.getName(), is);
is.close();
}
}
success = true;
} catch (Exception e) {
e.printStackTrace();
}finally{
closeFtpServer(ftp);
}
return success;
}
/**
* 连接FTP服务器
* @param url
* @param port
* @param username
* @param password
* @return
*/
public static FTPClient loginFtpServer(String url,int port,String username,String password){
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(url, port);
ftp.login(username, password);//登录
System.out.println("login success!");
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return null;
}
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("连接FTP服务器异常");
}
return ftp;
}
/**
* 关闭FTP服务器连接
* @param ftp
*/
public static void closeFtpServer(FTPClient ftp){
try{
ftp.logout();
}catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
}
}
FTP使用示例
最新推荐文章于 2021-08-11 21:00:05 发布