| src/main/java/org/springblade/modules/FTP/FtpMain.java | ●●●●● patch | view | raw | blame | history | |
| src/main/java/org/springblade/modules/FTP/FtpUploadFile.java | ●●●●● patch | view | raw | blame | history | |
| src/main/java/org/springblade/modules/FTP/FtpUtil.java | ●●●●● patch | view | raw | blame | history | |
| src/main/java/org/springblade/modules/FTP/OutJson.java | ●●●●● patch | view | raw | blame | history | |
| src/main/java/org/springblade/modules/FTP/monitor.java | ●●●●● patch | view | raw | blame | history | |
| src/main/java/org/springblade/modules/information/controller/InformationController.java | ●●●●● patch | view | raw | blame | history |
src/main/java/org/springblade/modules/FTP/FtpMain.java
File was renamed from src/main/java/org/springblade/common/utils/FtpMain.java @@ -1,4 +1,4 @@ package org.springblade.common.utils; package org.springblade.modules.FTP; import java.io.FileNotFoundException; src/main/java/org/springblade/modules/FTP/FtpUploadFile.java
File was renamed from src/main/java/org/springblade/common/utils/FtpUploadFile.java @@ -1,4 +1,4 @@ package org.springblade.common.utils; package org.springblade.modules.FTP; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; src/main/java/org/springblade/modules/FTP/FtpUtil.java
File was renamed from src/main/java/org/springblade/common/utils/FtpUtil.java @@ -1,4 +1,4 @@ package org.springblade.common.utils; package org.springblade.modules.FTP; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -11,13 +11,16 @@ /** * ftp工具类 * * @author lijj */ public class FtpUtil { private final static Log logger = LogFactory.getLog(FtpUtil.class); /** 本地字符编码 */ /** * 本地字符编码 */ private static String LOCAL_CHARSET = "GBK"; // FTP协议里面,规定文件名编码为iso-8859-1 @@ -26,18 +29,10 @@ /** * 获取FTPClient对象 * * @param ftpHost * FTP主机服务器 * * @param ftpPassword * FTP 登录密码 * * @param ftpUserName * FTP登录用户名 * * @param ftpPort * FTP端口 默认为21 * * @param ftpHost FTP主机服务器 * @param ftpPassword FTP 登录密码 * @param ftpUserName FTP登录用户名 * @param ftpPort FTP端口 默认为21 * @return */ public static FTPClient getFTPClient(String ftpHost, int ftpPort, String ftpUserName, String ftpPassword) { @@ -65,19 +60,13 @@ /** * 从FTP服务器下载文件 * * @param ftpHost FTP IP地址 * * @param ftpHost FTP IP地址 * @param ftpUserName FTP 用户名 * * @param ftpPassword FTP用户名密码 * * @param ftpPort FTP端口 * * @param ftpPath FTP服务器中文件所在路径 格式: ftptest/aa * * @param localPath 下载到本地的位置 格式:H:/download * * @param fileName 文件名称 * @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, String ftpPath, String localPath, String fileName) { @@ -101,16 +90,16 @@ InputStream retrieveFileStream = ftpClient.retrieveFileStream(fileNameTemp); // 第一种方式下载文件(推荐) //File localFile = new File(localPath + File.separatorChar + fileName); //OutputStream os = new FileOutputStream(localFile); //ftpClient.retrieveFile(fileName, os); os.close(); //File localFile = new File(localPath + File.separatorChar + fileName); //OutputStream os = new FileOutputStream(localFile); //ftpClient.retrieveFile(fileName, os); os.close(); // 第二种方式下载:将输入流转成字节,再生成文件,这种方式方便将字节数组直接返回给前台jsp页面 byte[] input2byte = input2byte(retrieveFileStream); byte2File(input2byte, localPath, fileName); if(null != retrieveFileStream){ if (null != retrieveFileStream) { retrieveFileStream.close(); } } catch (FileNotFoundException e) { @@ -181,7 +170,7 @@ return result; } if(null != input){ if (null != input) { input.close(); } @@ -293,4 +282,5 @@ } } } } src/main/java/org/springblade/modules/FTP/OutJson.java
File was renamed from src/main/java/org/springblade/common/utils/OutJson.java @@ -1,9 +1,6 @@ package org.springblade.common.utils; package org.springblade.modules.FTP; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStreamWriter; import java.io.Writer; import java.io.*; public class OutJson { /** @@ -138,4 +135,53 @@ } return result.toString(); } /** * 删除文件 * @param str * @return */ public static String stringReplace(String str) { //去掉" "号 String strs= str.replace("\"", ""); return strs ; } /** * 读取json文件并解析 * @return */ public static String TestJson(){ File file = new File("D:\\anbao\\sql.json"); StringBuilder localStrBulider = new StringBuilder(); if(file.isFile() && file.exists()) { try { InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(file), "utf-8"); BufferedReader bufferReader = new BufferedReader(inputStreamReader); String lineStr = null; try { while((lineStr = bufferReader.readLine()) != null) { localStrBulider.append(lineStr); } bufferReader.close(); inputStreamReader.close(); } catch (IOException e) { // TODO Auto-generated catch block System.out.println("file read error!"); e.printStackTrace(); } } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block System.out.println("file catch unsupported encoding!"); e.printStackTrace(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block System.out.println("file not found!"); e.printStackTrace(); } }else { System.out.println("file is not a file or file is not existing!"); } return localStrBulider.toString(); } } src/main/java/org/springblade/modules/FTP/monitor.java
New file @@ -0,0 +1,116 @@ package org.springblade.modules.FTP; import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPReply; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.io.IOException; import java.io.InputStream; @Component public class monitor { //ftp服务器IP地址 private static String ftpHost = "192.168.0.105"; //ftp服务器端口 private static int ftpPort = 21; //ftp服务器用户名 private static String ftpUserName = "yly"; //ftp服务器密码 private static String ftpPassword = "Yly@123"; //ftp服务器路径 private static String ftpPath = "anbao/sql.json"; @Scheduled(cron = "*/5 * * * * ?") public static boolean isFTPFileExist() { FTPClient ftp = new FTPClient(); try { // 连接ftp服务器 ftp.connect(ftpHost, ftpPort); // 登陆 ftp.login(ftpUserName, ftpPassword); // 检验登陆操作的返回码是否正确 if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) { ftp.disconnect(); return false; } ftp.enterLocalActiveMode(); // 设置文件类型为二进制,与ASCII有区别 ftp.setFileType(FTP.BINARY_FILE_TYPE); // 设置编码格式 ftp.setControlEncoding("GBK"); // 提取绝对地址的目录以及文件名 ftpPath = ftpPath.replace("ftp://" + ftpHost + ":" + ftpPort + "/", ""); String dir = ftpPath.substring(0, ftpPath.lastIndexOf("/")); String file = ftpPath.substring(ftpPath.lastIndexOf("/") + 1); // 进入文件所在目录,注意编码格式,以能够正确识别中文目录 ftp.changeWorkingDirectory(new String(dir.getBytes("GBK"), FTP.DEFAULT_CONTROL_ENCODING)); // 检验文件是否存在 InputStream is = ftp.retrieveFileStream(new String(file.getBytes("GBK"), FTP.DEFAULT_CONTROL_ENCODING)); if (is == null || ftp.getReplyCode() == FTPReply.FILE_UNAVAILABLE) { System.out.println(false); return false; } if (is != null) { System.out.println(true); is.close(); ftp.completePendingCommand(); } return true; } catch (Exception e) { e.printStackTrace(); } finally { if (ftp != null) { try { ftp.disconnect(); } catch (IOException e) { e.printStackTrace(); } } } return false; } } src/main/java/org/springblade/modules/information/controller/InformationController.java
@@ -18,15 +18,12 @@ import com.baomidou.mybatisplus.core.metadata.IPage; import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport; import com.google.gson.*; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.AllArgsConstructor; import com.alibaba.fastjson.JSON; import org.springblade.common.cache.DictCache; import org.springblade.common.enums.DictEnum; import org.springblade.common.utils.FtpUtil; import org.springblade.common.utils.OutJson; import org.springblade.core.boot.ctrl.BladeController; import org.springblade.core.cache.utils.CacheUtil; import org.springblade.core.excel.util.ExcelUtil; @@ -34,6 +31,8 @@ import org.springblade.core.mp.support.Query; import org.springblade.core.tool.api.R; import org.springblade.core.tool.support.Kv; import org.springblade.modules.FTP.FtpUtil; import org.springblade.modules.FTP.OutJson; import org.springblade.modules.information.entity.Information; import org.springblade.modules.information.excel.InforExcel; import org.springblade.modules.information.excel.InforImporter; @@ -336,60 +335,27 @@ } public static void main(String[] args) throws Exception { //ftp服务器IP地址 String ftpHost = "192.168.0.105"; //ftp服务器端口 int ftpPort = 21; //ftp服务器用户名 String ftpUserName = "yly"; //ftp服务器密码 String ftpPassword = "Yly@123"; //ftp服务器路径 String ftpPath = ""; //本地路径 String localPath = "D:\\anbao"; //文件名 String fileName = "sql.json"; FtpUtil.downloadFtpFile(ftpHost, ftpUserName, ftpPassword, ftpPort, ftpPath, localPath, fileName); String s = TestJson(); System.out.println(s); } public static String TestJson(){ File file = new File("D:\\anbao\\sql.json"); StringBuilder localStrBulider = new StringBuilder(); if(file.isFile() && file.exists()) { try { InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(file), "utf-8"); BufferedReader bufferReader = new BufferedReader(inputStreamReader); String lineStr = null; try { while((lineStr = bufferReader.readLine()) != null) { localStrBulider.append(lineStr); } bufferReader.close(); inputStreamReader.close(); } catch (IOException e) { // TODO Auto-generated catch block System.out.println("file read error!"); e.printStackTrace(); } } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block System.out.println("file catch unsupported encoding!"); e.printStackTrace(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block System.out.println("file not found!"); e.printStackTrace(); } }else { System.out.println("file is not a file or file is not existing!"); } return localStrBulider.toString(); } // public static void main(String[] args) throws Exception { // //ftp服务器IP地址 // String ftpHost = "192.168.0.105"; // //ftp服务器端口 // int ftpPort = 21; // //ftp服务器用户名 // String ftpUserName = "yly"; // //ftp服务器密码 // String ftpPassword = "Yly@123"; // //ftp服务器路径 // String ftpPath = "anbao/sql.json"; // //本地路径 // String localPath = "D:\\anbao"; // //文件名 // String fileName = "sql.json"; //// FtpUtil.downloadFtpFile(ftpHost, ftpUserName, ftpPassword, ftpPort, ftpPath, localPath, fileName); // String s = OutJson.TestJson(); // System.out.println(s); // //boolean ftpFileExist = FtpUtil.isFTPFileExist(ftpPath, ftpUserName, ftpPassword, ftpHost, ftpPort); // //System.out.println(ftpFileExist); // }