SpringBoot+Nginx实现文件上传和下载功能(下)
本文章主要讲springBoot实现文件上传的功能。如需查看nginx的操作请前往SpringBoot+Nginx实现文件上传和下载功能(上)_czxboys的博客-CSDN博客
1、导入依赖
在pom.xml
加入以下依赖:
<!--元数据处理-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!-- 文件上传组件 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
<!--ssh远程连接服务器-->
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.55</version>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
</dependency>
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 {
/**
* 服务器IP
*/
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;
/**
* 初始化配置链接
*
* @throws Exception 异常
*/
@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());
}
/**
* 销毁链接
*
* @throws Exception 销毁链接异常
*/
@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 ;
/**
* 上传文件
*
* @param inputStream 输入流
* @param filePath 文件路径
* @param name 文件名
* @return 路径
*/
@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;
}
/**
* 删除文件
*
* @param filePath 文件路径
* @param 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();
}
}
/**
* 下载文件
*
* @param filePath 文件路径
* @param name 文件名称
*/
@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;
}
/**
* 获取安全文件传输协议类
*
* @return 安全文件传输协议类
* @throws JSchException JSch链接异常
*/
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;
/**
* @author czx
* @since 2022/6/7 18:49
*/
@RestController
@RequestMapping("/common")
@Slf4j
public class SftpController {
@Resource
ISftpService sftpService;
/**
* 上传文件
*
* @param file 文件
* @return 结果
* @throws IOException 输入异常
*/
@PostMapping("/upload")
public Re<String> 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));
}
}