springboot上傳zip包并解壓至服務器nginx目錄方式
springboot上傳zip包并解壓至服務器nginx目錄
此案例場景為:
從前端上傳.zip 包,并進行解壓至 docker容器內(nèi)(服務部署使用了docker),然后容器內(nèi)部解壓目錄與 宿主機(linux)nginx 目錄的html 進行掛載,這樣,就可以通過nginx 來訪問上傳解壓出來的文件了。
那么具體看怎么實現(xiàn),下面會貼上相關(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下,并解壓,然后宿主機和容器內(nèi)路徑進行掛載,外部掛載地址為nginx服務器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");
//上傳文件到服務器/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 目錄的路徑地址拼接為可訪問的url ,進行修改實體對象
entity.setVrUrl("https://**ed.*****.com/vr/"+entity.getValue()+"/"+substring+"/output/index.html");
regionVillageService.updateEntity(entity);
return JsonResult.OK();
}
}
3.說明上面上傳的/opt/目錄為docker容器里面的目錄
那么我需要將容器里面的目錄和宿主機nginx目錄進行掛載后,才能通過http請求訪問
具體如何掛載呢?
關(guān)鍵:
-v /usr/local/nginx/html/vr:/opt
前面為宿主機nginx的靜態(tài)文件目錄,:后面為容器內(nèi)部目錄,這個配置智慧解壓縮后的文件就會同步到 nginx html目錄下,就可以訪問了
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 解壓 至服務器就完成了~
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
一文詳解如何在SpringMVC的視圖中渲染模型數(shù)據(jù)
SpringMVC是一個基于Spring框架的Web框架,它提供了一種方便的方式來處理 HTTP 請求和響應,在SpringMVC中,視圖是用來渲染模型數(shù)據(jù)的組件,它們負責將模型數(shù)據(jù)轉(zhuǎn)換為HTML、JSON、XML等格式的響應,在本文中,我們將討論如何在SpringMVC中的視圖中渲染模型數(shù)據(jù)2023-07-07
springboot2.6.3讀取不到nacos上的配置文件問題
這篇文章主要介紹了springboot2.6.3讀取不到nacos上的配置文件問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07
如何利用Spring?MVC實現(xiàn)RESTful風格
這篇文章主要介紹了如何利用Spring?MVC實現(xiàn)RESTful風格,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02

