|
@@ -1,11 +1,16 @@
|
|
|
package top.lvzhiqiang.util;
|
|
package top.lvzhiqiang.util;
|
|
|
|
|
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.net.ftp.FTP;
|
|
import org.apache.commons.net.ftp.FTP;
|
|
|
import org.apache.commons.net.ftp.FTPClient;
|
|
import org.apache.commons.net.ftp.FTPClient;
|
|
|
|
|
+import org.apache.commons.net.ftp.FTPFile;
|
|
|
import org.apache.commons.net.ftp.FTPReply;
|
|
import org.apache.commons.net.ftp.FTPReply;
|
|
|
|
|
|
|
|
|
|
+import java.io.File;
|
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
import java.io.InputStream;
|
|
|
|
|
+import java.io.OutputStream;
|
|
|
|
|
+import java.net.SocketException;
|
|
|
import java.time.LocalDate;
|
|
import java.time.LocalDate;
|
|
|
import java.util.Random;
|
|
import java.util.Random;
|
|
|
|
|
|
|
@@ -13,6 +18,7 @@ import java.util.Random;
|
|
|
* 用于上传文件的工具类
|
|
* 用于上传文件的工具类
|
|
|
* https://juejin.cn/post/6894219004673294349<使用nginx和vsftp搭建图片服务器并使用Java上传图片到该图片服务器>
|
|
* https://juejin.cn/post/6894219004673294349<使用nginx和vsftp搭建图片服务器并使用Java上传图片到该图片服务器>
|
|
|
*/
|
|
*/
|
|
|
|
|
+@Slf4j
|
|
|
public class FtpUtil {
|
|
public class FtpUtil {
|
|
|
|
|
|
|
|
private static String host;
|
|
private static String host;
|
|
@@ -35,41 +41,21 @@ public class FtpUtil {
|
|
|
baseUrl = baseUrl_;
|
|
baseUrl = baseUrl_;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public static boolean uploadFile(String filename, InputStream input) {
|
|
|
|
|
- return uploadFile(host, port, userName, passWord, basePath, filename, input);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
/**
|
|
/**
|
|
|
* Description: 向FTP服务器上传文件
|
|
* Description: 向FTP服务器上传文件
|
|
|
*
|
|
*
|
|
|
- * @param host FTP服务器ip
|
|
|
|
|
- * @param port FTP服务器端口
|
|
|
|
|
- * @param username FTP登录账号
|
|
|
|
|
- * @param password FTP登录密码
|
|
|
|
|
- * @param basePath FTP服务器基础目录,/home/ftpuser/images
|
|
|
|
|
- * @param filename 上传到FTP服务器上的文件名
|
|
|
|
|
|
|
+ * @param fileName 上传到FTP服务器上的文件名
|
|
|
* @param input 输入流
|
|
* @param input 输入流
|
|
|
* @return 成功返回true,否则返回false
|
|
* @return 成功返回true,否则返回false
|
|
|
*/
|
|
*/
|
|
|
- public static boolean uploadFile(String host, int port, String username, String password, String basePath, String filename, InputStream input) {
|
|
|
|
|
- boolean result = false;
|
|
|
|
|
- FTPClient ftpClient = new FTPClient();
|
|
|
|
|
|
|
+ public static boolean uploadFile(String fileName, InputStream input) {
|
|
|
|
|
+ FTPClient ftpClient = null;
|
|
|
|
|
+ boolean flag = false;
|
|
|
try {
|
|
try {
|
|
|
- // 连接FTP服务器
|
|
|
|
|
- //// 如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器
|
|
|
|
|
- ftpClient.connect(host, port);
|
|
|
|
|
- // 登录
|
|
|
|
|
- ftpClient.login(username, password);
|
|
|
|
|
- int replyCode = ftpClient.getReplyCode();
|
|
|
|
|
- if (!FTPReply.isPositiveCompletion(replyCode)) {
|
|
|
|
|
- ftpClient.disconnect();
|
|
|
|
|
- return result;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ ftpClient = genFtpClient();
|
|
|
|
|
|
|
|
// 设置为被动模式
|
|
// 设置为被动模式
|
|
|
ftpClient.enterLocalPassiveMode();
|
|
ftpClient.enterLocalPassiveMode();
|
|
|
- // 设置编码格式为utf-8
|
|
|
|
|
- ftpClient.setControlEncoding("UTF-8");
|
|
|
|
|
// 设置上传文件的类型为二进制类型
|
|
// 设置上传文件的类型为二进制类型
|
|
|
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
|
|
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
|
|
|
// 设置存储图片的文件夹
|
|
// 设置存储图片的文件夹
|
|
@@ -80,20 +66,54 @@ public class FtpUtil {
|
|
|
continue;
|
|
continue;
|
|
|
}
|
|
}
|
|
|
if (!ftpClient.changeWorkingDirectory(s)) {
|
|
if (!ftpClient.changeWorkingDirectory(s)) {
|
|
|
|
|
+ // 创建目录,一次只能创建一个目录
|
|
|
ftpClient.makeDirectory(s);
|
|
ftpClient.makeDirectory(s);
|
|
|
|
|
+ // 重新指定工作路径
|
|
|
ftpClient.changeWorkingDirectory(s);
|
|
ftpClient.changeWorkingDirectory(s);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
// 上传文件
|
|
// 上传文件
|
|
|
- if (!ftpClient.storeFile(filename, input)) {
|
|
|
|
|
- return result;
|
|
|
|
|
|
|
+ flag = ftpClient.storeFile(fileName, input);
|
|
|
|
|
+ } catch (SocketException e) {
|
|
|
|
|
+ log.error("文件上传-连接错误,fileName={}", fileName, e);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("文件上传-失败,fileName={}", fileName, e);
|
|
|
|
|
+ } finally {
|
|
|
|
|
+ try {
|
|
|
|
|
+ if (ftpClient.isConnected()) {
|
|
|
|
|
+ ftpClient.logout();
|
|
|
|
|
+ ftpClient.disconnect();
|
|
|
|
|
+ }
|
|
|
|
|
+ if (input != null) {
|
|
|
|
|
+ input.close();
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (IOException ioe) {
|
|
|
|
|
+ ioe.printStackTrace();
|
|
|
}
|
|
}
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- input.close();
|
|
|
|
|
|
|
+ return flag;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- result = true;
|
|
|
|
|
- } catch (IOException e) {
|
|
|
|
|
- e.printStackTrace();
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Description: 从FTP服务器上传删除文件
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param filePath
|
|
|
|
|
+ * @return 成功返回true,否则返回false
|
|
|
|
|
+ */
|
|
|
|
|
+ public static boolean delFile(String filePath) {
|
|
|
|
|
+ FTPClient ftpClient = null;
|
|
|
|
|
+ boolean flag = false;
|
|
|
|
|
+ try {
|
|
|
|
|
+ ftpClient = genFtpClient();
|
|
|
|
|
+
|
|
|
|
|
+ // 删除文件
|
|
|
|
|
+ flag = ftpClient.deleteFile(filePath);
|
|
|
|
|
+ } catch (SocketException e) {
|
|
|
|
|
+ log.error("文件删除-连接错误,filePath={}", filePath, e);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("文件删除-失败,filePath={}", filePath, e);
|
|
|
} finally {
|
|
} finally {
|
|
|
if (ftpClient.isConnected()) {
|
|
if (ftpClient.isConnected()) {
|
|
|
try {
|
|
try {
|
|
@@ -103,7 +123,77 @@ public class FtpUtil {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- return result;
|
|
|
|
|
|
|
+
|
|
|
|
|
+ return flag;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Description: 从FTP服务器上传删除文件
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param filePath
|
|
|
|
|
+ * @return 成功返回true,否则返回false
|
|
|
|
|
+ */
|
|
|
|
|
+ public static void downloadFile(File filePath, OutputStream is) {
|
|
|
|
|
+ FTPClient ftpClient = null;
|
|
|
|
|
+ try {
|
|
|
|
|
+ ftpClient = genFtpClient();
|
|
|
|
|
+
|
|
|
|
|
+ // 下载文件
|
|
|
|
|
+ // 转移到FTP服务器目录
|
|
|
|
|
+ ftpClient.changeWorkingDirectory(filePath.getParent());
|
|
|
|
|
+ FTPFile[] fs = ftpClient.listFiles();
|
|
|
|
|
+ String fileName = filePath.getName();
|
|
|
|
|
+ for (FTPFile ff : fs) {
|
|
|
|
|
+ if (ff.getName().equals(fileName)) {
|
|
|
|
|
+ ftpClient.retrieveFile(ff.getName(), is);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (SocketException e) {
|
|
|
|
|
+ log.error("文件下载-连接错误,filePath={}", filePath, e);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("文件下载-失败,filePath={}", filePath, e);
|
|
|
|
|
+ } finally {
|
|
|
|
|
+ if (ftpClient.isConnected()) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ ftpClient.logout();
|
|
|
|
|
+ ftpClient.disconnect();
|
|
|
|
|
+ } catch (IOException ioe) {
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static FTPClient genFtpClient() throws IOException {
|
|
|
|
|
+ return genFtpClient(host, port, userName, passWord);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param host_ FTP服务器ip
|
|
|
|
|
+ * @param port_ FTP服务器端口
|
|
|
|
|
+ * @param userName_ FTP登录账号
|
|
|
|
|
+ * @param passWord_ FTP登录密码
|
|
|
|
|
+ * @return
|
|
|
|
|
+ * @throws IOException
|
|
|
|
|
+ */
|
|
|
|
|
+ public static FTPClient genFtpClient(String host_,Integer port_,String userName_,String passWord_) throws IOException {
|
|
|
|
|
+ // 1.创建FTPClient对象
|
|
|
|
|
+ FTPClient ftpClient = new FTPClient();
|
|
|
|
|
+ // 2.保存FTP控制连接使用的字符集,必须在连接前设置
|
|
|
|
|
+ ftpClient.setControlEncoding("UTF-8");
|
|
|
|
|
+ // 3.连接FTP服务器,如果采用默认端口,可以使用ftp.connect(host)的方式直接连接FTP服务器
|
|
|
|
|
+ ftpClient.connect(host_, port_);
|
|
|
|
|
+ // 4.登录
|
|
|
|
|
+ ftpClient.login(userName_, passWord_);
|
|
|
|
|
+ // 5.连接成功或者失败返回的状态码,如果reply返回230表示成功,如果返回530表示无密码或用户名错误或密码错误或用户权限问题。
|
|
|
|
|
+ int replyCode = ftpClient.getReplyCode();
|
|
|
|
|
+ if (!FTPReply.isPositiveCompletion(replyCode)) {
|
|
|
|
|
+ ftpClient.disconnect();
|
|
|
|
|
+
|
|
|
|
|
+ throw new IOException("FTP登录失败!");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return ftpClient;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|