431 lines
15 KiB
Java
431 lines
15 KiB
Java
package com.fjy.spring.untils;
|
||
|
||
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;
|
||
|
||
import javax.servlet.http.HttpServletResponse;
|
||
import java.io.*;
|
||
|
||
/**
|
||
* @author F嘉阳
|
||
* @date 2018-05-06 11:35
|
||
*/
|
||
public class FtpUtils {
|
||
// private static final String FTP_URL = "176.122.138.235";
|
||
private static final String FTP_URL = "192.168.79.138";
|
||
private static final int PORT = 21;
|
||
private static final String USER_NAME = "cms";
|
||
private static final String PASSWORD = "imis2";
|
||
|
||
/**
|
||
* 本地字符编码
|
||
*/
|
||
private static final String LOCAL_CHARSET = "UTF-8";
|
||
|
||
/**
|
||
* FTP协议里面,规定文件名编码为iso-8859-1
|
||
*/
|
||
private static String SERVER_CHARSET = "iso-8859-1";
|
||
|
||
/**
|
||
* ftp上传单个文件
|
||
*
|
||
* @param directory 上传至ftp的路径名不包括ftp地址
|
||
* @param srcFileName 要上传的文件全路径名
|
||
* @param destName 上传至ftp后存储的文件名
|
||
* @throws IOException
|
||
*/
|
||
public static boolean upload(String directory, String srcFileName, String destName) throws IOException {
|
||
directory = new String(directory.getBytes("UTF-8"), "iso-8859-1");
|
||
destName = new String(destName.getBytes("UTF-8"), "iso-8859-1");
|
||
|
||
FTPClient ftpClient = makeConnectionFactory();
|
||
FileInputStream fis = null;
|
||
boolean result = false;
|
||
try {
|
||
File srcFile = new File(srcFileName);
|
||
fis = new FileInputStream(srcFile);
|
||
|
||
// 设置上传目录
|
||
ftpClient.changeWorkingDirectory(directory);
|
||
ftpClient.setBufferSize(1024);
|
||
ftpClient.setControlEncoding(LOCAL_CHARSET);
|
||
|
||
// 设置文件类型(二进制)
|
||
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
|
||
result = ftpClient.storeFile(destName, fis);
|
||
return result;
|
||
} catch (NumberFormatException e) {
|
||
System.out.println("FTP端口配置错误:不是数字:");
|
||
throw e;
|
||
} catch (FileNotFoundException e) {
|
||
throw new FileNotFoundException();
|
||
} catch (IOException e) {
|
||
throw new IOException(e);
|
||
} finally {
|
||
IOUtils.closeQuietly(fis);
|
||
try {
|
||
ftpClient.disconnect();
|
||
} catch (IOException e) {
|
||
throw new RuntimeException("关闭FTP连接发生异常!", e);
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
/**
|
||
* FTP单个文件下载
|
||
*
|
||
* @param directory 要下载的文件所在ftp的路径名不包含ftp地址
|
||
* @param destFileName 要下载的文件名
|
||
* @param downloadName 下载后存储的文件名全路径
|
||
*/
|
||
public static boolean download(String directory, String destFileName, String downloadName) throws IOException {
|
||
FTPClient ftpClient = makeConnectionFactory();
|
||
boolean result = false;
|
||
try {
|
||
ftpClient.setBufferSize(1024);
|
||
|
||
// 设置文件类型(二进制)
|
||
ftpClient.changeWorkingDirectory(directory);
|
||
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
|
||
|
||
System.out.println("destFileName:" + destFileName + ",downloadName:" + downloadName);
|
||
FileOutputStream fileOutputStream = new FileOutputStream(downloadName);
|
||
result = ftpClient.retrieveFile(destFileName, fileOutputStream);
|
||
|
||
fileOutputStream.flush();
|
||
return result;
|
||
} catch (NumberFormatException e) {
|
||
throw e;
|
||
} catch (FileNotFoundException e) {
|
||
throw new FileNotFoundException();
|
||
} catch (IOException e) {
|
||
throw new IOException(e);
|
||
} finally {
|
||
try {
|
||
ftpClient.disconnect();
|
||
} catch (IOException e) {
|
||
throw new RuntimeException("关闭FTP连接发生异常!", e);
|
||
}
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @param remotePath
|
||
* @param fileName
|
||
* @param localPath
|
||
* @return
|
||
* @author xh 测试成功 可以下载中文文件 ftp默认的编码为gbk
|
||
*/
|
||
public static boolean downFtpFile(String remotePath, String fileName,
|
||
String localPath) {
|
||
boolean success = false;
|
||
FTPClient ftp = new FTPClient();
|
||
try {
|
||
int reply;
|
||
ftp.connect(FTP_URL, PORT);
|
||
// 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
|
||
// 登录
|
||
ftp.login(USER_NAME, PASSWORD);
|
||
reply = ftp.getReplyCode();
|
||
if (!FTPReply.isPositiveCompletion(reply)) {
|
||
ftp.disconnect();
|
||
return success;
|
||
}
|
||
// 转移到FTP服务器目录
|
||
ftp.changeWorkingDirectory(remotePath);
|
||
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
|
||
|
||
FTPFile[] fs = ftp.listFiles();
|
||
for (FTPFile ff : fs) {
|
||
String fname = new String(ff.getName().getBytes("iso-8859-1"), "UTF-8");
|
||
if (fname.equals(fileName)) {
|
||
File localFile = new File(localPath + fname);
|
||
OutputStream is = new FileOutputStream(localFile);
|
||
ftp.retrieveFile(ff.getName(), is);
|
||
is.close();
|
||
break;
|
||
}
|
||
}
|
||
ftp.logout();
|
||
success = true;
|
||
} catch (IOException e) {
|
||
e.printStackTrace();
|
||
} finally {
|
||
if (ftp.isConnected()) {
|
||
try {
|
||
ftp.disconnect();
|
||
} catch (IOException ioe) {
|
||
}
|
||
}
|
||
}
|
||
return success;
|
||
}
|
||
|
||
/**
|
||
* @param fileName
|
||
* @return
|
||
* @author xh 测试成功 可以下载中文文件 ftp默认的编码为gbk
|
||
*/
|
||
public static OutputStream downFtpFileByStream(String fileName) {
|
||
|
||
String remotePath = "/www/ftp/cms/";
|
||
boolean success = false;
|
||
OutputStream is = null;
|
||
|
||
FTPClient ftp = new FTPClient();
|
||
try {
|
||
int reply;
|
||
ftp.connect(FTP_URL, PORT);
|
||
// 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
|
||
// 登录
|
||
ftp.login(USER_NAME, PASSWORD);
|
||
reply = ftp.getReplyCode();
|
||
if (!FTPReply.isPositiveCompletion(reply)) {
|
||
ftp.disconnect();
|
||
return null;
|
||
}
|
||
// 转移到FTP服务器目录
|
||
ftp.changeWorkingDirectory(remotePath);
|
||
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
|
||
|
||
FTPFile[] fs = ftp.listFiles();
|
||
for (FTPFile ff : fs) {
|
||
String fname = new String(ff.getName().getBytes("iso-8859-1"), "UTF-8");
|
||
if (fname.equals(fileName)) {
|
||
File localFile = new File(fname);
|
||
is = new FileOutputStream(localFile);
|
||
ftp.retrieveFile(ff.getName(), is);
|
||
return is;
|
||
}
|
||
}
|
||
ftp.logout();
|
||
success = true;
|
||
} catch (IOException e) {
|
||
e.printStackTrace();
|
||
} finally {
|
||
try {
|
||
assert is != null;
|
||
is.close();
|
||
} catch (IOException e) {
|
||
e.printStackTrace();
|
||
}
|
||
if (ftp.isConnected()) {
|
||
try {
|
||
ftp.disconnect();
|
||
} catch (IOException ioe) {
|
||
}
|
||
}
|
||
}
|
||
return null;
|
||
}
|
||
|
||
|
||
/**
|
||
* @param directory 要重命名的文件所在ftp的路径名不包含ftp地址
|
||
* @param oldFileName 要重命名的文件名
|
||
* @param newFileName 重命名后的文件名
|
||
* @throws IOException
|
||
*/
|
||
public static boolean rename(String directory, String oldFileName, String newFileName) throws IOException {
|
||
directory = new String(directory.getBytes("UTF-8"), "iso-8859-1");
|
||
oldFileName = new String(oldFileName.getBytes("UTF-8"), "iso-8859-1");
|
||
newFileName = new String(newFileName.getBytes("UTF-8"), "iso-8859-1");
|
||
|
||
/**
|
||
* 判断远程文件是否重命名成功,如果成功返回true,否则返回false
|
||
*/
|
||
boolean result = false;
|
||
FTPClient ftpClient = makeConnectionFactory();
|
||
try {
|
||
ftpClient.changeWorkingDirectory(directory);
|
||
//重命名远程文件
|
||
result = ftpClient.rename(oldFileName, newFileName);
|
||
return result;
|
||
} catch (NumberFormatException e) {
|
||
throw e;
|
||
} catch (IOException e) {
|
||
throw new IOException("连接ftp服务器失败!", e);
|
||
} finally {
|
||
try {
|
||
ftpClient.disconnect();
|
||
} catch (IOException e) {
|
||
throw new RuntimeException("关闭FTP连接发生异常!", e);
|
||
}
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @param directory 要删除的文件所在ftp的路径名不包含ftp地址
|
||
* @param fileName 要删除的文件名
|
||
* @return
|
||
* @throws IOException
|
||
*/
|
||
public static boolean remove(String directory, String fileName) throws IOException {
|
||
directory = new String(directory.getBytes("UTF-8"), "iso-8859-1");
|
||
fileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1");
|
||
|
||
/**
|
||
* 判断远程文件是否移除成功,如果成功返回true,否则返回false
|
||
*/
|
||
boolean result = false;
|
||
FTPClient ftpClient = makeConnectionFactory();
|
||
try {
|
||
ftpClient.changeWorkingDirectory(directory);
|
||
//删除远程文件
|
||
result = ftpClient.deleteFile(fileName);
|
||
return result;
|
||
} catch (NumberFormatException e) {
|
||
throw e;
|
||
} catch (IOException e) {
|
||
throw new IOException("连接ftp服务器失败!", e);
|
||
} finally {
|
||
try {
|
||
ftpClient.disconnect();
|
||
} catch (IOException e) {
|
||
throw new RuntimeException("关闭FTP连接发生异常!", e);
|
||
}
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @param directory 要创建的目录所在ftp的路径名不包含ftp地址
|
||
* @param newDirectory 要创建的新目录名
|
||
* @return
|
||
* @throws IOException
|
||
*/
|
||
public static boolean makeDirecotory(String directory, String newDirectory) throws IOException {
|
||
directory = new String(directory.getBytes("UTF-8"), "iso-8859-1");
|
||
newDirectory = new String(newDirectory.getBytes("UTF-8"), "iso-8859-1");
|
||
|
||
/**
|
||
* 判断远程文件是否移除成功,如果成功返回true,否则返回false
|
||
*/
|
||
boolean result = false;
|
||
FTPClient ftpClient = makeConnectionFactory();
|
||
try {
|
||
ftpClient.changeWorkingDirectory(directory);
|
||
result = ftpClient.makeDirectory(newDirectory);//创建新目录
|
||
return result;
|
||
} catch (NumberFormatException e) {
|
||
throw e;
|
||
} catch (IOException e) {
|
||
throw new IOException("连接ftp服务器失败!", e);
|
||
} finally {
|
||
try {
|
||
ftpClient.disconnect();
|
||
} catch (IOException e) {
|
||
throw new RuntimeException("关闭FTP连接发生异常!", e);
|
||
}
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @param directory 要重命名的目录所在ftp的路径名不包含ftp地址
|
||
* @param oldDirectory 要重命名的旧目录名
|
||
* @param newDirectory 重命名后的新目录
|
||
* @return
|
||
* @throws IOException
|
||
*/
|
||
public static boolean renameDirecotory(String directory, String oldDirectory, String newDirectory) throws IOException {
|
||
directory = new String(directory.getBytes("UTF-8"), "iso-8859-1");
|
||
oldDirectory = new String(oldDirectory.getBytes("UTF-8"), "iso-8859-1");
|
||
newDirectory = new String(newDirectory.getBytes("UTF-8"), "iso-8859-1");
|
||
|
||
// 判断远程文件是否移除成功,如果成功返回true,否则返回false
|
||
boolean result = false;
|
||
FTPClient ftpClient = makeConnectionFactory();
|
||
try {
|
||
ftpClient.changeWorkingDirectory(directory);
|
||
// 重命名目录
|
||
result = ftpClient.rename(oldDirectory, newDirectory);
|
||
return result;
|
||
} catch (NumberFormatException e) {
|
||
throw e;
|
||
} catch (IOException e) {
|
||
throw new IOException("连接ftp服务器失败!", e);
|
||
} finally {
|
||
try {
|
||
ftpClient.disconnect();
|
||
} catch (IOException e) {
|
||
throw new RuntimeException("关闭FTP连接发生异常!", e);
|
||
}
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @param directory 要重命名的目录所在ftp的路径名不包含ftp地址
|
||
* @param deldirectory 要删除的目录名
|
||
* @return
|
||
* @throws IOException
|
||
*/
|
||
public static boolean removeDirecotory(String directory, String deldirectory) throws IOException {
|
||
directory = new String(directory.getBytes("UTF-8"), "iso-8859-1");
|
||
deldirectory = new String(deldirectory.getBytes("UTF-8"), "iso-8859-1");
|
||
|
||
/**
|
||
* 判断远程文件是否移除成功,如果成功返回true,否则返回false
|
||
*/
|
||
boolean result = false;
|
||
FTPClient ftpClient = makeConnectionFactory();
|
||
try {
|
||
ftpClient.changeWorkingDirectory(directory);
|
||
result = ftpClient.removeDirectory(deldirectory);//删除目录
|
||
return result;
|
||
} catch (NumberFormatException e) {
|
||
throw e;
|
||
} catch (IOException e) {
|
||
throw new IOException("连接ftp服务器失败!", e);
|
||
} finally {
|
||
try {
|
||
ftpClient.disconnect();
|
||
} catch (IOException e) {
|
||
throw new RuntimeException("关闭FTP连接发生异常!", e);
|
||
}
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @param directory
|
||
* @return
|
||
* @throws IOException
|
||
*/
|
||
public static String[] list(String directory) throws IOException {
|
||
FTPClient ftpClient = makeConnectionFactory();
|
||
try {
|
||
ftpClient.changeWorkingDirectory(directory);
|
||
ftpClient.enterLocalPassiveMode();
|
||
//删除目录
|
||
return ftpClient.listNames();
|
||
} catch (NumberFormatException e) {
|
||
throw e;
|
||
} catch (IOException e) {
|
||
throw new IOException("连接ftp服务器失败!", e);
|
||
} finally {
|
||
try {
|
||
ftpClient.disconnect();
|
||
} catch (IOException e) {
|
||
throw new RuntimeException("关闭FTP连接发生异常!", e);
|
||
}
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 创建FTP连接的工厂方法
|
||
*
|
||
* @return
|
||
* @throws IOException
|
||
*/
|
||
private static FTPClient makeConnectionFactory() throws IOException {
|
||
FTPClient ftpClient = new FTPClient();
|
||
ftpClient.connect(FTP_URL, PORT);
|
||
ftpClient.login(USER_NAME, PASSWORD);
|
||
ftpClient.enterLocalPassiveMode();
|
||
ftpClient.setControlEncoding(LOCAL_CHARSET);
|
||
return ftpClient;
|
||
}
|
||
}
|