From ad6dfb8323ab40ca8fe4fed94722c755d89c3203 Mon Sep 17 00:00:00 2001 From: jiangping <jp@doumee.com> Date: 星期三, 28 二月 2024 15:14:47 +0800 Subject: [PATCH] 整理 --- server/dmvisit_service/src/main/java/com/doumee/core/utils/FtpUtil.java | 83 +++++++++++++++++++++++++++-------------- 1 files changed, 55 insertions(+), 28 deletions(-) diff --git a/server/dmvisit_service/src/main/java/com/doumee/core/utils/FtpUtil.java b/server/dmvisit_service/src/main/java/com/doumee/core/utils/FtpUtil.java index f158543..a62bf46 100644 --- a/server/dmvisit_service/src/main/java/com/doumee/core/utils/FtpUtil.java +++ b/server/dmvisit_service/src/main/java/com/doumee/core/utils/FtpUtil.java @@ -29,6 +29,10 @@ @Slf4j public class FtpUtil { public FTPClient ftpClient = new FTPClient(); + public static String hostname; + public static int port; + public static String username; + public static String password; public FtpUtil() { // 璁剧疆灏嗚繃绋嬩腑浣跨敤鍒扮殑鍛戒护杈撳嚭鍒版帶鍒跺彴 @@ -39,26 +43,22 @@ public FtpUtil(String hostname, int port, String username, String password) throws IOException { // 璁剧疆灏嗚繃绋嬩腑浣跨敤鍒扮殑鍛戒护杈撳嚭鍒版帶鍒跺彴 - connect(hostname, port, username, password); + FtpUtil.hostname =hostname; + FtpUtil.port =port; + FtpUtil.username =username; + FtpUtil.password =password; + connect(); } /** */ /** * 杩炴帴鍒癋TP鏈嶅姟鍣� * - * @param hostname - * 涓绘満鍚� - * @param port - * 绔彛 - * @param username - * 鐢ㄦ埛鍚� - * @param password * 瀵嗙爜 * @return 鏄惁杩炴帴鎴愬姛 * @throws IOException */ - public boolean connect(String hostname, int port, String username, - String password) throws IOException { + public boolean connect() throws IOException { ftpClient.connect(hostname, port); ftpClient.setControlEncoding("GBK"); if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) { @@ -233,6 +233,39 @@ } return result; } + public boolean uploadInputstream(InputStream inputStream, String remote) { + // 璁剧疆PassiveMode浼犺緭 + try { + ftpClient.enterLocalPassiveMode(); + + // 璁剧疆浠ヤ簩杩涘埗娴佺殑鏂瑰紡浼犺緭 + ftpClient.setFileType(FTP.BINARY_FILE_TYPE); + ftpClient.setControlEncoding("GBK"); + // 瀵硅繙绋嬬洰褰曠殑澶勭悊 + String remoteFileName = remote; + if (remote.contains("/")) { + remoteFileName = remote.substring(remote.lastIndexOf("/") + 1); + // 鍒涘缓鏈嶅姟鍣ㄨ繙绋嬬洰褰曠粨鏋勶紝鍒涘缓澶辫触鐩存帴杩斿洖 + if (StringUtils.equals(CreateDirecroty(remote, ftpClient), "2")) { + log.error("鍒涘缓ftp鐩綍澶辫触======================="+remote); + return false; + } + } + boolean result = ftpClient.storeFile(remoteFileName, inputStream); + inputStream.close(); + ftpClient.logout(); + if(result){ + log.info("涓婁紶鏂囦欢鎴愬姛======================"+remote); + }else{ + log.error("涓婁紶鏂囦欢澶辫触======================="+remote); + } + return result; + }catch (Exception e){ + e.printStackTrace(); + log.error("涓婁紶鏂囦欢澶辫触======================="+remote); + } + return false; + } public int getNumFromStr(String str,char searchstr) { int count = 0; char[] charArray = str.toCharArray(); @@ -265,8 +298,8 @@ String result; // 瀵硅繙绋嬬洰褰曠殑澶勭悊 String remoteFileName = remote ; - if (remote.contains(File.separator)) { - remoteFileName = remote.substring(remote.lastIndexOf(File.separator) + 1); + if (remote.contains("/")) { + remoteFileName = remote.substring(remote.lastIndexOf("/") + 1); // 鍒涘缓鏈嶅姟鍣ㄨ繙绋嬬洰褰曠粨鏋勶紝鍒涘缓澶辫触鐩存帴杩斿洖 if (StringUtils.equals(CreateDirecroty(remote, ftpClient), "2")) { return "2"; @@ -339,20 +372,20 @@ throws IOException { String status = "1"; // UploadStatus status = UploadStatus.Create_Directory_Success; - String directory = remote.substring(0, remote.lastIndexOf(File.separator) + 1); - if (!directory.equalsIgnoreCase(File.separator) + String directory = remote.substring(0, remote.lastIndexOf("/") + 1); + if (!directory.equalsIgnoreCase("/") && !ftpClient.changeWorkingDirectory(new String(directory .getBytes("GBK"), "iso-8859-1"))) { // 濡傛灉杩滅▼鐩綍涓嶅瓨鍦紝鍒欓�掑綊鍒涘缓杩滅▼鏈嶅姟鍣ㄧ洰褰� int start = 0; int end = 0; - if (directory.startsWith(File.separator)) { + if (directory.startsWith("/")) { start = 1; } else { start = 0; } end = directory - .indexOf(File.separator, start); + .indexOf("/", start); while (true) { String subDirectory = new String(remote.substring(start, end) .getBytes("GBK"), "iso-8859-1"); @@ -367,7 +400,7 @@ } start = end + 1; - end = directory.indexOf(File.separator, + end = directory.indexOf("/", start); // 妫�鏌ユ墍鏈夌洰褰曟槸鍚﹀垱寤哄畬姣� @@ -508,7 +541,7 @@ public static void main(String[] args) throws Exception { try { FtpUtil ftpUtil = new FtpUtil("175.27.187.84", 21, "ftpuser","doumee168" ); - ftpUtil.uploadOnlineFile("https://profile-avatar.csdnimg.cn/default.jpg!1", "test"+File.separator, UUID.randomUUID().toString()+"test.jpg"); + ftpUtil.uploadOnlineFile("https://profile-avatar.csdnimg.cn/default.jpg!1", "test"+"/", UUID.randomUUID().toString()+"test.jpg"); /*FtpUtil myFtp = new FtpUtil("106.15.54.228", 21, "ftptlg", "tlg168.com"); System.out.println(myFtp.upload("D:\\devices.sql", "/1111/devices.sql")); @@ -598,9 +631,7 @@ String remoteDirectoryPath) throws Exception { File src = new File(localDirectory); try { - remoteDirectoryPath = remoteDirectoryPath + File.separator; - -// System.out.println(remoteDirectoryPath+":===============鍒涘缓鐩綍================="); + remoteDirectoryPath = remoteDirectoryPath + "/"; boolean makeDirFlag = this.ftpClient.makeDirectory(remoteDirectoryPath); }catch (IOException e) { e.printStackTrace(); @@ -611,12 +642,10 @@ for (int currentFile = 0;currentFile < allFile.length;currentFile++) { File f =allFile[currentFile]; String srcName= f.getPath().toString(); -// System.out.println(srcName+":====================璇诲彇鏂囦欢======================"); String tPath = remoteDirectoryPath+f.getName(); if (!f.isDirectory()) { -// System.out.println(srcName+":====================寮�濮嬩紶杈撴枃浠�======================"); - int dNum = getNumFromStr(tPath,File.separator.toCharArray()[0]); + int dNum = getNumFromStr(tPath,"/".toCharArray()[0]); uploadFile(f, tPath); if(dNum -2>=0){ for (int i = 0; i < dNum-1; i++) { @@ -624,11 +653,9 @@ } } }else{ -// System.out.println(srcName+":====================鍒涘缓瀛愮洰褰�======================"); uploadDirectory(srcName, - tPath+File.separator); + tPath+"/"); } -// System.out.println(srcName+":====================涓婁紶瀹屾瘯======================"); } return true; } @@ -639,7 +666,7 @@ * */ public boolean delDirectory(String pathName ) throws Exception { try { - this.ftpClient.changeWorkingDirectory( pathName.substring(0, pathName.lastIndexOf(File.separator)) ); + this.ftpClient.changeWorkingDirectory( pathName.substring(0, pathName.lastIndexOf("/")) ); ftpClient.removeDirectory(pathName); }catch (IOException e) { e.printStackTrace(); -- Gitblit v1.9.3