chenr 3 maanden geleden
bovenliggende
commit
de6d9f7278

+ 377 - 0
blade-service/blade-manager/src/main/java/org/springblade/manager/utils/SignFtpUtil.java

@@ -0,0 +1,377 @@
+package org.springblade.manager.utils;
+
+
+import com.mixsmart.utils.StringUtils;
+import lombok.AllArgsConstructor;
+import org.apache.commons.net.ftp.FTP;
+import org.apache.commons.net.ftp.FTPClient;
+import org.springblade.common.utils.CommonUtil;
+import org.springblade.manager.controller.ExcelTabController;
+import org.springblade.manager.entity.ExcelTab;
+import org.springblade.manager.entity.WbsTreePrivate;
+import org.springframework.jdbc.core.BeanPropertyRowMapper;
+import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.mock.web.MockMultipartFile;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Component;
+import org.springframework.web.bind.annotation.RestController;
+
+import javax.annotation.Resource;
+import java.io.*;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.StandardCopyOption;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Objects;
+
+@Component
+public class SignFtpUtil {
+
+    // -外网
+    private static String SERVER = "219.151.181.73";
+    private static final int PORT = 21 ;
+    private static final String USER = "chenran";
+    private static final String PASS = "123456";
+    private static final int CONNECT_TIMEOUT = 9000000; // 30秒
+    private static final int DATA_TIMEOUT = 9000000;
+    private static final String PATH = "/mnt/sdc/Users/hongchuangyanfa/Desktop";
+    @Resource
+    private  JdbcTemplate jdbcTemplate;
+
+    /**
+     *  获取ftp 客户端
+     * @return
+     */
+    public static FTPClient getFTPClinet() {
+        FTPClient ftp = new FTPClient();
+        try {
+            ftp.setControlEncoding("UTF-8");
+            // 设置连接超时时间
+            ftp.setConnectTimeout(CONNECT_TIMEOUT);
+            // 设置数据传输超时时间
+            ftp.setDataTimeout(DATA_TIMEOUT);
+            // 连接到FTP服务器
+            //String sys_isonline = ParamCache.getValue(CommonConstant.SYS_ISONLINE);
+            System.out.println("ftp上传ip地址:"+SERVER);
+            ftp.connect(SERVER, PORT);
+            ftp.setControlEncoding("UTF-8");
+            ftp.enterLocalPassiveMode();
+            ftp.setFileType(FTP.BINARY_FILE_TYPE);
+            ftp.enterLocalPassiveMode();//被动模式
+            //登录
+            boolean loginS = ftp.login(USER, PASS);
+            if (!loginS) {
+                return null;
+            } else {
+                return ftp;
+            }
+        } catch (IOException e) {
+            System.out.println("ftp连接失败");
+            return null;
+        }
+    }
+
+    /**
+     *  上传
+     * @return
+     */
+    public static int uploadFile(String localFilePath, String remoteFilePath) {
+        // 设置连接超时时间
+        FTPClient ftpClinet = SignFtpUtil.getFTPClinet();
+        try {
+            // 检查本地文件是否存在
+            File localFile = new File(localFilePath);
+            if (!localFile.exists()) {
+                return 2;
+            }
+            // 上传文件
+            FileInputStream inputStream = new FileInputStream(localFile);
+            try {
+                boolean done = ftpClinet.storeFile(remoteFilePath, inputStream);
+                if (done) {
+                    return 1;
+                } else {
+                    return 3;
+                }
+            } finally {
+                inputStream.close();
+            }
+
+        } catch (IOException e) {
+            e.printStackTrace();
+            return 4;
+        } finally {
+            try {
+                if (ftpClinet.isConnected()) {
+                    ftpClinet.logout();
+                    ftpClinet.disconnect();
+                }
+            } catch (IOException ex) {
+                ex.printStackTrace();
+                return 5;
+            }
+        }
+    }
+
+    /**
+     *  上传
+     * @return
+     */
+    public static int uploadFile(String remoteFilePath, InputStream inputStream) {
+        // 设置连接超时时间
+        FTPClient ftpClinet = SignFtpUtil.getFTPClinet();
+        try {
+            try {
+                System.out.println("上传文件中.....文件路径:" + remoteFilePath);
+                boolean done = ftpClinet.storeFile(remoteFilePath, inputStream);
+                if (done) {
+                    return 1;
+                } else {
+                    return 3;
+                }
+            } finally {
+                inputStream.close();
+            }
+
+        } catch (IOException e) {
+            e.printStackTrace();
+            return 4;
+        } finally {
+            try {
+                if (ftpClinet.isConnected()) {
+                    ftpClinet.logout();
+                    ftpClinet.disconnect();
+                }
+            } catch (IOException ex) {
+                ex.printStackTrace();
+                return 5;
+            }
+        }
+    }
+
+    /**
+     *  删除FTP文件
+     * @return
+     */
+    public static int FTPDeleteDir(String remoteFilePath) {
+        FTPClient ftpClinet = SignFtpUtil.getFTPClinet();
+        try {
+            if (ftpClinet.deleteFile(remoteFilePath)) {
+                System.out.println(remoteFilePath+"-s");
+                return 1;
+            } else {
+                System.out.println(remoteFilePath+"-f");
+                return 2;
+            }
+        } catch (Exception e) {
+            System.out.println(e+"-f");
+            e.printStackTrace();
+            return 2;
+        } finally {
+            try {
+                if (ftpClinet.isConnected()) {
+                    ftpClinet.logout();
+                    ftpClinet.disconnect();
+                }
+                return 1;
+            } catch (IOException ex) {
+                ex.printStackTrace();
+                return 5;
+            }
+        }
+    }
+    /**
+     *  创建文件路径
+     * @return
+     */
+    public static int FTPCreateDir(String remoteFilePath) {
+        FTPClient ftpClinet = SignFtpUtil.getFTPClinet();
+        try {
+            if (ftpClinet.makeDirectory(remoteFilePath)) {
+                return 1;
+            } else {
+                return 2;
+            }
+        } catch (Exception e) {
+            e.printStackTrace();
+            return 2;
+        } finally {
+            try {
+                if (ftpClinet.isConnected()) {
+                    ftpClinet.logout();
+                    ftpClinet.disconnect();
+                }
+                return 1;
+            } catch (IOException ex) {
+                ex.printStackTrace();
+                return 5;
+            }
+        }
+    }
+
+
+    /**
+     *  下载
+     * @return
+     */
+    public static int downloadFile(String localFilePath, String remoteFilePath) {
+        // 设置连接超时时间
+        FTPClient ftpClinet = SignFtpUtil.getFTPClinet();
+        try {
+            // 上传文件
+            System.out.println("下载文件中.....路径:" + remoteFilePath);
+            OutputStream outputStream = new FileOutputStream(new File(localFilePath));
+            try {
+                boolean done = ftpClinet.retrieveFile(remoteFilePath, outputStream);
+                if (done) {
+                    return 1;
+                } else {
+                    return 3;
+                }
+            } finally {
+                outputStream.close();
+            }
+        } catch (IOException e) {
+            e.printStackTrace();
+            return 4;
+        } finally {
+            try {
+                if (ftpClinet.isConnected()) {
+                    ftpClinet.logout();
+                    ftpClinet.disconnect();
+                }
+            } catch (IOException ex) {
+                ex.printStackTrace();
+                return 5;
+            }
+        }
+    }
+
+    public void html() throws IOException {
+        String sql="SELECT p_key_id,project_id,html_url from m_wbs_tree_private where type=2 and is_deleted=0 and html_url NOT LIKE '%privateUrlCopy%'";
+        List<WbsTreePrivate> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(WbsTreePrivate.class));
+        int i=list.size();
+        for (WbsTreePrivate w : list) {
+            i--;
+            //数据库原始html路径
+            String htmlUrl = w.getHtmlUrl();
+            if(StringUtils.isNotEmpty(htmlUrl)){
+                //截取出的要下载的html
+                String DownLoadhtmlUrl=htmlUrl.substring(htmlUrl.lastIndexOf("/"));
+                String projectPath="E:\\html\\"+w.getProjectId();
+                File folder = new File(projectPath);
+                if(!folder.exists()){
+                    folder.mkdir();
+                }
+                //本地下载的路径
+                String localPath=projectPath+"\\"+htmlUrl.substring(htmlUrl.lastIndexOf("/")+1);
+                File folder1 = new File(localPath);
+                if(!folder1.exists()){
+                    //下载文件
+                    downloadFile(localPath,"/privateUrl"+DownLoadhtmlUrl);
+                }
+                File localFile = new File(localPath);
+                if (localFile.exists()) {
+                    String sqlPath="/mnt/sdc/Users/hongchuangyanfa/Desktop"+"/privateUrlCopy"+"/"+w.getProjectId()+DownLoadhtmlUrl;
+                   String update="update m_wbs_tree_private set html_url='"+sqlPath+"' where p_key_id="+w.getPKeyId();
+                   jdbcTemplate.update(update);
+                }
+            }
+            System.out.println("还有"+i+"条");
+        }
+    }
+
+       public void htm183l() throws Exception {
+           String sql="SELECT p_key_id,project_id,html_url from m_wbs_tree_private where type=2 and is_deleted=0 and html_url NOT LIKE '%privateUrlCopy%'";
+           List<WbsTreePrivate> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(WbsTreePrivate.class));
+           int i=list.size();
+           for (WbsTreePrivate w : list) {
+               i--;
+               String localUrl="E:\\html183\\"+w.getProjectId();
+               String htmlUrl = w.getHtmlUrl();
+               String htmlName=htmlUrl.substring(htmlUrl.lastIndexOf("/"));
+                InputStream inputStreamByUrl = FileUtils.getInputStreamByUrl(htmlUrl);
+                if(inputStreamByUrl!=null){
+                    File folder = new File(localUrl);
+                    if (!folder.exists()) {
+                        folder.mkdir();
+                    }
+                    File localFile = new File(localUrl+htmlName);
+                    if(!localFile.exists()){
+                        FileUtils.saveFile(inputStreamByUrl, localUrl+htmlName);
+                    }
+                    if(localFile.exists()){
+                        String sqlPath="/home/www/wwwroot/Users/hongchuangyanfa/Desktop/privateUrlCopy/"+w.getProjectId()+htmlName;
+                        String update="update m_wbs_tree_private set html_url='"+sqlPath+"' where p_key_id="+w.getPKeyId();
+                        jdbcTemplate.update(update);
+                    }
+                }
+               System.out.println("还有"+i+"条");
+           }
+        }
+
+        public void checkHtml() throws Exception {
+            StringBuilder result=new StringBuilder("");
+            String sql = "Select * from m_excel_tab where parent_id=0 and is_deleted=0";
+            List<ExcelTab> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(ExcelTab.class));
+            String excelPath="E\\excel\\";
+            for (ExcelTab excelTab : query) {
+                String sql1 = "Select * from m_excel_tab where parent_id=" + excelTab.getId() + " and is_deleted=0";
+                List<ExcelTab> query1 = jdbcTemplate.query(sql1, new BeanPropertyRowMapper<>(ExcelTab.class));
+                for (ExcelTab tab : query1) {
+                    String sql2 = "Select * from m_excel_tab where parent_id=" + tab.getId() + " and is_deleted=0";
+                    List<ExcelTab> query2 = jdbcTemplate.query(sql2, new BeanPropertyRowMapper<>(ExcelTab.class));
+
+                    for (ExcelTab excelTab1 : query2) {
+                        if(!isExist(excelTab1.getHtmlUrl())){
+                            if(excelTab1.getFileUrl() != null&&excelTab1.getFileUrl().endsWith(".xlsx") || excelTab1.getFileUrl().endsWith(".xls")){
+                                long resourceLength = CommonUtil.getResourceLength(excelTab1.getFileUrl());
+                                if(resourceLength>=500){
+                                    //先下载这个文件,再上传
+                                    String fileUrl = excelTab1.getFileUrl();
+                                    String url = fileUrl.substring(fileUrl.indexOf("Desktop") + "Desktop".length() + 1);
+                                    String localPath=excelPath+url;
+                                    downloadFile(localPath, url);
+                                    Path path = Paths.get(localPath);
+                                    byte[] content = Files.readAllBytes(path);
+                                    MockMultipartFile file = new MockMultipartFile("file", excelTab1.getName(), "application/vnd.ms-excel", content);
+
+                                }else {
+                                    result.append(excelTab.getName() + "--" + tab.getName() + "--" + excelTab1.getName()+"\n");
+                                }
+                            }
+                        }
+                    }
+                    System.out.println(result);
+                }
+            }
+        }
+
+        public Boolean isExist(String path){
+            String server = SERVER;
+            int port = PORT;
+            String user = USER;
+            String pass = PASS;
+            FTPClient ftpClient = new FTPClient();
+            try {
+                ftpClient.connect(server, port);
+                ftpClient.login(user, pass);
+                // 尝试获取文件状态
+                try {
+                    if (ftpClient.getStatus(path) != null) {
+                        return true;
+                    }
+                } catch (IOException e) {
+                    return false;
+                }
+
+                ftpClient.logout();
+                ftpClient.disconnect();
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+            return false;
+        }
+}