- SpringBoot+Nginx实现文件上传和下载功能(下)
- @[toc]
- 1、导入依赖
- 2、配置sshd连接
- 3、配置JSch
- 4、配置文件传输的服务层
- 5、配置controller层
本文章主要讲springBoot实现文件上传的功能。如需查看nginx的操作请前往SpringBoot+Nginx实现文件上传和下载功能(上)_czxboys的博客-CSDN博客
1、导入依赖在pom.xml加入以下依赖:
org.springframework.boot
spring-boot-configuration-processor
true
commons-fileupload
commons-fileupload
1.4
com.jcraft
jsch
0.1.55
org.projectlombok
lombok
1.18.24
2、配置sshd连接
编写配置属性映射类
package com.czx.jsch.properties;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@Data
@ConfigurationProperties(prefix = "spring.jsch")
public class JschProperties {
private String host;
private int port;
private String username;
private String password;
private String url;
private String path;
private String strictHostKeyChecking = "no";
}
application.yml的配置
spring:
jsch:
host: 192.168.64.128
port: 22
username: root
password: czx123
url: http://192.168.64.128:9090/upload/
path: /export/share/upload
3、配置JSch
JSch对象的初始化配置
package com.czx.jsch.config;
import com.czx.jsch.properties.JschProperties;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.annotation.Configuration;
import javax.annotation.Resource;
@Configuration
@Data
@Slf4j
public class JSchAutoConfiguration implements InitializingBean, DisposableBean {
@Resource
private JschProperties jschProperties;
private Session session;
@Override
public void afterPropertiesSet() throws Exception {
JSch jSch = new JSch();
session = jSch.getSession(jschProperties.getUsername(),jschProperties.getHost(),jschProperties.getPort());
if(session==null){
throw new Exception("session is null");
}
session.setPassword(jschProperties.getPassword());
session.setConfig("StrictHostKeyChecking",jschProperties.getStrictHostKeyChecking());
session.connect();
// 打印SSH服务器版本
log.info("Jsch_AutoConfiguration:::{}",session.getServerVersion());
}
@Override
public void destroy() throws Exception {
if (session!=null){
session.disconnect();
}
log.info("Jsch_AutoConfiguration::: destroy connection");
}
}
4、配置文件传输的服务层
ISftpService接口就不贴代码了,直接给impl实现接口
package com.czx.jsch.servive.impl;
import com.czx.jsch.config.JSchAutoConfiguration;
import com.czx.jsch.properties.JschProperties;
import com.czx.jsch.servive.ISftpService;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.InputStream;
@Service
public class SftpServiceImpl implements ISftpService {
@Resource
JSchAutoConfiguration jSchAutoConfiguration;
@Resource
JschProperties jschProperties ;
@Override
public String put(InputStream inputStream, String filePath, String name) {
try {
ChannelSftp sftp = this.getChannelSftp();
// 上传的图片路径
String path = jschProperties.getPath() + "/" + filePath + "/" + name;
// 上传文件
sftp.put(inputStream, path);
sftp.quit();
sftp.exit();
} catch (JSchException | SftpException e) {
e.printStackTrace();
}
// 返回图片的浏览路径
return jschProperties.getUrl() + filePath + "/" + name;
}
@Override
public void delete(String filePath, String name) {
try {
ChannelSftp sftp = this.getChannelSftp();
sftp.rm(jschProperties.getPath() + "/" + filePath + "/" + name);
sftp.quit();
sftp.exit();
} catch (JSchException | SftpException e) {
e.printStackTrace();
}
}
@Override
public InputStream get(String filePath, String name) {
InputStream inputStream = null;
try {
ChannelSftp sftp = this.getChannelSftp();
inputStream = sftp.get(jschProperties.getPath() + "/" + filePath + "/" + name);
sftp.quit();
sftp.exit();
} catch (JSchException | SftpException e) {
e.printStackTrace();
}
return inputStream;
}
private ChannelSftp getChannelSftp() throws JSchException {
Session session = jSchAutoConfiguration.getSession();
// 安全文件传输协议
ChannelSftp sftp = (ChannelSftp) session.openChannel("sftp");
sftp.connect();
return sftp;
}
}
5、配置controller层
package com.czx.jsch.controller;
import com.czx.jsch.common.Re;
import com.czx.jsch.servive.ISftpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
@RestController
@RequestMapping("/common")
@Slf4j
public class SftpController {
@Resource
ISftpService sftpService;
@PostMapping("/upload")
public Re upload(MultipartFile file) throws IOException {
log.info(file.getOriginalFilename());
String fileName = file.getOriginalFilename();
InputStream inputStream = file.getInputStream();
return Re.success(sftpService.put(inputStream,"images",fileName));
}
}
项目代码:czxboys / jsch-springboot · GitCode



