欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

springboot上传zip包并解压至服务器nginx目录方式,

来源: javaer 分享于  点击 38902 次 点评:227

springboot上传zip包并解压至服务器nginx目录方式,


目录
  • springboot上传zip包并解压至服务器nginx目录
    • 1.首先需要引入zip相关jar包
    • 2.然后我直接贴controller 代码了
    • 3.说明上面上传的/opt/目录为docker容器里面的目录
  • 总结

    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.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容器内 /opt下,并解压,然后宿主机和容器内路径进行挂载,外部挂载地址为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请求访问

    具体如何挂载呢?

    关键:

    -v /usr/local/nginx/html/vr:/opt

    前面为宿主机nginx的静态文件目录,:后面为容器内部目录,这个配置智慧解压缩后的文件就会同步到 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 解压 至服务器就完成了~

    总结

    以上为个人经验,希望能给大家一个参考,也希望大家多多支持3672js教程。

    您可能感兴趣的文章:
    • SpringBoot中开启GZIP压缩的方式
    • SpringBoot3实现Gzip压缩优化的技术指南
    • SpringBoot中使用EasyExcel并行导出多个excel文件并压缩zip后下载的代码详解
    • SpringBoot启用GZIP压缩的代码工程
    • springboot多文件或者文件夹压缩成zip的方法
    • Docker部署SpringBoot项目,本地运行正常,部署至服务器报错zip file closed问题
    相关栏目:

    用户点评