jcraft SFTP 文件上传
一 依赖
<properties> <jsch.version>0.1.54</jsch.version> </properties>
<dependency> <groupId>com.jcraft</groupId> <artifactId>jsch</artifactId> <version>${jsch.version}</version> </dependency>
二 工具类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | package com.mangoubiubiu.utils; import com.jcraft.jsch.*; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.Properties; public class SFTPTransFile { private static final String sourceDir = "D:\\hhh.txt" ; //传输的源端目录 private static final String destDir = "/apps/images" ; //传输的目的端目录 private static final String host = "192.168.56.12" ; //目的端的IP地址 private static final int port = 22 ; //目的端的端口号 private static final String userName = "root" ; //目的端的用户名 private static final String passWord = "root" ; //目的端的密码 public static void main(String[] args) throws Exception { //连接目的端 ChannelSftp sftp = connect(host, port, userName, passWord); //创建目录 // mkdir(destDir, sftp); //文件上传 // uploadFile(new File(sourceDir), destDir, sftp); //文件重命名 //renameFile(destDir,"look.txt","xl.txt",sftp); //文件下载 //downloadFile(destDir,"xl.txt","F:\\xl.txt",sftp); //删除文件 //deleteFile(destDir,"xl.txt",sftp); } //连接目的端 public static ChannelSftp connect(String host, int port, String userName, String passWord) { ChannelSftp sftp = null ; try { JSch jsch = new JSch(); jsch.getSession(userName, host, port); Session sshSession = jsch.getSession(userName, host, port); System.out.println( "Session创建成功" ); sshSession.setPassword(passWord); System.out.println( "密码输入成功" ); Properties sshConfig = new Properties(); sshConfig.put( "StrictHostKeyChecking" , "no" ); System.out.println( "链接参数设置成功" ); sshSession.setConfig(sshConfig); sshSession.connect(); System.out.println( "Session已连接" ); Channel channel = sshSession.openChannel( "sftp" ); channel.connect(); sftp = (ChannelSftp) channel; System.out.println( "连接到主机" + host + "." ); } catch (Exception e) { e.printStackTrace(); } return sftp; } //文件上传 public static void uploadFile(FileInputStream file, String destDir, ChannelSftp sftp) { try { sftp.cd(destDir); // sftp.put(new FileInputStream(file), file.getName()); System.out.println( "文件上传成功" ); } catch (Exception e) { System.out.println( "文件上传失败" ); } } //创建目录 public static void mkdir(String destDir, ChannelSftp sftp) { try { sftp.cd(destDir); System.out.println( "文件夹创建成功: " + destDir); } catch (Exception e) { System.out.println( "进入目录失败,创建目录: " + destDir); //创建目录 String[] dirs = destDir.split( "/" ); String tempPath = "" ; for (String dir : dirs) { if (dir == null || dir.equals( "" )) { continue ; } tempPath += "/" + dir; try { System.out.println( "进入目录[" + tempPath + "]" ); sftp.cd(tempPath); } catch (SftpException ex) { System.out.println( "进入目录失败,创建目录[" + tempPath + "]" ); try { sftp.mkdir(tempPath); sftp.cd(tempPath); System.out.println( "进入目录[" + tempPath + "]" ); } catch (SftpException exc) { System.out.println( "进入目录失败,创建目录[" + tempPath + "]" ); } } } } } //文件重命名 public static void renameFile(String destDir, String oldName, String newName, ChannelSftp sftp) { try { sftp.cd(destDir); sftp.rename(oldName, newName); System.out.println( "文件重命名成功" ); } catch (Exception e) { System.out.println( "文件重命名失败" ); } } //文件下载 public static void downloadFile(String destDir, String downloadFile, String saveFile, ChannelSftp sftp) { try { sftp.cd(destDir); File file = new File(saveFile); sftp.get(downloadFile, new FileOutputStream(file)); System.out.println( "文件下载成功" ); } catch (Exception e) { System.out.println( "文件下载失败" ); } } //文件删除 public static void deleteFile(String destDir, String deleteFile, ChannelSftp sftp) { try { sftp.cd(destDir); sftp.rm(deleteFile); System.out.println( "文件删除成功" ); } catch (Exception e) { System.out.println( "文件删除失败" ); } } } |
三 业务代码
package com.mangoubiubiu.service.impl; import cn.hutool.core.io.FileTypeUtil; import com.jcraft.jsch.ChannelSftp; import com.mangoubiubiu.entities.Student; import com.mangoubiubiu.mapper.StudentMapper; import com.mangoubiubiu.service.StudentService; import com.mangoubiubiu.utils.SFTPTransFile; import com.mangoubiubiu.vo.StudentQry; import lombok.extern.slf4j.Slf4j; import org.joda.time.DateTime; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.List; import java.util.Queue; import java.util.UUID; @Service @Slf4j public class StudentServiceImpl implements StudentService { @Autowired private StudentMapper studentMapper; private static final String sourceDir = "D:\\hhh.txt";//传输的源端目录 private static final String destDir = "/apps/images";//传输的目的端目录 private static final String host = "192.168.56.12";//目的端的IP地址 private static final int port = 22; //目的端的端口号 private static final String userName = "root";//目的端的用户名 private static final String passWord = "root";//目的端的密码 @Override public Student getStudent(String id) { return studentMapper.getStuById(id); } @Override public List<Student> getAllStudent(StudentQry qry) { return studentMapper.findAll(qry); } @Override public String uploadFile(MultipartFile file) { Long startCurrentTimes=System.currentTimeMillis(); InputStream inputStream=null; String fileName=""; try { String fileType=FileTypeUtil.getType(file.getInputStream()); if("jpg".equals(fileType) || "png".equals(fileType) || "jpeg".equals(fileType) || "gif".equals(fileType) ){ log.info("文件类型========={}",fileType); // 上传文件流。 inputStream = file.getInputStream(); //获取文件名称 fileName=file.getOriginalFilename(); //在文件中添加随机值 uuid String uuid= UUID.randomUUID().toString().replaceAll("-",""); fileName=uuid+fileName; ChannelSftp sftp = SFTPTransFile.connect(host, port, userName, passWord); sftp.cd(destDir); sftp.put(inputStream,fileName); }else{ log.info("文件上传失败,不支持的文件类型========={}",fileType); } } catch (Exception e) { e.printStackTrace(); }finally { try { if(inputStream!=null){ inputStream.close(); } }catch (IOException e){ } } Long endCurrentTimes=System.currentTimeMillis(); log.info("文件上传成功,耗时{}ms",endCurrentTimes-startCurrentTimes); return fileName; } @Override public List<Student> getAllStudentT(StudentQry qry) { return studentMapper.findStuAllAndRewardOrPunish(qry); } }
本文作者:KwFruit
本文链接:https://www.cnblogs.com/mangoubiubiu/p/15452025.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)