FTP上传、下载实现,FTP级联创建目录实现
This commit is contained in:
@@ -16,7 +16,8 @@ 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.apache.commons.net.ftp.FTPFileFilter;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author F嘉阳
|
||||
@@ -25,23 +26,39 @@ import org.apache.commons.net.ftp.FTPFileFilter;
|
||||
public class FtpOperationUtil {
|
||||
|
||||
private FTPClient ftpClient;
|
||||
public static final int BINARY_FILE_TYPE = FTP.BINARY_FILE_TYPE;
|
||||
public static final int ASCII_FILE_TYPE = FTP.ASCII_FILE_TYPE;
|
||||
private static final int BINARY_FILE_TYPE = FTP.BINARY_FILE_TYPE;
|
||||
private static final int ASCII_FILE_TYPE = FTP.ASCII_FILE_TYPE;
|
||||
|
||||
/**
|
||||
* 利用FtpProperties进行服务器连接
|
||||
*
|
||||
* @param FtpProperties 参数配置Bean类
|
||||
* @throws SocketException
|
||||
* @throws IOException
|
||||
*/
|
||||
public void connectServer(FtpProperties FtpProperties) throws SocketException,
|
||||
public void connectServer() throws SocketException,
|
||||
IOException {
|
||||
String server = FtpProperties.getServer();
|
||||
int port = FtpProperties.getPort();
|
||||
String user = FtpProperties.getUsername();
|
||||
String password = FtpProperties.getPassword();
|
||||
String location = FtpProperties.getLocation();
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -114,9 +131,44 @@ public class FtpOperationUtil {
|
||||
* @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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 在服务器上删除目录
|
||||
*
|
||||
@@ -125,6 +177,7 @@ public class FtpOperationUtil {
|
||||
* @throws IOException
|
||||
*/
|
||||
public boolean removeDirectory(String path) throws IOException {
|
||||
path = new String(path.getBytes("UTF-8"), "iso-8859-1");
|
||||
return ftpClient.removeDirectory(path);
|
||||
}
|
||||
|
||||
@@ -139,6 +192,8 @@ public class FtpOperationUtil {
|
||||
public boolean removeDirectory(String path, boolean isAll)
|
||||
throws IOException {
|
||||
|
||||
path = new String(path.getBytes("UTF-8"), "iso-8859-1");
|
||||
|
||||
if (!isAll) {
|
||||
return removeDirectory(path);
|
||||
}
|
||||
@@ -173,6 +228,9 @@ public class FtpOperationUtil {
|
||||
* @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) {
|
||||
@@ -187,6 +245,7 @@ public class FtpOperationUtil {
|
||||
|
||||
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) {
|
||||
@@ -203,6 +262,9 @@ public class FtpOperationUtil {
|
||||
* @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() {
|
||||
@@ -223,6 +285,16 @@ public class FtpOperationUtil {
|
||||
return retList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有文件和目录
|
||||
*
|
||||
* @return
|
||||
* @throws IOException
|
||||
*/
|
||||
public String[] list() throws IOException {
|
||||
return ftpClient.listNames();
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除服务器上的文件
|
||||
*
|
||||
@@ -231,6 +303,8 @@ public class FtpOperationUtil {
|
||||
* @throws IOException
|
||||
*/
|
||||
public boolean deleteFile(String pathName) throws IOException {
|
||||
pathName = new String(pathName.getBytes("UTF-8"), "iso-8859-1");
|
||||
|
||||
return ftpClient.deleteFile(pathName);
|
||||
}
|
||||
|
||||
@@ -244,6 +318,11 @@ public class FtpOperationUtil {
|
||||
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 {
|
||||
@@ -253,6 +332,35 @@ public class FtpOperationUtil {
|
||||
//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 {
|
||||
@@ -271,6 +379,7 @@ public class FtpOperationUtil {
|
||||
* @throws IOException
|
||||
*/
|
||||
public boolean uploadFile(String fileName) throws IOException {
|
||||
fileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1");
|
||||
return uploadFile(fileName, fileName);
|
||||
}
|
||||
|
||||
@@ -284,11 +393,47 @@ public class FtpOperationUtil {
|
||||
*/
|
||||
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) {
|
||||
@@ -336,7 +481,7 @@ public class FtpOperationUtil {
|
||||
* @throws IOException
|
||||
*/
|
||||
public InputStream downFile(String sourceFileName) throws IOException {
|
||||
sourceFileName = sourceFileName;
|
||||
sourceFileName = new String(sourceFileName.getBytes("UTF-8"), "iso-8859-1");
|
||||
return ftpClient.retrieveFileStream(sourceFileName);
|
||||
}
|
||||
|
||||
@@ -344,6 +489,7 @@ public class FtpOperationUtil {
|
||||
//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("/");
|
||||
@@ -356,7 +502,7 @@ public class FtpOperationUtil {
|
||||
//ftpUtil.download("ERP实验1:销售预测与SOP.doc", "F:\\JAVA Workspace\\Temp\\ERP实验1:销售预测与SOP.doc");
|
||||
|
||||
String name = "ERP实验1:销售预测与SOP.doc";
|
||||
InputStream inputStream = ftpUtil.downFile(new String(name.getBytes("UTF-8"), "iso-8859-1"));
|
||||
InputStream inputStream = ftpUtil.downFile(name);
|
||||
|
||||
String destination = "F:\\JAVA Workspace\\Temp\\ERP实验1:销售预测与SOP.doc";
|
||||
int index;
|
||||
|
||||
Reference in New Issue
Block a user