Administrator
2022-06-16 2ebca060a47784db76369a4aa84f63293da9be33
src/main/java/org/springblade/modules/FTP/FtpUtil.java
@@ -6,13 +6,16 @@
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.net.SocketException;
import java.util.Date;
import java.util.Map;
import static org.springblade.common.constant.FtpConstant.*;
import static org.springblade.common.constant.FtpConstant.ftpPath;
import net.lingala.zip4j.core.ZipFile;
import static org.springblade.common.config.FtpConfig.*;
/**
 * ftp工具类
@@ -35,22 +38,26 @@
    * 获取FTPClient对象
    *
    * @param ftpHost     FTP主机服务器
    * @param ftpPassword FTP 登录密码
    * @param ftpUserName FTP登录用户名
    * @param ftpPasswordOut FTP 登录密码
    * @param ftpUserNameOut FTP登录用户名
    * @param ftpPort     FTP端口 默认为21
    * @return
    */
   public static FTPClient getFTPClient(String ftpHost, int ftpPort, String ftpUserName, String ftpPassword) {
   public static FTPClient getFTPClient(String ftpHost, int ftpPort, String ftpUserNameOut, String ftpPasswordOut) {
      FTPClient ftpClient = null;
      try {
         ftpClient = new FTPClient();
         //设置超时时间
         ftpClient.setDataTimeout(10000);
         //连接超时为60秒
         ftpClient.setConnectTimeout(10000);
         ftpClient.connect(ftpHost, ftpPort);// 连接FTP服务器
         ftpClient.login(ftpUserName, ftpPassword);// 登陆FTP服务器
         ftpClient.login(ftpUserNameOut, ftpPasswordOut);// 登陆FTP服务器
         if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
            logger.info("未连接到FTP,用户名或密码错误。");
            ftpClient.disconnect();
         } else {
            logger.info("FTP连接成功。");
//            logger.info("FTP连接成功。");
         }
      } catch (SocketException e) {
         e.printStackTrace();
@@ -66,20 +73,20 @@
    * 从FTP服务器下载文件
    *
    * @param ftpHost     FTP IP地址
    * @param ftpUserName FTP 用户名
    * @param ftpPassword FTP用户名密码
    * @param ftpUserNameOut FTP 用户名
    * @param ftpPasswordOut FTP用户名密码
    * @param ftpPort     FTP端口
    * @param ftpPath     FTP服务器中文件所在路径 格式: ftptest/aa
    * @param localPath   下载到本地的位置 格式:H:/download
    * @param fileName    文件名称
    */
   public static void downloadFtpFile(String ftpHost, String ftpUserName, String ftpPassword, int ftpPort,
   public static void downloadFtpFile(String ftpHost, String ftpUserNameOut, String ftpPasswordOut, int ftpPort,
                              String ftpPath, String localPath, String fileName) {
      FTPClient ftpClient = null;
      try {
         ftpClient = getFTPClient(ftpHost, ftpPort, ftpUserName, ftpPassword);
         ftpClient = getFTPClient(ftpHost, ftpPort, ftpUserNameOut, ftpPasswordOut);
         // 设置上传文件的类型为二进制类型
         if (FTPReply.isPositiveCompletion(ftpClient.sendCommand("OPTS UTF8", "ON"))) {// 开启服务器对UTF-8的支持,如果服务器支持就用UTF-8编码,否则就使用本地编码(GBK).
            LOCAL_CHARSET = "UTF-8";
@@ -132,13 +139,13 @@
   }
   public static boolean uploadFile(String ftpHost, int ftpPort, String ftpUserName, String ftpPassword,
   public static boolean uploadFile(String ftpHost, int ftpPort, String ftpUserNameOut, String ftpPasswordOut,
                            String basePath, String filePath, String filename, InputStream input) {
      boolean result = false;
      FTPClient ftpClient = null;
      try {
         int reply;
         ftpClient = getFTPClient(ftpHost, ftpPort, ftpUserName, ftpPassword);
         ftpClient = getFTPClient(ftpHost, ftpPort, ftpUserNameOut, ftpPasswordOut);
         reply = ftpClient.getReplyCode();
         if (!FTPReply.isPositiveCompletion(reply)) {
            ftpClient.disconnect();
@@ -197,12 +204,12 @@
   }
   public static boolean deleteFile(String ftpHost, int ftpPort, String ftpUserName, String ftpPassword, String pathname,
   public static boolean deleteFile(String ftpHost, int ftpPort, String ftpUserNameOut, String ftpPasswordOut, String pathname,
                            String filename) {
      boolean flag = false;
      FTPClient ftpClient = new FTPClient();
      try {
         ftpClient = getFTPClient(ftpHost, ftpPort, ftpUserName, ftpPassword);
         ftpClient = getFTPClient(ftpHost, ftpPort, ftpUserNameOut, ftpPasswordOut);
         // 验证FTP服务器是否登录成功
         int replyCode = ftpClient.getReplyCode();
         if (!FTPReply.isPositiveCompletion(replyCode)) {
@@ -287,21 +294,202 @@
         }
      }
   }
   /**
    *
    * 解压zip 包
    * @author panchaoyuan
    * @param srcFile    Unzipped file
    * @param destDirPath   Unzipped destination folder
    * @throws RuntimeException
    * @throws IOException
    */
   public static void unZip(MultipartFile srcFile, String destDirPath, String savePath) throws RuntimeException, IOException {
      File file = null;
      InputStream ins = srcFile.getInputStream();
      String savaPaths = savePath+srcFile.getOriginalFilename();
      file=new File(savaPaths);
      inputStreamToFile(ins, file);
      if (!file.exists()) {
         throw new RuntimeException(file.getPath() + ",file is not found");
      }
      ZipFile zipFile = null;
      try {
         zipFile = new ZipFile(file);
//         zipFile.setFileNameCharset("utf-8");
         zipFile.setFileNameCharset("gbk");
         //解压到 destDirPath
         zipFile.extractAll(destDirPath);
      }catch(Exception e) {
         throw new RuntimeException("unzip error from FileUtil", e);
      }
   }
   /**
    * 输入流转换为文件
    * @author panchaoyuan
    * @return
    */
   private static void inputStreamToFile(InputStream ins, File file) {
      try {
         OutputStream os = new FileOutputStream(file);
         int bytesRead = 0;
         byte[] buffer = new byte[8192];
         while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
            os.write(buffer, 0, bytesRead);
         }
         os.close();
         ins.close();
      }catch(Exception e) {
         e.printStackTrace();
      }
   }
   /**
    * 执行sql 上传
    * @param s1 sql
    * @param obj sql
    */
   public static void sqlFileUpload(String s1){
      String json1 = JSON.toJSONString(s1);
   public static void sqlFileUpload(String obj){
      String json1 = JSON.toJSONString(obj);
      String response1 = String.valueOf((new Date()).getTime());
      OutJson.createJsonFile(json1, localPath, "n"+response1);
      OutJson.createJsonFile(json1, localPath, "nq"+response1);
      FileInputStream in1 = null;
      try {
         in1 = new FileInputStream(new File(localPath + "n"+response1+".json"));
         in1 = new FileInputStream(new File(localPath + "nq"+response1+".json"));
      } catch (FileNotFoundException e) {
         e.printStackTrace();
      }
      FtpUtil.uploadFile(ftpHost_dev, ftpPort, ftpUserName, ftpPassword, ftpPath, "/",  "n"+response1+".json", in1);
      FtpUtil.uploadFile(ftpHost, ftpPort, ftpUserNameOut, ftpPasswordOut, ftpPath, "/",  "nq"+response1+".json", in1);
      MysqlCenlint.deletess("nq"+response1+".json");
   }
   /**
    * 执行sql 上传,推送审查结果,json 命名用户id相同的文件名称一致
    * @param obj sql
    * @param id 用户id
    */
   public static void sqlFileUploadUserExamine(String obj,String id){
      String json1 = JSON.toJSONString(obj);
      OutJson.createJsonFile(json1, localPath, "nq"+id);
      FileInputStream in1 = null;
      try {
         in1 = new FileInputStream(new File(localPath + "nq"+id+".json"));
      } catch (FileNotFoundException e) {
         e.printStackTrace();
      }
      FtpUtil.uploadFile(ftpHost, ftpPort, ftpUserNameOut, ftpPasswordOut, ftpPath, "/",  "nq"+id+".json", in1);
      MysqlCenlint.deletess("nq"+id+".json");
   }
   /**
    * 执行 对象数据(用户,报名对象信息等) 上传
    * @param obj 对象信息
    * @param uuid
    */
   public static void objectFileUpload(Object obj,String uuid){
      String json1 = JSON.toJSONString(obj);
      OutJson.createJsonFile(json1, localPath, "ni"+uuid);
      FileInputStream in1 = null;
      try {
         in1 = new FileInputStream(new File(localPath + "ni"+uuid+".json"));
      } catch (FileNotFoundException e) {
         e.printStackTrace();
      }
      FtpUtil.uploadFile(ftpHost, ftpPort, ftpUserNameOut, ftpPasswordOut, ftpPath, "/",  "ni"+uuid+".json", in1);
      System.out.println("用户新增,向外网推送数据结束.................");
      MysqlCenlint.deletess("ni"+uuid+".json");
   }
   /**
    * 执行 对象数据(用户,报名对象信息等) 上传
    * @param obj 对象信息
    */
   public static void objectFileUploadList(Object obj){
      String json1 = JSON.toJSONString(obj);
      String response1 = String.valueOf((new Date()).getTime());
      OutJson.createJsonFile(json1, localPath, "nl"+response1);
      FileInputStream in1 = null;
      try {
         in1 = new FileInputStream(new File(localPath + "nl"+response1+".json"));
      } catch (FileNotFoundException e) {
         e.printStackTrace();
      }
      FtpUtil.uploadFile(ftpHost, ftpPort, ftpUserNameOut, ftpPasswordOut, ftpPath, "/",  "nl"+response1+".json", in1);
      MysqlCenlint.deletess("nl"+response1+".json");
   }
   /**
    * 执行 对象数据(实操成绩导入反馈) 上传
    * @param obj 对象信息
    */
   public static void objectFileUploadExamScore(Object obj){
      String json1 = JSON.toJSONString(obj);
      String response1 = String.valueOf((new Date()).getTime());
      OutJson.createJsonFile(json1, localPath, "ne"+response1);
      FileInputStream in1 = null;
      try {
         in1 = new FileInputStream(new File(localPath + "ne"+response1+".json"));
      } catch (FileNotFoundException e) {
         e.printStackTrace();
      }
      FtpUtil.uploadFile(ftpHost, ftpPort, ftpUserNameOut, ftpPasswordOut, ftpPath, "/",  "ne"+response1+".json", in1);
      MysqlCenlint.deletess("ne"+response1+".json");
   }
   /**
    * 返回报名信息结果
    * @param obj
    */
   public static void objectFileUploadListTrain(Object obj){
      String json1 = JSON.toJSONString(obj);
      String response1 = String.valueOf((new Date()).getTime());
      OutJson.createJsonFile(json1, localPath, "nt"+response1);
      FileInputStream in1 = null;
      try {
         in1 = new FileInputStream(new File(localPath + "nt"+response1+".json"));
      } catch (FileNotFoundException e) {
         e.printStackTrace();
      }
      FtpUtil.uploadFile(ftpHost, ftpPort, ftpUserNameOut, ftpPasswordOut, ftpPath, "/",  "nt"+response1+".json", in1);
      MysqlCenlint.deletess("nt"+response1+".json");
   }
   /**
    * 向外网发送保安员证编号位数数据
    * @param obj
    */
   public static void objectFileUploadSecurityNumberCount(Object obj){
      String json1 = JSON.toJSONString(obj);
      String response1 = String.valueOf((new Date()).getTime());
      OutJson.createJsonFile(json1, localPath, "ns"+response1);
      FileInputStream in1 = null;
      try {
         in1 = new FileInputStream(new File(localPath + "ns"+response1+".json"));
      } catch (FileNotFoundException e) {
         e.printStackTrace();
      }
      FtpUtil.uploadFile(ftpHost, ftpPort, ftpUserNameOut, ftpPasswordOut, ftpPath, "/",  "ns"+response1+".json", in1);
      MysqlCenlint.deletess("ns"+response1+".json");
   }
   /**
    * 管理员新增保安员并生成保安员证编号
    * @param obj
    */
   public static void objectFileUploadPaperList(Object obj) {
      String json1 = JSON.toJSONString(obj);
      String response1 = String.valueOf((new Date()).getTime());
      OutJson.createJsonFile(json1, localPath, "np"+response1);
      FileInputStream in1 = null;
      try {
         in1 = new FileInputStream(new File(localPath + "np"+response1+".json"));
      } catch (FileNotFoundException e) {
         e.printStackTrace();
      }
      FtpUtil.uploadFile(ftpHost, ftpPort, ftpUserNameOut, ftpPasswordOut, ftpPath, "/",  "np"+response1+".json", in1);
      MysqlCenlint.deletess("np"+response1+".json");
   }
}