From 86df207c37502cce1b2043e1c7c0486459eef1d6 Mon Sep 17 00:00:00 2001 From: mitao <2763622819@qq.com> Date: 星期四, 13 三月 2025 19:58:45 +0800 Subject: [PATCH] 党员、诉求评论基础代码 --- springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/utlis/SFTPUtil.java | 163 +++++++++++++++++++++++++++++++++++++++-------------- 1 files changed, 119 insertions(+), 44 deletions(-) diff --git a/springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/utlis/SFTPUtil.java b/springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/utlis/SFTPUtil.java index 8fc9a8d..890157d 100644 --- a/springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/utlis/SFTPUtil.java +++ b/springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/utlis/SFTPUtil.java @@ -1,14 +1,15 @@ package com.panzhihua.common.utlis; -import com.jcraft.jsch.*; -import com.panzhihua.common.constants.FtpConstants; +import java.io.*; +import java.util.Properties; +import java.util.Vector; + import org.apache.commons.io.IOUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.*; -import java.util.Properties; -import java.util.Vector; +import com.jcraft.jsch.*; +import com.panzhihua.common.constants.FtpConstants; /** * @version 1.0.0 @@ -52,6 +53,16 @@ this.host = FtpConstants.FTPHOST; this.port = FtpConstants.FTPPORT; } + public static void main(String[] args) throws SftpException, IOException { +// SFTPUtil sftp = new SFTPUtil("root", "evanN7F2K5M6", "47.104.148.185", 22); + SFTPUtil sftp = new SFTPUtil("root", "Wpkl#&%!698", "172.192.3.2", 22); + sftp.login(); + File file = new File("F:\\settings.xml"); + InputStream is = new FileInputStream(file); + // 多级目录创建并上传 + sftp.uploadMore("/mnt/data/ftp/ss", "settings.xml", is); + sftp.logout(); + } /** * 连接sftp服务器 @@ -79,10 +90,11 @@ channel.connect(); log.info("channel is connected"); - sftp = (ChannelSftp) channel; + sftp = (ChannelSftp)channel; log.info(String.format("sftp server host:[%s] port:[%s] is connect successfull", host, port)); } catch (JSchException e) { - log.error("Cannot connect to specified sftp server : {}:{} \n Exception message is: {}", new Object[]{host, port, e.getMessage()}); + log.error("Cannot connect to specified sftp server : {}:{} \n Exception message is: {}", host, port, + e.getMessage()); } } @@ -107,10 +119,14 @@ /** * 递归根据路径创建文件夹 * - * @param dirs 根据 / 分隔后的数组文件夹名称 - * @param tempPath 拼接路径 - * @param length 文件夹的格式 - * @param index 数组下标 + * @param dirs + * 根据 / 分隔后的数组文件夹名称 + * @param tempPath + * 拼接路径 + * @param length + * 文件夹的格式 + * @param index + * 数组下标 * @return */ public void mkdirDir(String[] dirs, String tempPath, int length, int index) { @@ -144,9 +160,12 @@ /** * 将输入流的数据上传到sftp作为文件(多层目录) * - * @param directory 上传到该目录(多层目录) - * @param sftpFileName sftp端文件名 - * @param input 输入流 + * @param directory + * 上传到该目录(多层目录) + * @param sftpFileName + * sftp端文件名 + * @param input + * 输入流 * @throws SftpException * @throws Exception */ @@ -166,9 +185,12 @@ /** * 将输入流的数据上传到sftp作为文件 * - * @param directory 上传到该目录(单层目录) - * @param sftpFileName sftp端文件名 - * @param input 输入流 + * @param directory + * 上传到该目录(单层目录) + * @param sftpFileName + * sftp端文件名 + * @param input + * 输入流 * @throws SftpException * @throws Exception */ @@ -187,8 +209,10 @@ /** * 上传单个文件 * - * @param directory 上传到sftp目录 - * @param uploadFile 要上传的文件,包括路径 + * @param directory + * 上传到sftp目录 + * @param uploadFile + * 要上传的文件,包括路径 * @throws FileNotFoundException * @throws SftpException * @throws Exception @@ -201,9 +225,12 @@ /** * 将byte[]上传到sftp,作为文件。注意:从String生成byte[]是,要指定字符集。 * - * @param directory 上传到sftp目录 - * @param sftpFileName 文件在sftp端的命名 - * @param byteArr 要上传的字节数组 + * @param directory + * 上传到sftp目录 + * @param sftpFileName + * 文件在sftp端的命名 + * @param byteArr + * 要上传的字节数组 * @throws SftpException * @throws Exception */ @@ -214,29 +241,38 @@ /** * 将字符串按照指定的字符编码上传到sftp * - * @param directory 上传到sftp目录 - * @param sftpFileName 文件在sftp端的命名 - * @param dataStr 待上传的数据 - * @param charsetName sftp上的文件,按该字符编码保存 + * @param directory + * 上传到sftp目录 + * @param sftpFileName + * 文件在sftp端的命名 + * @param dataStr + * 待上传的数据 + * @param charsetName + * sftp上的文件,按该字符编码保存 * @throws UnsupportedEncodingException * @throws SftpException * @throws Exception */ - public void upload(String directory, String sftpFileName, String dataStr, String charsetName) throws UnsupportedEncodingException, SftpException { + public void upload(String directory, String sftpFileName, String dataStr, String charsetName) + throws UnsupportedEncodingException, SftpException { upload(directory, sftpFileName, new ByteArrayInputStream(dataStr.getBytes(charsetName))); } /** * 下载文件 * - * @param directory 下载目录 - * @param downloadFile 下载的文件 - * @param saveFile 存在本地的路径 + * @param directory + * 下载目录 + * @param downloadFile + * 下载的文件 + * @param saveFile + * 存在本地的路径 * @throws SftpException * @throws FileNotFoundException * @throws Exception */ - public void download(String directory, String downloadFile, String saveFile) throws SftpException, FileNotFoundException { + public void download(String directory, String downloadFile, String saveFile) + throws SftpException, FileNotFoundException { if (directory != null && !"".equals(directory)) { sftp.cd(directory); } @@ -248,8 +284,10 @@ /** * 下载文件 * - * @param directory 下载目录 - * @param downloadFile 下载的文件名 + * @param directory + * 下载目录 + * @param downloadFile + * 下载的文件名 * @return 字节数组 * @throws SftpException * @throws IOException @@ -266,10 +304,34 @@ } /** + * 下载文件 + * + * @param directory + * 下载目录 + * @param downloadFile + * 下载的文件名 + * @return 字节数组 + * @throws SftpException + * @throws IOException + * @throws Exception + */ + public InputStream downloadInputStream(String directory, String downloadFile) throws SftpException, IOException { + if (directory != null && !"".equals(directory)) { + sftp.cd(directory); + } + InputStream is = sftp.get(downloadFile); + // byte[] fileData = IOUtils.toByteArray(is); + // log.info("file:{} is download successful", downloadFile); + return is; + } + + /** * 删除文件 * - * @param directory 要删除文件所在目录 - * @param deleteFile 要删除的文件 + * @param directory + * 要删除文件所在目录 + * @param deleteFile + * 要删除的文件 * @throws SftpException * @throws Exception */ @@ -281,7 +343,8 @@ /** * 列出目录下的文件 * - * @param directory 要列出的目录 + * @param directory + * 要列出的目录 * @return * @throws SftpException */ @@ -289,13 +352,25 @@ return sftp.ls(directory); } - public static void main(String[] args) throws SftpException, IOException { - SFTPUtil sftp = new SFTPUtil("root", "evanN7F2K5M6", "47.104.148.185", 22); - sftp.login(); - File file = new File("F:\\settings.xml"); - InputStream is = new FileInputStream(file); - // 多级目录创建并上传 - sftp.uploadMore("/mnt/data/ftp/ss", "settings.xml", is); - sftp.logout(); + /** + * 判断文件或者目录是否存在 + * + * @param path + * 目录或者文件的路径 + * @return 存在结果 + */ + public boolean isExistDir(String path) { + boolean isExist = false; + try { + SftpATTRS sftpATTRS = sftp.lstat(path); + isExist = true; + return sftpATTRS.isDir(); + } catch (Exception e) { + if (e.getMessage().toLowerCase().equals("no such file")) { + isExist = false; + } + } + return isExist; + } } \ No newline at end of file -- Gitblit v1.7.1