springboot上傳zip包并解壓至服務(wù)器nginx目錄方式
springboot上傳zip包并解壓至服務(wù)器nginx目錄
此案例場(chǎng)景為:
從前端上傳.zip 包,并進(jìn)行解壓至 docker容器內(nèi)(服務(wù)部署使用了docker),然后容器內(nèi)部解壓目錄與 宿主機(jī)(linux)nginx 目錄的html 進(jìn)行掛載,這樣,就可以通過(guò)nginx 來(lái)訪問(wèn)上傳解壓出來(lái)的文件了。
那么具體看怎么實(shí)現(xiàn),下面會(huì)貼上相關(guān)代碼:
1.首先需要引入zip相關(guān)jar包
<dependency> <groupId>net.lingala.zip4j</groupId> <artifactId>zip4j</artifactId> <version>1.3.1</version> </dependency>
2.然后我直接貼controller 代碼了
package com.fnm.feynman.smartvillage.admin.controller; import com.diboot.core.vo.JsonResult; import com.fnm.feynman.smartvillage.business.entity.RegionVillage; import com.fnm.feynman.smartvillage.business.service.RegionVillageService; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import net.lingala.zip4j.core.ZipFile; import net.lingala.zip4j.exception.ZipException; import org.apache.commons.io.FileUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; 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 java.io.File; import java.io.IOException; /** * @author yanjun.liu * @date 2020/12/24--16:41 */ @Slf4j @RestController @RequestMapping("/admin/open") public class UploadVrController { @Autowired private RegionVillageService regionVillageService; /** * https://www.cnblogs.com/0616--ataozhijia/p/5022028.html * @param id 村莊id * @param file 上傳的vr * @throws IOException * 上傳到docker容器內(nèi) /opt下,并解壓,然后宿主機(jī)和容器內(nèi)路徑進(jìn)行掛載,外部掛載地址為nginx服務(wù)器html 目錄 */ @ApiOperation("上傳vr") @PostMapping("/uploadVr/{id}") public JsonResult uploadVr(@PathVariable("id") Long id, MultipartFile file) throws IOException, ZipException { RegionVillage entity = regionVillageService.getEntity(id); File file1 = new File("/opt/" + entity.getValue()+".zip"); //上傳文件到服務(wù)器/opt/目錄下 //File file1 = new File("E:/zip/" + entity.getValue()+".zip"); FileUtils.writeByteArrayToFile(file1,file.getBytes()); ZipFile zipFile = new ZipFile(file1); //給的vr是gbk,如果這里,你上傳的zip包為utf-8,那么這里改為utf-8 zipFile.setFileNameCharset("gbk"); String path="/opt/"+entity.getValue(); //解壓到指定目錄 zipFile.extractAll(path); String substring = file.getOriginalFilename().substring(0, file.getOriginalFilename().indexOf(".")); //將解壓到nginx 目錄的路徑地址拼接為可訪問(wèn)的url ,進(jìn)行修改實(shí)體對(duì)象 entity.setVrUrl("https://**ed.*****.com/vr/"+entity.getValue()+"/"+substring+"/output/index.html"); regionVillageService.updateEntity(entity); return JsonResult.OK(); } }
3.說(shuō)明上面上傳的/opt/目錄為docker容器里面的目錄
那么我需要將容器里面的目錄和宿主機(jī)nginx目錄進(jìn)行掛載后,才能通過(guò)http請(qǐng)求訪問(wèn)
具體如何掛載呢?
關(guān)鍵:
-v /usr/local/nginx/html/vr:/opt
前面為宿主機(jī)nginx的靜態(tài)文件目錄,:后面為容器內(nèi)部目錄,這個(gè)配置智慧解壓縮后的文件就會(huì)同步到 nginx html目錄下,就可以訪問(wèn)了
docker run -d -p 4011:4011 --net mynet --name feynman-smart-village-admin -v /usr/local/nginx/html/vr:/opt feynman-smart-village-admin:latest
至此 上傳zip 解壓 至服務(wù)器就完成了~
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
一文詳解如何在SpringMVC的視圖中渲染模型數(shù)據(jù)
SpringMVC是一個(gè)基于Spring框架的Web框架,它提供了一種方便的方式來(lái)處理 HTTP 請(qǐng)求和響應(yīng),在SpringMVC中,視圖是用來(lái)渲染模型數(shù)據(jù)的組件,它們負(fù)責(zé)將模型數(shù)據(jù)轉(zhuǎn)換為HTML、JSON、XML等格式的響應(yīng),在本文中,我們將討論如何在SpringMVC中的視圖中渲染模型數(shù)據(jù)2023-07-07springboot2.6.3讀取不到nacos上的配置文件問(wèn)題
這篇文章主要介紹了springboot2.6.3讀取不到nacos上的配置文件問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07如何利用Spring?MVC實(shí)現(xiàn)RESTful風(fēng)格
這篇文章主要介紹了如何利用Spring?MVC實(shí)現(xiàn)RESTful風(fēng)格,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02java操作oracle數(shù)據(jù)庫(kù)示例
這篇文章主要介紹了java操作oracle數(shù)據(jù)庫(kù)示例,需要的朋友可以參考下2014-04-04