From 177249c76aeea0b4bf8d8816d4994e3b445b45ce Mon Sep 17 00:00:00 2001 From: huanghongfa <huanghongfa123456> Date: 星期四, 02 九月 2021 10:39:34 +0800 Subject: [PATCH] Merge remote-tracking branch 'origin/zzj' into zzj --- springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/utlis/SFTPUtil.java | 142 +++++++++++++++++++++++++++++------------------ 1 files changed, 88 insertions(+), 54 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 10f8fbe..505ec3d 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 @@ -53,6 +54,16 @@ this.port = FtpConstants.FTPPORT; } + 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(); + } + /** * 连接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 @@ -268,8 +306,10 @@ /** * 下载文件 * - * @param directory 下载目录 - * @param downloadFile 下载的文件名 + * @param directory + * 下载目录 + * @param downloadFile + * 下载的文件名 * @return 字节数组 * @throws SftpException * @throws IOException @@ -280,16 +320,18 @@ sftp.cd(directory); } InputStream is = sftp.get(downloadFile); -// byte[] fileData = IOUtils.toByteArray(is); -// log.info("file:{} is download successful", 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 */ @@ -301,7 +343,8 @@ /** * 列出目录下的文件 * - * @param directory 要列出的目录 + * @param directory + * 要列出的目录 * @return * @throws SftpException */ @@ -311,11 +354,13 @@ /** * 判断文件或者目录是否存在 - * @param path 目录或者文件的路径 + * + * @param path + * 目录或者文件的路径 * @return 存在结果 */ - public boolean isExistDir(String path){ - boolean isExist=false; + public boolean isExistDir(String path) { + boolean isExist = false; try { SftpATTRS sftpATTRS = sftp.lstat(path); isExist = true; @@ -327,16 +372,5 @@ } return isExist; - } - - - 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(); } } \ No newline at end of file -- Gitblit v1.7.1