实例详解SpringBoot+nginx实现资源上传功能
最近⼩编在学习使⽤nginx放置静态资源,例如图⽚、视频、css/js等,下⾯就来记录⼀下⼀波学习⼲货。
⼩编使⽤的服务器是阿⾥云的轻量应⽤服务器,系统使⽤的是Ubuntu。注意记得开放 9090TCP端⼝,如果不使⽤ 9090端⼝作为服务器端⼝也可不⽤。
安装
⼩编是将安装包放在/usr/nginx 中,进⼊⽬录下然后执⾏tar -zxvf nginx-1.11. 进⾏解压
配置
修改 /usr/nginx/f :
server {
listen  9090;
server_name localhost;
location ~ .(jpg|png|jpeg|gif|bmp)$ { #可识别的⽂件后缀
root /usr/nginx/image/; #图⽚的映射路径
autoindex on; #开启⾃动索引
expires 1h; #过期时间
}
location ~ .(css|js)$ {
root /usr/nginx/static/;
autoindex on;
expires 1h;
}
location ~ .(AVI|mov|rmvb|rm|FLV|mp4|3GP)$ {
root /usr/nginx/video/;
autoindex on;
expires 1h;
}
该修改的修改,该增加的增加,切记勿乱删
最后⼀步,启动nginx,执⾏ ./usr/nginx/sbin/nginx
到这⾥服务器nginx就准备可以了
2. SpringBoot 实现资源的上传
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.1.7.RELEASE</version>
<scope>test</scope>
</dependency>
<!-- Apache⼯具组件 -->
<dependency>
<groupId>org.apachemons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.8.1</version>
</dependency>
<dependency>
<groupId>org.apachemons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
spring framework组件
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.6</version>
</dependency>
<!-- ⽂件上传组件 -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.22</version>
</dependency>
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.54</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.10.3</version>
</dependency>
</dependencies>
ftp:
host: ⾃⼰服务器ip
userName: 服务器账号
password: 服务器密码
port: 22
rootPath: /usr/nginx/image
img:
url: ip:9090/  # ftp.img.url 可以不添加,这⾥只是为了上传⽂件成功后返回⽂件路径⼯具类 FtpUtil.class:
import com.jcraft.jsch.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.InputStream;
import java.util.Properties;
@Component
public class FtpUtil {
private static Logger logger = Logger(FtpUtil.class);
/**
* ftp服务器ip地址
*/
private static String host;
@Value("${ftp.host}")
public void setHost(String val){
FtpUtil.host = val;
}
/**
* 端⼝
*/
private static int port;
@Value("${ftp.port}")
public void setPort(int val){
FtpUtil.port = val;
}
/
**
* ⽤户名
*/
private static String userName;
@Value("${ftp.userName}")
public void setUserName(String val){
FtpUtil.userName = val;
}
/**
* 密码
*/
private static String password;
@Value("${ftp.password}")
public void setPassword(String val){
FtpUtil.password = val;
}
/**
* 存放图⽚的根⽬录
*/
private static String rootPath;
@Value("${Path}")
public void setRootPath(String val){
}
/**
* 存放图⽚的路径
*/
private static String imgUrl;
@Value("${ftp.img.url}")
public void setImgUrl(String val){
FtpUtil.imgUrl = val;
}
/**
* 获取连接
*/
private static ChannelSftp getChannel() throws Exception{
JSch jsch = new JSch();
//->ssh root@host:port
Session sshSession = Session(userName,host,port);
//密码
sshSession.setPassword(password);
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
sshSession.setConfig(sshConfig);
Channel channel = sshSession.openChannel("sftp");
return (ChannelSftp) channel;
}
/**
* ftp上传图⽚
* @param inputStream 图⽚io流
* @param imagePath 路径,不存在就创建⽬录
* @param imagesName 图⽚名称
* @return urlStr 图⽚的存放路径
*/
public static String putImages(InputStream inputStream, String imagePath, String imagesName){  try {
ChannelSftp sftp = getChannel();
String path = rootPath + imagePath + "/";
createDir(path,sftp);
//上传⽂件
sftp.put(inputStream, path + imagesName);
logger.info("上传成功!");
sftp.quit();
//处理返回的路径
String resultFile;
resultFile = imgUrl + imagePath + imagesName;
return resultFile;
} catch (Exception e) {
<("上传失败:" + e.getMessage());
}
return "";
}
/**
* 创建⽬录
*/
private static void createDir(String path,ChannelSftp sftp) throws SftpException {
String[] folders = path.split("/");
sftp.cd("/");
for ( String folder : folders ) {
if ( folder.length() > 0 ) {
try {
sftp.cd( folder );
}catch ( SftpException e ) {
sftp.mkdir( folder );
sftp.cd( folder );
}
}
}
}
/**
* 删除图⽚
*/
public static void delImages(String imagesName){
try {
ChannelSftp sftp = getChannel();
String path = rootPath + imagesName;
<(path);
sftp.quit();
} catch (Exception e) {
e.printStackTrace();
}
}
}
⼯具类IDUtils.class(修改上传图⽚名):
import java.util.Random;
public class IDUtils {
/**
* ⽣成随机图⽚名
*/
public static String genImageName() {
//取当前时间的长整形值包含毫秒
long millis = System.currentTimeMillis();
//加上三位随机数
Random random = new Random();
int end3 = Int(999);
//如果不⾜三位前⾯补0
String str = millis + String.format("%03d", end3);
return str;
}
}
NginxService.class:
import com.wzy.util.FtpUtil;
import com.wzy.util.IDUtils;
slf4j.Slf4j;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
/
**
* @Package: com.wzy.service
* @Author: Clarence1
* @Date: 2019/10/4 21:34
*/
@Service
@Slf4j
public class NginxService {
public Object uploadPicture(MultipartFile uploadFile) {
//1、给上传的图⽚⽣成新的⽂件名
//1.1获取原始⽂件名
String oldName = OriginalFilename();
//1.2使⽤IDUtils⼯具类⽣成新的⽂件名,新⽂件名 = newName + ⽂件后缀  String newName = ImageName();
assert oldName != null;
newName = newName + oldName.substring(oldName.lastIndexOf("."));  //1.3⽣成⽂件在服务器端存储的⼦⽬录
String filePath = new DateTime().toString("/yyyyMMdd/");
//2、把图⽚上传到图⽚服务器
//2.1获取上传的io流
InputStream input = null;
try {
input = InputStream();
} catch (IOException e) {
e.printStackTrace();
}
//2.2调⽤FtpUtil⼯具类进⾏上传
return FtpUtil.putImages(input, filePath, newName);
}
}
NginxController.class:
import com.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wzy.service.NginxService;
slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.util.HashMap;
import java.util.Map;
@RestController
@Slf4j
public class NginxController {
@Autowired
private NginxService nginxService;
/**
* 可上传图⽚、视频,只需在nginx配置中配置可识别的后缀
*/
@PostMapping("/upload")
public String pictureUpload(@RequestParam(value = "file") MultipartFile uploadFile) {
long begin = System.currentTimeMillis();
String json = "";
try {
Object result = nginxService.uploadPicture(uploadFile);
json = new ObjectMapper().writeValueAsString(result);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
long end = System.currentTimeMillis();
log.info("任务结束,共耗时:[" + (end-begin) + "]毫秒");
return json;
}
@PostMapping("/uploads")
public Object picturesUpload(@RequestParam(value = "file") MultipartFile[] uploadFile) {
long begin = System.currentTimeMillis();
Map<Object, Object> map = new HashMap<>(10);
int count = 0;
for (MultipartFile file : uploadFile) {
Object result = nginxService.uploadPicture(file);
map.put(count, result);
count++;
}
long end = System.currentTimeMillis();
log.info("任务结束,共耗时:[" + (end-begin) + "]毫秒");
return map;
}
}
启动项⽬,Postman神器⼀波
注意:
1.如果要视频跟图⽚⼀起上传的话,只要修改 f配置⽂件,添加相应的视频后缀即可,代码没变,上传后也是放在/usr/image 下,要不然⽂件能上传,但是访问不了
2.上⾯代码 uploads接⼝是实现多⽂件上传

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。