|
|
@@ -0,0 +1,127 @@
|
|
|
+package top.lvzhiqiang.util;
|
|
|
+
|
|
|
+import org.apache.commons.net.ftp.FTP;
|
|
|
+import org.apache.commons.net.ftp.FTPClient;
|
|
|
+import org.apache.commons.net.ftp.FTPReply;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.time.LocalDate;
|
|
|
+import java.util.Random;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 用于上传文件的工具类
|
|
|
+ * https://juejin.cn/post/6894219004673294349<使用nginx和vsftp搭建图片服务器并使用Java上传图片到该图片服务器>
|
|
|
+ */
|
|
|
+public class FtpUtil {
|
|
|
+
|
|
|
+ private static String host;
|
|
|
+ private static int port;
|
|
|
+ private static String userName;
|
|
|
+ private static String passWord;
|
|
|
+ private static String basePath;
|
|
|
+ private static String baseUrl;
|
|
|
+
|
|
|
+ public static String getBaseUrl() {
|
|
|
+ return baseUrl;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void setParams(String host_, String port_, String userName_, String passWord_, String basePath_, String baseUrl_) {
|
|
|
+ host = host_;
|
|
|
+ port = Integer.parseInt(port_);
|
|
|
+ userName = userName_;
|
|
|
+ passWord = passWord_;
|
|
|
+ basePath = basePath_;
|
|
|
+ baseUrl = baseUrl_;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static boolean uploadFile(String filename, InputStream input) {
|
|
|
+ return uploadFile(host, port, userName, passWord, basePath, filename, input);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 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 input 输入流
|
|
|
+ * @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();
|
|
|
+ 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.enterLocalPassiveMode();
|
|
|
+ // 设置编码格式为utf-8
|
|
|
+ ftpClient.setControlEncoding("UTF-8");
|
|
|
+ // 设置上传文件的类型为二进制类型
|
|
|
+ ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
|
|
|
+ // 设置存储图片的文件夹
|
|
|
+ String workingDirectory = basePath.concat(LocalDate.now().format(DateUtils.dateFormatter_));
|
|
|
+ String[] split = workingDirectory.split("/");
|
|
|
+ for (String s : split) {
|
|
|
+ if (StringUtils.isEmpty(s)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (!ftpClient.changeWorkingDirectory(s)) {
|
|
|
+ ftpClient.makeDirectory(s);
|
|
|
+ ftpClient.changeWorkingDirectory(s);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 上传文件
|
|
|
+ if (!ftpClient.storeFile(filename, input)) {
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ input.close();
|
|
|
+
|
|
|
+ result = true;
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ if (ftpClient.isConnected()) {
|
|
|
+ try {
|
|
|
+ ftpClient.logout();
|
|
|
+ ftpClient.disconnect();
|
|
|
+ } catch (IOException ioe) {
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成随机图片名
|
|
|
+ */
|
|
|
+ public static String genImageName() {
|
|
|
+ //取当前时间的长整形值包含毫秒
|
|
|
+ long millis = System.currentTimeMillis();
|
|
|
+ //加上三位随机数
|
|
|
+ Random random = new Random();
|
|
|
+ int end3 = random.nextInt(999);
|
|
|
+ //如果不足三位前面补0
|
|
|
+ String str = millis + String.format("%03d", end3);
|
|
|
+
|
|
|
+ return str;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ System.out.println(genImageName());
|
|
|
+ }
|
|
|
+}
|