1.引入相关依赖
1 2 3 4 5
| <dependency> <groupId>com.jcraft</groupId> <artifactId>jsch</artifactId> <version>0.1.55</version> </dependency>
|
2.定义相关配置
1 2 3 4 5 6 7 8
| excel.domain=http://192.168.102.1/excel/ pic.domain=http://192.168.102.1/uploadImg/ sftp.host=192.168.102.1 sftp.port=22 sftp.user=root sftp.password=12341234 sftp.file.path=/home/nginx/html/excel sftp.image.path=/home/nginx/html/uploadImg
|
3.实现工具类
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
| @Slf4j @Component public class SFtpUtil {
@Value("${sftp.host}") private String host; @Value("${sftp.port}") private Integer port; @Value("${sftp.user}") private String user; @Value("${sftp.password}") private String pwd; @Value("${sftp.file.path}") private String filePath; @Value("${sftp.image.path}") private String picPath;
public ChannelSftp connect() throws JSchException {
ChannelSftp sftp = null; try { Session session = null; JSch jsch = new JSch(); if (port != null) { session = jsch.getSession(user, host, port.intValue()); } else { session = jsch.getSession(user, host); } session.setPassword(pwd); session.setConfig("StrictHostKeyChecking", "no"); session.connect(30000);
Channel channel = session.openChannel("sftp"); channel.connect(); sftp = (ChannelSftp) channel; } catch (JSchException e) { e.printStackTrace(); System.out.println("SFTPUitl 获取连接发生错误"); throw e; } return sftp; }
public void destroy(ChannelSftp sftp) { try { if (sftp != null) { if (sftp.isConnected()) { sftp.disconnect(); } if (sftp.getSession() != null) { sftp.getSession().disconnect(); } } }catch (Exception e) { log.error("",e); } }
public void upload(InputStream ins, String fileName) throws Exception { upload(filePath,ins,fileName); } public void uploadPic(InputStream ins, String fileName) throws Exception { upload(picPath,ins,fileName); }
public void upload(String directory, InputStream ins, String fileName) throws Exception {
ChannelSftp sftp = null; try { sftp = connect(); Vector<?> content = sftp.ls(directory); if (content == null) { sftp.mkdir(directory); } sftp.cd(directory);
sftp.put(ins, new String(fileName.getBytes(), "UTF-8"));
} finally { destroy(sftp); }
} }
|
4.相关调用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| @Autowired private SFtpUtil sFtpUtil;
String dateStr = DateUtils.getDateStr(); String fileName = "aaa" + dateStr + ".xlsx"; ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
EasyExcel.write(outputStream,ProductExcelDTO.class).head(ProductExcelDTO.class).sheet("测试1").doWrite(resultLs);
byte[] content = outputStream.toByteArray(); InputStream is = new ByteArrayInputStream(content);
sFtpUtil.upload(is, fileName);
|
上面代码目的是生成个excel文件上传到sftp服务器,供前端下载的时候调用。当文件上传后,将对应的http请求链接返回给前端,前端拿到链接直接请求从而将文件下载下来。怎么配置http请求,就需要运维走nginx进行配置。本例中http链接就是配置文件定义的excel.domain=http://192.168.102.1/excel/ + 对应的文件名。
5.总结
使用sftp上传功能,适用像下载功能这种一次性访问的,或简单的小项目。毕竟sftp服务器不是分布式的。像图片上传这种还是建议走fastzDFS这种分布式的上传工具,上传的图片都会有备份,其中一台出现问题,也能够正常访问。