最近小編在學習使用nginx放置靜態資源,例如圖片、視頻、css/js等,下面就來記錄一下一波學習干貨。
1.nginx安裝及配置
小編使用的服務器是阿里云的輕量應用服務器,系統使用的是Ubuntu。注意記得開放 9090TCP端口,如果不使用 9090端口作為服務器端口也可不用。
安裝
首先,獲取安裝包是必要的吧,這里提供一個nginx-1.11.3-ubuntu.tar.gz https://pan.baidu.com/s/1vvb41QkOJ4VqfyFckXBkjA (密碼45wz)
小編是將安裝包放在/usr/nginx 中,進入目錄下然后執行 tar -zxvf nginx-1.11.3.tar.gz
進行解壓
配置
修改 /usr/nginx/conf/nginx.conf :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
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就準備可以了
你可以試下在 /usr/nginx/image 下放圖片01.jpg,然后在本地 http://ip:9090/01.jpg 看看圖片能否訪問到
2. SpringBoot 實現資源的上傳
pom.xml:
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
|
<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.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version> 3.8 . 1 </version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-io</artifactId> <version> 1.3 . 2 </version> </dependency> <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> |
appilcation.yml:
1
2
3
4
5
6
7
8
|
ftp: host: 自己服務器ip userName: 服務器賬號 password: 服務器密碼 port: 22 rootPath: /usr/nginx/image img: url: http: //ip:9090/ # ftp.img.url 可以不添加,這里只是為了上傳文件成功后返回文件路徑 |
工具類 FtpUtil.class:
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
|
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 = LoggerFactory.getLogger(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 ( "${ftp.rootPath}" ) public void setRootPath(String val){ FtpUtil.rootPath = 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 = jsch.getSession(userName,host,port); //密碼 sshSession.setPassword(password); Properties sshConfig = new Properties(); sshConfig.put( "StrictHostKeyChecking" , "no" ); sshSession.setConfig(sshConfig); sshSession.connect(); Channel channel = sshSession.openChannel( "sftp" ); channel.connect(); 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(); sftp.exit(); //處理返回的路徑 String resultFile; resultFile = imgUrl + imagePath + imagesName; return resultFile; } catch (Exception e) { logger.error( "上傳失敗:" + 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; sftp.rm(path); sftp.quit(); sftp.exit(); } catch (Exception e) { e.printStackTrace(); } } } |
工具類IDUtils.class(修改上傳圖片名):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
import java.util.Random; public class IDUtils { /** * 生成隨機圖片名 */ public static String genImageName() { //取當前時間的長整形值包含毫秒 long millis = System.currentTimeMillis(); //加上三位隨機數 Random random = new Random(); int end3 = random.nextInt( 999 ); //如果不足三位前面補0 String str = millis + String.format( "%03d" , end3); return str; } } |
NginxService.class:
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
|
import com.wzy.util.FtpUtil; import com.wzy.util.IDUtils; import lombok.extern.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 = uploadFile.getOriginalFilename(); //1.2使用IDUtils工具類生成新的文件名,新文件名 = newName + 文件后綴 String newName = IDUtils.genImageName(); 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 = uploadFile.getInputStream(); } catch (IOException e) { e.printStackTrace(); } //2.2調用FtpUtil工具類進行上傳 return FtpUtil.putImages(input, filePath, newName); } } |
NginxController.class:
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
|
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.wzy.service.NginxService; import lombok.extern.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.如果要視頻跟圖片一起上傳的話,只要修改 nginx.conf配置文件,添加相應的視頻后綴即可,代碼沒變,上傳后也是放在 /usr/image 下,要不然文件能上傳,但是訪問不了
2.上面代碼 uploads接口是實現多文件上傳
總結
以上所述是小編給大家介紹的SpringBoot+nginx實現資源上傳功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
如果你覺得本文對你有幫助,歡迎轉載,煩請注明出處,謝謝!
原文鏈接:https://blog.csdn.net/qq_38752386/article/details/102453756