582 lines
19 KiB
Java
582 lines
19 KiB
Java
package com.fjy.spring.untils;
|
||
|
||
import java.io.BufferedInputStream;
|
||
import java.io.BufferedOutputStream;
|
||
import java.io.File;
|
||
import java.io.FileInputStream;
|
||
import java.io.FileOutputStream;
|
||
import java.io.IOException;
|
||
import java.io.InputStream;
|
||
import java.io.OutputStream;
|
||
import java.net.SocketException;
|
||
import java.util.ArrayList;
|
||
import java.util.List;
|
||
|
||
import com.fjy.spring.properties.FtpProperties;
|
||
import org.apache.commons.net.ftp.FTP;
|
||
import org.apache.commons.net.ftp.FTPClient;
|
||
import org.apache.commons.net.ftp.FTPFile;
|
||
import org.springframework.beans.factory.annotation.Autowired;
|
||
import org.springframework.stereotype.Component;
|
||
|
||
/**
|
||
* @author F嘉阳
|
||
* @date 2018-05-07 19:56
|
||
*/
|
||
public class FtpOperationUtil {
|
||
|
||
private FTPClient ftpClient;
|
||
private static final int BINARY_FILE_TYPE = FTP.BINARY_FILE_TYPE;
|
||
private static final int ASCII_FILE_TYPE = FTP.ASCII_FILE_TYPE;
|
||
|
||
/**
|
||
* 利用FtpProperties进行服务器连接
|
||
*
|
||
* @throws SocketException
|
||
* @throws IOException
|
||
*/
|
||
public void connectServer() throws SocketException,
|
||
IOException {
|
||
FtpProperties ftpProperties = new FtpProperties();
|
||
String server = ftpProperties.getServer();
|
||
int port = ftpProperties.getPort();
|
||
String user = ftpProperties.getUsername();
|
||
String password = ftpProperties.getPassword();
|
||
String location = ftpProperties.getLocation();
|
||
connectServer(server, port, user, password, location);
|
||
}
|
||
|
||
public void connectServer(String path) throws SocketException,
|
||
IOException {
|
||
path = new String(path.getBytes("UTF-8"), "iso-8859-1");
|
||
if (!existDirectory(path)) {
|
||
createDirectory(path);
|
||
}
|
||
ftpClient.changeWorkingDirectory(path);
|
||
FtpProperties ftpProperties = new FtpProperties();
|
||
String server = ftpProperties.getServer();
|
||
int port = ftpProperties.getPort();
|
||
String user = ftpProperties.getUsername();
|
||
String password = ftpProperties.getPassword();
|
||
String location = ftpProperties.getLocation();
|
||
connectServer(server, port, user, password, location);
|
||
}
|
||
|
||
/**
|
||
* 使用详细信息进行服务器连接
|
||
*
|
||
* @param server:服务器地址名称
|
||
* @param port:端口号
|
||
* @param user:用户名
|
||
* @param password:用户密码
|
||
* @param path:转移到FTP服务器目录
|
||
* @throws SocketException
|
||
* @throws IOException
|
||
*/
|
||
public void connectServer(String server, int port, String user,
|
||
String password, String path) throws SocketException, IOException {
|
||
ftpClient = new FTPClient();
|
||
ftpClient.connect(server, port);
|
||
System.out.println("Connected to " + server + ".");
|
||
//连接成功后的回应码
|
||
System.out.println(ftpClient.getReplyCode());
|
||
ftpClient.login(user, password);
|
||
if (path != null && path.length() != 0) {
|
||
ftpClient.changeWorkingDirectory(path);
|
||
}
|
||
ftpClient.setBufferSize(1024);//设置上传缓存大小
|
||
ftpClient.setControlEncoding("UTF-8");//设置编码
|
||
ftpClient.setFileType(BINARY_FILE_TYPE);//设置文件类型
|
||
}
|
||
|
||
/**
|
||
* 设置传输文件类型:FTP.BINARY_FILE_TYPE | FTP.ASCII_FILE_TYPE
|
||
* 二进制文件或文本文件
|
||
*
|
||
* @param fileType
|
||
* @throws IOException
|
||
*/
|
||
public void setFileType(int fileType) throws IOException {
|
||
ftpClient.setFileType(fileType);
|
||
}
|
||
|
||
/**
|
||
* 关闭连接
|
||
*
|
||
* @throws IOException
|
||
*/
|
||
public void closeServer() throws IOException {
|
||
if (ftpClient != null && ftpClient.isConnected()) {
|
||
ftpClient.logout();//退出FTP服务器
|
||
ftpClient.disconnect();//关闭FTP连接
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 转移到FTP服务器工作目录
|
||
*
|
||
* @param path
|
||
* @return
|
||
* @throws IOException
|
||
*/
|
||
public boolean changeDirectory(String path) throws IOException {
|
||
return ftpClient.changeWorkingDirectory(path);
|
||
}
|
||
|
||
/**
|
||
* 在服务器上创建目录
|
||
*
|
||
* @param pathName
|
||
* @return
|
||
* @throws IOException
|
||
*/
|
||
public boolean createDirectory(String pathName) throws IOException {
|
||
pathName = new String(pathName.getBytes("UTF-8"), "iso-8859-1");
|
||
return ftpClient.makeDirectory(pathName);
|
||
}
|
||
|
||
/**
|
||
* 创建多级目录
|
||
*
|
||
* @param multiDirectory
|
||
* @return
|
||
*/
|
||
private boolean createMultiDirectory(String multiDirectory) {
|
||
boolean bool = false;
|
||
try {
|
||
String[] dirs = multiDirectory.split("/");
|
||
ftpClient.changeWorkingDirectory("/");
|
||
|
||
//按顺序检查目录是否存在,不存在则创建目录
|
||
for (int i = 1; dirs != null && i < dirs.length; i++) {
|
||
dirs[i] = new String(dirs[i].getBytes("UTF-8"), "iso-8859-1");
|
||
if (!ftpClient.changeWorkingDirectory(dirs[i])) {
|
||
if (ftpClient.makeDirectory(dirs[i])) {
|
||
if (!ftpClient.changeWorkingDirectory(dirs[i])) {
|
||
return false;
|
||
}
|
||
} else {
|
||
return false;
|
||
}
|
||
}
|
||
}
|
||
|
||
bool = true;
|
||
} catch (IOException e) {
|
||
e.printStackTrace();
|
||
} finally {
|
||
return bool;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 在服务器上删除目录
|
||
*
|
||
* @param path
|
||
* @return
|
||
* @throws IOException
|
||
*/
|
||
public boolean removeDirectory(String path) throws IOException {
|
||
path = new String(path.getBytes("UTF-8"), "iso-8859-1");
|
||
return ftpClient.removeDirectory(path);
|
||
}
|
||
|
||
/**
|
||
* 删除所有文件和目录
|
||
*
|
||
* @param path
|
||
* @param isAll true:删除所有文件和目录
|
||
* @return
|
||
* @throws IOException
|
||
*/
|
||
public boolean removeDirectory(String path, boolean isAll)
|
||
throws IOException {
|
||
|
||
path = new String(path.getBytes("UTF-8"), "iso-8859-1");
|
||
|
||
if (!isAll) {
|
||
return removeDirectory(path);
|
||
}
|
||
|
||
FTPFile[] ftpFileArr = ftpClient.listFiles(path);
|
||
if (ftpFileArr == null || ftpFileArr.length == 0) {
|
||
return removeDirectory(path);
|
||
}
|
||
//
|
||
for (FTPFile ftpFile : ftpFileArr) {
|
||
String name = ftpFile.getName();
|
||
if (ftpFile.isDirectory()) {
|
||
System.out.println("* [sD]Delete subPath [" + path + "/" + name + "]");
|
||
removeDirectory(path + "/" + name, true);
|
||
} else if (ftpFile.isFile()) {
|
||
System.out.println("* [sF]Delete file [" + path + "/" + name + "]");
|
||
deleteFile(path + "/" + name);
|
||
} else if (ftpFile.isSymbolicLink()) {
|
||
|
||
} else if (ftpFile.isUnknown()) {
|
||
|
||
}
|
||
}
|
||
return ftpClient.removeDirectory(path);
|
||
}
|
||
|
||
/**
|
||
* 检查目录在服务器上是否存在 true:存在 false:不存在
|
||
*
|
||
* @param path
|
||
* @return
|
||
* @throws IOException
|
||
*/
|
||
public boolean existDirectory(String path) throws IOException {
|
||
|
||
path = new String(path.getBytes("UTF-8"), "iso-8859-1");
|
||
|
||
boolean flag = false;
|
||
FTPFile[] ftpFileArr = ftpClient.listFiles(path);
|
||
for (FTPFile ftpFile : ftpFileArr) {
|
||
if (ftpFile.isDirectory()
|
||
&& ftpFile.getName().equalsIgnoreCase(path)) {
|
||
flag = true;
|
||
break;
|
||
}
|
||
}
|
||
return flag;
|
||
}
|
||
|
||
public boolean isExistsFile(String fileName) {
|
||
try {
|
||
fileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1");
|
||
FTPFile[] file = ftpClient.listFiles(fileName);
|
||
return file.length > 0 ? true : false;
|
||
} catch (IOException e) {
|
||
e.printStackTrace();
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 查看指定路径下是否存在该文件
|
||
*
|
||
* @param path
|
||
* @param fileName
|
||
* @return
|
||
*/
|
||
public boolean isExistsFile(String path, String fileName) throws IOException {
|
||
boolean flagChange = false;
|
||
|
||
if (path != null && path.length() != 0) {
|
||
String[] dirs = path.split("/");
|
||
ftpClient.changeWorkingDirectory("/");
|
||
|
||
for (int i = 1; dirs != null && i < dirs.length; i++) {
|
||
dirs[i] = new String(dirs[i].getBytes("UTF-8"), "iso-8859-1");
|
||
flagChange = ftpClient.changeWorkingDirectory(dirs[i]);
|
||
}
|
||
}
|
||
// 该语句必须位于创建目录之后
|
||
System.out.println("【目录切换】" + path + flagChange);
|
||
try {
|
||
fileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1");
|
||
FTPFile[] file = ftpClient.listFiles(fileName);
|
||
return file.length > 0;
|
||
} catch (IOException e) {
|
||
e.getMessage();
|
||
e.printStackTrace();
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 得到文件列表,listFiles返回包含目录和文件,它返回的是一个FTPFile数组
|
||
* listNames():只包含目录的字符串数组
|
||
* String[] fileNameArr = ftpClient.listNames(path);
|
||
*
|
||
* @param path:服务器上的文件目录:/DF4
|
||
*/
|
||
public List<String> getFileList(String path) throws IOException {
|
||
path = new String(path.getBytes("UTF-8"), "iso-8859-1");
|
||
|
||
|
||
FTPFile[] ftpFiles = ftpClient.listFiles(path);
|
||
//通过FTPFileFilter遍历只获得文件
|
||
/* FTPFile[] ftpFiles2= ftpClient.listFiles(path,new FTPFileFilter() {
|
||
@Override
|
||
public boolean accept(FTPFile ftpFile) {
|
||
return ftpFile.isFile();
|
||
}
|
||
}); */
|
||
List<String> retList = new ArrayList<String>();
|
||
if (ftpFiles == null || ftpFiles.length == 0) {
|
||
return retList;
|
||
}
|
||
for (FTPFile ftpFile : ftpFiles) {
|
||
if (ftpFile.isFile()) {
|
||
retList.add(ftpFile.getName());
|
||
}
|
||
}
|
||
return retList;
|
||
}
|
||
|
||
/**
|
||
* 获取所有文件和目录
|
||
*
|
||
* @return
|
||
* @throws IOException
|
||
*/
|
||
public String[] list() throws IOException {
|
||
return ftpClient.listNames();
|
||
}
|
||
|
||
public String[] list(String path) throws IOException {
|
||
String[] dirs = path.split("/");
|
||
ftpClient.changeWorkingDirectory("/");
|
||
for (int i = 1; dirs != null && i < dirs.length; i++) {
|
||
dirs[i] = new String(dirs[i].getBytes("UTF-8"), "iso-8859-1");
|
||
ftpClient.changeWorkingDirectory(dirs[i]);
|
||
}
|
||
return ftpClient.listNames();
|
||
}
|
||
|
||
/**
|
||
* 删除服务器上的文件
|
||
*
|
||
* @param pathName
|
||
* @return
|
||
* @throws IOException
|
||
*/
|
||
public boolean deleteFile(String pathName) throws IOException {
|
||
pathName = new String(pathName.getBytes("UTF-8"), "iso-8859-1");
|
||
|
||
return ftpClient.deleteFile(pathName);
|
||
}
|
||
|
||
public boolean renameFile(String oldFileName, String newFileName) throws IOException {
|
||
oldFileName = new String(oldFileName.getBytes("UTF-8"), "iso-8859-1");
|
||
newFileName = new String(newFileName.getBytes("UTF-8"), "iso-8859-1");
|
||
|
||
return ftpClient.rename(oldFileName, newFileName);
|
||
}
|
||
|
||
/**
|
||
* 上传文件到ftp服务器
|
||
* 在进行上传和下载文件的时候,设置文件的类型最好是:
|
||
* ftpUtil.setFileType(FtpUtil.BINARY_FILE_TYPE)
|
||
* localFilePath:本地文件路径和名称
|
||
* remoteFileName:服务器文件名称
|
||
*/
|
||
public boolean uploadFile(String localFilePath, String remoteFileName)
|
||
throws IOException {
|
||
remoteFileName = new String(remoteFileName.getBytes("UTF-8"), "iso-8859-1");
|
||
if (!existDirectory(localFilePath)) {
|
||
createDirectory(localFilePath);
|
||
ftpClient.changeWorkingDirectory(localFilePath);
|
||
}
|
||
|
||
boolean flag = false;
|
||
InputStream iStream = null;
|
||
try {
|
||
iStream = new FileInputStream(localFilePath);
|
||
//我们可以使用BufferedInputStream进行封装
|
||
//BufferedInputStream bis=new BufferedInputStream(iStream);
|
||
//flag = ftpClient.storeFile(remoteFileName, bis);
|
||
flag = ftpClient.storeFile(remoteFileName, iStream);
|
||
} catch (IOException e) {
|
||
System.out.println(e.getMessage());
|
||
flag = false;
|
||
return flag;
|
||
} finally {
|
||
if (iStream != null) {
|
||
iStream.close();
|
||
}
|
||
}
|
||
return flag;
|
||
}
|
||
|
||
public boolean uploadFile(String localFilePath, String path, String remoteFileName)
|
||
throws IOException {
|
||
remoteFileName = new String(remoteFileName.getBytes("UTF-8"), "iso-8859-1");
|
||
|
||
boolean flag = false;
|
||
InputStream iStream = null;
|
||
try {
|
||
iStream = new FileInputStream(localFilePath);
|
||
if (path != null && path.length() != 0) {
|
||
createDirectory(path);
|
||
// 该语句必须位于创建目录之后
|
||
path = new String(path.getBytes("UTF-8"), "iso-8859-1");
|
||
boolean flagChange = ftpClient.changeWorkingDirectory(path);
|
||
System.out.println("【目录切换】" + flagChange);
|
||
}
|
||
flag = ftpClient.storeFile(remoteFileName, iStream);
|
||
} catch (IOException e) {
|
||
System.out.println(e.getMessage());
|
||
flag = false;
|
||
return flag;
|
||
} finally {
|
||
if (iStream != null) {
|
||
iStream.close();
|
||
}
|
||
}
|
||
return flag;
|
||
}
|
||
|
||
/**
|
||
* 上传文件到ftp服务器,上传新的文件名称和原名称一样
|
||
*
|
||
* @param fileName:文件名称
|
||
* @return
|
||
* @throws IOException
|
||
*/
|
||
public boolean uploadFile(String fileName) throws IOException {
|
||
fileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1");
|
||
return uploadFile(fileName, fileName);
|
||
}
|
||
|
||
/**
|
||
* 上传文件到ftp服务器
|
||
*
|
||
* @param iStream 输入流
|
||
* @param newName 新文件名称
|
||
* @return
|
||
* @throws IOException
|
||
*/
|
||
public boolean uploadFile(InputStream iStream, String newName)
|
||
throws IOException {
|
||
newName = new String(newName.getBytes("UTF-8"), "iso-8859-1");
|
||
boolean flag = false;
|
||
try {
|
||
flag = ftpClient.storeFile(newName, iStream);
|
||
} catch (IOException e) {
|
||
flag = false;
|
||
System.out.println(e.getMessage());
|
||
return flag;
|
||
} finally {
|
||
if (iStream != null) {
|
||
iStream.close();
|
||
}
|
||
}
|
||
return flag;
|
||
}
|
||
|
||
/**
|
||
* 上传文件到ftp服务器
|
||
*
|
||
* @param iStream 输入流
|
||
* @param newName 新文件名称
|
||
* @return
|
||
* @throws IOException
|
||
*/
|
||
public boolean uploadFile(InputStream iStream, String newName, String path)
|
||
throws IOException {
|
||
|
||
newName = new String(newName.getBytes("UTF-8"), "iso-8859-1");
|
||
boolean flag = false;
|
||
try {
|
||
if (path != null && path.length() != 0) {
|
||
createMultiDirectory(path);
|
||
// 该语句必须位于创建目录之后
|
||
path = new String(path.getBytes("UTF-8"), "iso-8859-1");
|
||
boolean flagChange = ftpClient.changeWorkingDirectory(path);
|
||
System.out.println("【目录切换】" + flagChange);
|
||
}
|
||
flag = ftpClient.storeFile(newName, iStream);
|
||
} catch (IOException e) {
|
||
flag = false;
|
||
System.out.println(e.getMessage());
|
||
return flag;
|
||
} finally {
|
||
if (iStream != null) {
|
||
iStream.close();
|
||
}
|
||
}
|
||
return flag;
|
||
}
|
||
|
||
/**
|
||
* 从ftp服务器上下载文件到本地
|
||
*
|
||
* @param remoteFileName:ftp服务器上文件名称
|
||
* @param localFileName:本地文件名称
|
||
* @return
|
||
* @throws IOException
|
||
*/
|
||
public boolean download(String remoteFileName, String localFileName)
|
||
throws IOException {
|
||
remoteFileName = new String(remoteFileName.getBytes("UTF-8"), "iso-8859-1");
|
||
|
||
boolean flag = false;
|
||
File outfile = new File(localFileName);
|
||
OutputStream oStream = null;
|
||
try {
|
||
oStream = new FileOutputStream(outfile);
|
||
//我们可以使用BufferedOutputStream进行封装
|
||
//BufferedOutputStream bos=new BufferedOutputStream(oStream);
|
||
//flag = ftpClient.retrieveFile(remoteFileName, bos);
|
||
flag = ftpClient.retrieveFile(remoteFileName, oStream);
|
||
} catch (IOException e) {
|
||
flag = false;
|
||
return flag;
|
||
} finally {
|
||
oStream.close();
|
||
}
|
||
return flag;
|
||
}
|
||
|
||
/**
|
||
* 从ftp服务器上下载文件到本地
|
||
*
|
||
* @param sourceFileName:服务器资源文件名称
|
||
* @return InputStream 输入流
|
||
* @throws IOException
|
||
*/
|
||
public InputStream downFile(String sourceFileName) throws IOException {
|
||
sourceFileName = new String(sourceFileName.getBytes("UTF-8"), "iso-8859-1");
|
||
return ftpClient.retrieveFileStream(sourceFileName);
|
||
}
|
||
|
||
public InputStream downFile(String path, String sourceFileName) throws IOException {
|
||
sourceFileName = new String(sourceFileName.getBytes("UTF-8"), "iso-8859-1");
|
||
path = new String(path.getBytes("UTF-8"), "iso-8859-1");
|
||
if (path != null && path.length() != 0) {
|
||
boolean flagChange = ftpClient.changeWorkingDirectory(path);
|
||
System.out.println("【目录切换】" + path + flagChange);
|
||
}
|
||
return ftpClient.retrieveFileStream(sourceFileName);
|
||
}
|
||
|
||
public static void main(String[] args) throws Exception {
|
||
//testUpload();
|
||
//testDownload();
|
||
FtpOperationUtil ftpUtil = new FtpOperationUtil();
|
||
ftpUtil = new FtpOperationUtil();
|
||
ftpUtil.connectServer("192.168.79.138", FTPClient.DEFAULT_PORT, "cms", "imis2", null);
|
||
//获得ftp服务器上目录名称为DF4下的所有文件名称
|
||
List<String> list = ftpUtil.getFileList("/");
|
||
for (String str : list) {
|
||
System.out.println(str);
|
||
}
|
||
// 上传本地D盘文件aaa.txt到服务器,服务器上名称为bbb.txt
|
||
//ftpUtil.uploadFile("F:\\JAVA Workspace\\Temp\\upload\\ERP实验1:销售预测与SOP.doc", "ERP实验1:销售预测与SOP.doc");
|
||
// 从服务器上下载文件bbb.txt到本地d盘名称为ccc.txt
|
||
//ftpUtil.download("ERP实验1:销售预测与SOP.doc", "F:\\JAVA Workspace\\Temp\\ERP实验1:销售预测与SOP.doc");
|
||
|
||
String name = "ERP实验1:销售预测与SOP.doc";
|
||
InputStream inputStream = ftpUtil.downFile(name);
|
||
|
||
String destination = "F:\\JAVA Workspace\\Temp\\ERP实验1:销售预测与SOP.doc";
|
||
int index;
|
||
byte[] bytes = new byte[1024];
|
||
FileOutputStream downloadFile = new FileOutputStream(destination);
|
||
while ((index = inputStream.read(bytes)) != -1) {
|
||
downloadFile.write(bytes, 0, index);
|
||
downloadFile.flush();
|
||
}
|
||
downloadFile.close();
|
||
inputStream.close();
|
||
|
||
// 删除ftp服务器上文件:bbb.txt
|
||
//ftpUtil.deleteFile("bbb.txt");
|
||
}
|
||
|
||
}
|