Springboot uploads zip package and decompresses to server nginx directory
This case scenario is:
Upload the .zip package from the front end and decompress it into the docker container (the service deployment uses docker), and then mount the decompressed directory inside the container with the html of the host (linux) nginx directory. In this way, you can access the uploaded decompressed files through nginx.
So, it depends on how to implement it. The following will post the relevant code:
1. First, you need to introduce zip-related jar packages
<dependency> <groupId>.zip4j</groupId> <artifactId>zip4j</artifactId> <version>1.3.1</version> </dependency>
2. Then I posted the controller code directly
package ; import ; import ; import ; import ; import .slf4j.Slf4j; import .; import .; import ; import ; import ; import ; import ; import ; import ; import ; import ; /** * @author * @date 2020/12/24--16:41 */ @Slf4j @RestController @RequestMapping("/admin/open") public class UploadVrController { @Autowired private RegionVillageService regionVillageService; /** * /0616--ataozhijia/p/ * @param id Village id * @param file uploaded vr * @throws IOException * Upload it to /opt in the docker container, decompress it, and then mount the host and the paths in the container. The external mount address is nginx server html directory */ @ApiOperation("Upload vr") @PostMapping("/uploadVr/{id}") public JsonResult uploadVr(@PathVariable("id") Long id, MultipartFile file) throws IOException, ZipException { RegionVillage entity = (id); File file1 = new File("/opt/" + ()+".zip"); //Upload the file to the server/opt/ directory //File file1 = new File("E:/zip/" + ()+".zip"); (file1,()); ZipFile zipFile = new ZipFile(file1); //The vr given is gbk. If the zip package you upload is utf-8, then change it to utf-8 here ("gbk"); String path="/opt/"+(); //Extract to the specified directory (path); String substring = ().substring(0, ().indexOf(".")); //Stitize the path address decompressed to nginx directory into accessible urls and modify the entity object ("https://**ed.*****.com/vr/"+()+"/"+substring+"/output/"); (entity); return (); } }
3. Explain that the /opt/ directory uploaded above is the directory in the docker container
Then I need to mount the directory in the container and the host nginx directory before accessing through http request
How to mount it specifically?
key:
-v /usr/local/nginx/html/vr:/opt
The first is the static file directory of the host nginx, and the following is the internal directory of the container. The files after smart decompression of this configuration will be synchronized to the nginx html directory and can be accessed.
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
At this point, upload zip, decompress and then complete it~
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.