Browse Source

优化 ftp类

wuxw 6 years ago
parent
commit
8420fd98ea

+ 6 - 5
CommonService/src/main/java/com/java110/common/smo/impl/FileInnerServiceSMOImpl.java

@@ -5,9 +5,7 @@ import com.java110.config.properties.code.Java110Properties;
 import com.java110.core.base.smo.BaseServiceSMO;
 import com.java110.core.smo.file.IFileInnerServiceSMO;
 import com.java110.dto.file.FileDto;
-import com.java110.utils.util.Base64Convert;
-import com.java110.utils.util.BeanConvertUtil;
-import com.java110.utils.util.FtpUpload;
+import com.java110.core.client.FtpUploadTemplate;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RestController;
@@ -25,6 +23,9 @@ public class FileInnerServiceSMOImpl extends BaseServiceSMO implements IFileInne
     @Autowired
     private Java110Properties java110Properties;
 
+    @Autowired
+    private FtpUploadTemplate ftpUploadTemplate;
+
 
     @Override
     public String saveFile(@RequestBody FileDto fileDto) {
@@ -32,7 +33,7 @@ public class FileInnerServiceSMOImpl extends BaseServiceSMO implements IFileInne
         //int saveFileFlag = fileServiceDaoImpl.saveFile(BeanConvertUtil.beanCovertMap(fileDto));
 
 
-        String fileName = FtpUpload.upload(fileDto.getContext(), java110Properties.getFtpServer(),
+        String fileName = ftpUploadTemplate.upload(fileDto.getContext(), java110Properties.getFtpServer(),
                 java110Properties.getFtpPort(), java110Properties.getFtpUserName(),
                 java110Properties.getFtpUserPassword(), java110Properties.getFtpPath());
 
@@ -49,7 +50,7 @@ public class FileInnerServiceSMOImpl extends BaseServiceSMO implements IFileInne
             ftpPath += fileName.substring(0, fileName.lastIndexOf("/")+1);
             fileName = fileName.substring(fileName.lastIndexOf("/")+1, fileName.length());
         }
-        byte[] fileImg = FtpUpload.downFileByte(ftpPath, fileName, java110Properties.getFtpServer(),
+        byte[] fileImg = ftpUploadTemplate.downFileByte(ftpPath, fileName, java110Properties.getFtpServer(),
                 java110Properties.getFtpPort(), java110Properties.getFtpUserName(),
                 java110Properties.getFtpUserPassword());
 

+ 4 - 11
WebService/src/main/java/com/java110/web/smo/file/impl/UploadVedioSMOImpl.java

@@ -4,16 +4,9 @@ import com.alibaba.fastjson.JSONObject;
 import com.java110.config.properties.code.Java110Properties;
 import com.java110.core.component.BaseComponentSMO;
 import com.java110.core.context.IPageData;
-import com.java110.entity.component.ComponentValidateResult;
-import com.java110.utils.constant.PrivilegeCodeConstant;
-import com.java110.utils.constant.ServiceConstant;
-import com.java110.utils.util.Assert;
-import com.java110.utils.util.Base64Convert;
-import com.java110.utils.util.FtpUpload;
-import com.java110.web.smo.file.IAddFileSMO;
+import com.java110.core.client.FtpUploadTemplate;
 import com.java110.web.smo.file.IUploadVedioSMO;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.http.HttpMethod;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
 import org.springframework.stereotype.Service;
@@ -21,7 +14,6 @@ import org.springframework.web.client.RestTemplate;
 import org.springframework.web.multipart.MultipartFile;
 
 import java.io.IOException;
-import java.io.InputStream;
 
 /**
  * 添加小区服务实现类
@@ -35,7 +27,8 @@ public class UploadVedioSMOImpl extends BaseComponentSMO implements IUploadVedio
 
     @Autowired
     private Java110Properties java110Properties;
-
+    @Autowired
+    private FtpUploadTemplate ftpUploadTemplate;
 
     @Override
     public ResponseEntity<Object> upload(IPageData pd, MultipartFile uploadFile) throws IOException {
@@ -45,7 +38,7 @@ public class UploadVedioSMOImpl extends BaseComponentSMO implements IUploadVedio
             throw new IllegalArgumentException("上传文件超过200兆");
         }
 
-        String fileName = FtpUpload.upload(uploadFile, java110Properties.getFtpServer(),
+        String fileName = ftpUploadTemplate.upload(uploadFile, java110Properties.getFtpServer(),
                 java110Properties.getFtpPort(), java110Properties.getFtpUserName(),
                 java110Properties.getFtpUserPassword(), java110Properties.getFtpPath());
         JSONObject outParam = new JSONObject();

+ 28 - 23
java110-utils/src/main/java/com/java110/utils/util/FtpUpload.java

@@ -1,19 +1,22 @@
-package com.java110.utils.util;
+package com.java110.core.client;
 
+import com.java110.utils.util.Base64Convert;
+import com.java110.utils.util.DateUtil;
 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.FTPReply;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Component;
 import org.springframework.web.multipart.MultipartFile;
-import sun.misc.BASE64Decoder;
 
 import java.io.*;
 import java.util.UUID;
 
-public class FtpUpload {
-//    private static Logger logger = LoggerFactory.getLogger(FtpUpload.class);
+@Component
+public class FtpUploadTemplate {
+    private static Logger logger = LoggerFactory.getLogger(FtpUploadTemplate.class);
 
     /*
      * private static String server = "www.datasvisser.cn"; //地址 private static
@@ -21,7 +24,7 @@ public class FtpUpload {
      * private static String userPassword ="MXUsssMjhssE+*=a3C4\\0";//密码
      */
     private static String ftpPath = "uploadFiles"; // 文件上传目录
-    private static FTPClient ftpClient = new FTPClient();
+    private FTPClient ftpClient = null;
     private static String LOCAL_CHARSET = "GBK";
     private static String SERVER_CHARSET = "ISO-8859-1";
     private final static String localpath = "F:/";//下载到F盘下
@@ -35,17 +38,18 @@ public class FtpUpload {
      *图片上传工具方法
      * 默认上传至 img 文件下的当前日期下
      */
-    public static String upload(String imageBase64, String server, int port,
-                                String userName, String userPassword, String ftpPath) {
+    public String upload(String imageBase64, String server, int port,
+                         String userName, String userPassword, String ftpPath) {
         String fileName = "";
         try {
+            ftpClient = new FTPClient();
             // request.setCharacterEncoding("utf-8");
             ftpClient.connect(server, port);
             ftpClient.login(userName, userPassword);
             ftpClient.enterLocalPassiveMode();
             ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
             ftpPath = ftpPath + IMAGE_DEFAULT_PATH + DateUtil.getNowII() + "/";
-            mkDir(ftpPath);// 创建目录
+            mkDir(ftpClient, ftpPath);// 创建目录
             // 设置上传目录 must
             ftpClient.changeWorkingDirectory(ftpPath);
             if (FTPReply.isPositiveCompletion(ftpClient.sendCommand("OPTS UTF8", "ON"))) {// 开启服务器对UTF-8的支持,如果服务器支持就用UTF-8编码,否则就使用本地编码(GBK).
@@ -56,13 +60,13 @@ public class FtpUpload {
             if (imageBase64.contains("data:image/png;base64,")) {
                 imageBase64 = imageBase64.replace("data:image/png;base64,", "");
                 fileName += ".png";
-            }else if(imageBase64.contains("data:image/jpeg;base64,")){
+            } else if (imageBase64.contains("data:image/jpeg;base64,")) {
                 imageBase64 = imageBase64.replace("data:image/jpeg;base64,", "");
                 fileName += ".jpg";
-            }else if(imageBase64.contains("data:image/webp;base64,")){
+            } else if (imageBase64.contains("data:image/webp;base64,")) {
                 imageBase64 = imageBase64.replace("data:image/webp;base64,", "");
                 fileName += ".jpg";
-            }else{
+            } else {
                 fileName += ".jpg";
             }
             FTPFile[] fs = ftpClient.listFiles(fileName);
@@ -82,14 +86,14 @@ public class FtpUpload {
                 throw new IllegalArgumentException("存储文件失败");
             }
         } catch (Exception e) {
-            //logger.error("上传文件失败", e);
+            logger.error("上传文件失败", e);
             throw new IllegalArgumentException("上传文件失败");
         } finally {
             try {
                 ftpClient.disconnect();
             } catch (IOException e) {
                 e.printStackTrace();
-                //logger.error("关闭ftpClient 失败", e);
+                logger.error("关闭ftpClient 失败", e);
             }
         }
         return IMAGE_DEFAULT_PATH + DateUtil.getNowII() + "/" + fileName;
@@ -99,16 +103,17 @@ public class FtpUpload {
     /*
      *文件上传工具方法
      */
-    public static String upload(MultipartFile uploadFile, String server, int port,
-                                String userName, String userPassword, String ftpPath) {
+    public String upload(MultipartFile uploadFile, String server, int port,
+                         String userName, String userPassword, String ftpPath) {
         String fileName = "";
         try {
             // request.setCharacterEncoding("utf-8");
+            ftpClient = new FTPClient();
             ftpClient.connect(server, port);
             ftpClient.login(userName, userPassword);
             ftpClient.enterLocalPassiveMode();
             ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
-            mkDir(ftpPath);// 创建目录
+            mkDir(ftpClient, ftpPath);// 创建目录
             // 设置上传目录 must
             ftpClient.changeWorkingDirectory(ftpPath);
             if (FTPReply.isPositiveCompletion(ftpClient.sendCommand("OPTS UTF8", "ON"))) {// 开启服务器对UTF-8的支持,如果服务器支持就用UTF-8编码,否则就使用本地编码(GBK).
@@ -131,7 +136,7 @@ public class FtpUpload {
                 throw new IllegalArgumentException("存储文件失败");
             }
         } catch (Exception e) {
-           // logger.error("上传文件失败", e);
+            // logger.error("上传文件失败", e);
             throw new IllegalArgumentException("上传文件失败");
         } finally {
             try {
@@ -146,7 +151,7 @@ public class FtpUpload {
     /*
      *文件下载工具方法
      */
-    public static byte[] downFileByte(String remotePath, String fileName, String server, int port, String userName, String userPassword) {
+    public byte[] downFileByte(String remotePath, String fileName, String server, int port, String userName, String userPassword) {
         byte[] return_arraybyte = null;
         try {
             ftpClient.connect(server, port);
@@ -165,20 +170,20 @@ public class FtpUpload {
                 }
                 return_arraybyte = byteOut.toByteArray();
                 byteOut.close();
-                if(ins != null) {
+                if (ins != null) {
                     ins.close();
                 }
             }
         } catch (Exception e) {
             e.printStackTrace();
-            //logger.error("从ftp读取文件失败", e);
+            logger.error("从ftp读取文件失败", e);
         } finally {
-            closeConnect();
+            closeConnect(ftpClient);
         }
         return return_arraybyte;
     }
 
-    public static void closeConnect() {
+    public void closeConnect(FTPClient ftpClient) {
         try {
             ftpClient.disconnect();
         } catch (Exception e) {
@@ -192,7 +197,7 @@ public class FtpUpload {
      * @param ftpPath 需要上传、创建的目录
      * @return
      */
-    public static boolean mkDir(String ftpPath) {
+    public static boolean mkDir(FTPClient ftpClient, String ftpPath) {
         if (!ftpClient.isConnected()) {
             return false;
         }