例項詳解SpringBoot+nginx實現資源上傳功能
最近小編在學習使用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 :
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:
<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:
ftp: host: 自己伺服器ip userName: 伺服器賬號 password: 伺服器密碼 port: 22 rootPath: /usr/nginx/image img: url: http://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 = 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(修改上傳圖片名):
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:
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:
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實現資源上傳功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回覆大家的。在此也非常感謝大家對碼農教程網站的支援!
如果你覺得本文對你有幫助,歡迎轉載,煩請註明出處,謝謝!