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)文章
mybatis自定義參數(shù)類型轉(zhuǎn)換器數(shù)據(jù)庫(kù)字段加密脫敏
這篇文章主要為大家介紹了mybatis自定義參數(shù)類型轉(zhuǎn)換器數(shù)據(jù)庫(kù)字段加密脫敏,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
Java實(shí)現(xiàn)提取不重復(fù)的整數(shù)實(shí)例
這篇文章主要介紹了Java實(shí)現(xiàn)提取不重復(fù)的整數(shù)實(shí)例,具有一定借鑒價(jià)值,需要的朋友可以參考下2017-12-12
Java開(kāi)發(fā)者必備10大數(shù)據(jù)工具和框架
這篇文章主要為大家詳細(xì)介紹了Java開(kāi)發(fā)者必備10大數(shù)據(jù)工具和框架,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
Spring Security中防護(hù)CSRF功能詳解
這篇文章主要介紹了Spring Security中防護(hù)CSRF功能,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01
Java 基礎(chǔ)語(yǔ)法之解析 Java 的包和繼承
包是組織類的一種方式,繼承顧名思義,比如誰(shuí)繼承了長(zhǎng)輩的產(chǎn)業(yè),其實(shí)這里的繼承和我們生活中的繼承很類似,下面文字將為大家詳細(xì)介紹Java的包和繼承2021-09-09
解決tk mapper 通用mapper的bug問(wèn)題
這篇文章主要介紹了解決tk mapper 通用mapper的bug問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06

