本文主要是介绍springboot上传zip包并解压至服务器nginx目录方式,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
《springboot上传zip包并解压至服务器nginx目录方式》:本文主要介绍springboot上传zip包并解压至服务器nginx目录方式,具有很好的参考价值,希望对大家有所帮助,如有错误...
springboot上传zip包并解压至服务器nginx目录
此案例场编程景为:
从前端上传.zip 包,并进行解压至 docker容器内(服务部署使用了docker),然后容器内部解压目录与 宿主机(linux)nginx 目录的html 进行挂载,这样,就可以通过nginx 来访问上传解压出来的文件了。
那么具体看怎么实现,下面会贴上相关代码:
1.首先需要引入zip相关jar包
<dependency> <groupId>net.lingala.zip4j</groupId> <artifactId>zip4j</artifactId> <version>1.3.1</version> </dependency>
2.然后我直接贴controller 代码了
package com.fnm.feynmandroidan.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 js private RegionVillageService regionVillageService; /** * https://www.cnblogs.com/0616--ataozhijia/p/5022028.html * @param id 村庄id * @param file 上传的vr * @throws IOException * 上传到docker容器内 /opt下,并解压,然后宿主机和容器内路径进行挂载,外部挂载地址为nginx服务器html 目录 */ @ApiOperation("上传vr") @PostMapping("/uploadVr/{id}") public JsonResult uploadVr(@PathVariable("id") Long id, MuphpltipartFile 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请求访问
具体如何挂载呢?
关键:
-v /usr/local/nginx/html/vr:/opt
前面为宿主机nginx的静态文件目录,:后面为容器内部目录,这个配置智慧解压缩后的文件就会同步到 nginx html目录下,就可以访问了
docker run -d -p 4011:4011 --net mynet --name feynman-smart-village-admihttp://www.chinasem.cnn -v /usr/local/nginx/html/vr:/opt feynman-smart-village-admin:latest
至此 上传zip 解压 至服务器就完成了~
总结
这篇关于springboot上传zip包并解压至服务器nginx目录方式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!