智慧保安后台管理项目备份
zhongrj
2024-05-24 b5960d1968e007b91d4d33dd7cbb74f1b566f2c1
src/main/java/org/springblade/modules/FTP/FtpUtil.java
@@ -1,13 +1,19 @@
package org.springblade.modules.FTP;
import com.alibaba.fastjson.JSON;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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 net.lingala.zip4j.core.ZipFile;
import static org.springblade.common.config.FtpConfig.*;
/**
 * ftp工具类
@@ -282,5 +288,74 @@
         }
      }
   }
   /**
    * 执行sql 上传
    * @param s1 sql
    */
   public static void sqlFileUpload(String s1){
      String json1 = JSON.toJSONString(s1);
      String response1 = String.valueOf((new Date()).getTime());
      OutJson.createJsonFile(json1, localPath, "n"+response1);
      FileInputStream in1 = null;
      try {
         in1 = new FileInputStream(new File(localPath + "n"+response1+".json"));
      } catch (FileNotFoundException e) {
         e.printStackTrace();
      }
      FtpUtil.uploadFile(ftpHost, ftpPort, ftpUserName, ftpPassword, ftpPath, "/",  "n"+response1+".json", in1);
   }
   /**
    *
    * 解压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();
      }
   }
}