| | |
| | | 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 |
| | |
| | | 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服务器 |
| | |
| | | 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()); |
| | | } |
| | | } |
| | | |
| | |
| | | /** |
| | | * 递归根据路径创建文件夹 |
| | | * |
| | | * @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) { |
| | |
| | | /** |
| | | * 将输入流的数据上传到sftp作为文件(多层目录) |
| | | * |
| | | * @param directory 上传到该目录(多层目录) |
| | | * @param sftpFileName sftp端文件名 |
| | | * @param input 输入流 |
| | | * @param directory |
| | | * 上传到该目录(多层目录) |
| | | * @param sftpFileName |
| | | * sftp端文件名 |
| | | * @param input |
| | | * 输入流 |
| | | * @throws SftpException |
| | | * @throws Exception |
| | | */ |
| | |
| | | /** |
| | | * 将输入流的数据上传到sftp作为文件 |
| | | * |
| | | * @param directory 上传到该目录(单层目录) |
| | | * @param sftpFileName sftp端文件名 |
| | | * @param input 输入流 |
| | | * @param directory |
| | | * 上传到该目录(单层目录) |
| | | * @param sftpFileName |
| | | * sftp端文件名 |
| | | * @param input |
| | | * 输入流 |
| | | * @throws SftpException |
| | | * @throws Exception |
| | | */ |
| | |
| | | /** |
| | | * 上传单个文件 |
| | | * |
| | | * @param directory 上传到sftp目录 |
| | | * @param uploadFile 要上传的文件,包括路径 |
| | | * @param directory |
| | | * 上传到sftp目录 |
| | | * @param uploadFile |
| | | * 要上传的文件,包括路径 |
| | | * @throws FileNotFoundException |
| | | * @throws SftpException |
| | | * @throws Exception |
| | |
| | | /** |
| | | * 将byte[]上传到sftp,作为文件。注意:从String生成byte[]是,要指定字符集。 |
| | | * |
| | | * @param directory 上传到sftp目录 |
| | | * @param sftpFileName 文件在sftp端的命名 |
| | | * @param byteArr 要上传的字节数组 |
| | | * @param directory |
| | | * 上传到sftp目录 |
| | | * @param sftpFileName |
| | | * 文件在sftp端的命名 |
| | | * @param byteArr |
| | | * 要上传的字节数组 |
| | | * @throws SftpException |
| | | * @throws Exception |
| | | */ |
| | |
| | | /** |
| | | * 将字符串按照指定的字符编码上传到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); |
| | | } |
| | |
| | | /** |
| | | * 下载文件 |
| | | * |
| | | * @param directory 下载目录 |
| | | * @param downloadFile 下载的文件名 |
| | | * @param directory |
| | | * 下载目录 |
| | | * @param downloadFile |
| | | * 下载的文件名 |
| | | * @return 字节数组 |
| | | * @throws SftpException |
| | | * @throws IOException |
| | |
| | | } |
| | | |
| | | /** |
| | | * 下载文件 |
| | | * |
| | | * @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 |
| | | */ |
| | |
| | | /** |
| | | * 列出目录下的文件 |
| | | * |
| | | * @param directory 要列出的目录 |
| | | * @param directory |
| | | * 要列出的目录 |
| | | * @return |
| | | * @throws SftpException |
| | | */ |
| | |
| | | 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; |
| | | |
| | | } |
| | | } |