Java springboot compressed file upload, decompress, delete compressed package
1. Configuration file
In
file-server: path: \material-main\ # Name it casually。Notice,in spite ofwindowsstilllinux,The path does not require a disk letter,Use code to identify
2. Tools
If you need to delete the compressed package, please unpack the comment below
import .slf4j.Slf4j; import ; import ; import ; import ; import ; import ; import ; @Slf4j public class UnzipUtils { /** * Absolute path to pass the file */ public static void zipUncompress(String inputFile) { ("UnzipUtils starts decompression"); File oriFile = new File(inputFile); // Determine whether the source file exists String destDirPath = (".zip", ""); FileOutputStream fos = null; InputStream is = null; ZipFile zipFile = null; try { //Create a compressed file object zipFile = new ZipFile(oriFile); //Start decompression Enumeration<?> entries = (); while (()) { ZipEntry entry = (ZipEntry) (); // If it is a folder, create a folder if (()) { (); } else { // If it is a file, create a file first and then use io stream to copy the contents. File targetFile = new File(destDirPath + "/" + ()); // Ensure that the parent folder of this file must exist if (!().exists()) { ().mkdirs(); } (); // Write the compressed file contents into this file is = (entry); fos = new FileOutputStream(targetFile); int len; byte[] buf = new byte[1024]; while ((len = (buf)) != -1) { (buf, 0, len); } } } } catch (Exception e) { ("Exception during file decompression,{}", e); } finally { // Close the flow order, open first and then close try { if (fos != null) { (); } if (is != null) { (); } if (zipFile != null) { (); } } catch (IOException e) { ("File stream closing exception,{}", e); } } //Delete the file after decompression// if (()) { // (); // (); // if (()) { // (); // (); // if (()) { // ("File not deleted");// } // } // } ("UnzipUtils decompression is complete"); } }
3. Use
controller layer. Note that I use swagger3, which is springdoc.
Use swagger2 (springfox), the writing method is not so troublesome
package ; import ; import ; import .; import .; import ; import ; import .*; import ; @RestController @RequestMapping("/files") @Tag(name = "FilesController", description = "File Management") public class FilesController { @Autowired private IFilesService filesService; //Swagger3 writing method @PostMapping(value = "/file/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) @Operation(summary = "Upload zip file to server") public Result uploadFile(@RequestPart("file") MultipartFile file) { return (file); } // How to write swagger2// @ApiOperation("Upload zip file")// @PostMapping("/file/upload") // public Result uploadFile(MultipartFile file) { // return (file); // } }
Service specific business
package ; import ; import ; import ; import ; import ; import ; import ; import .slf4j.Slf4j; import .; import ; import ; import ; import ; import ; import ; import ; import ; import ; import .*; @Service @Slf4j public class FilesServiceImpl implements IFilesService { @Value("${}") private String uploadPath; private String getUploadPath() {//Intelligent identification complete path Properties props = (); String property = (""); String userHomePath = (""); String filePath = "";//File storage address if (("Windows")) { String[] arr = (":"); String pan = arr[0] + ":";//Winters will take the first disk letter filePath = pan + uploadPath; } else if (("Linux")) { filePath = uploadPath; } return filePath; } @Override public Result uploadFile(MultipartFile file) { String originalFilename = ();//Original name if ((originalFilename) || !(".zip")) { return new Result(ResultCode.FILE_WRONG); } String newName = ().toString().replace("-", "");//uuid as the new name of the folder, no duplication String zipName = newName + ".zip";//uuid as the new name of the compressed file, no duplication //Create folder, today's date String date = MyDateUtils.parseDate2String(new Date()); //File storage location, add one layer of date String path = getUploadPath() + date; //Return result Map<String, Object> pathMap = new HashMap<>(); InputStream inputStream = null;//File Stream try { inputStream = (); //Detection create folder Path directory = (path); if (!(directory)) { (directory); } Long size = (inputStream, (zipName));//Upload the file, the return value is the file size ("zip_size", size); } catch (Exception e) { ("zip_size", 0); return new Result(e); } finally { try { if (inputStream != null) { (); } } catch (IOException e) { throw new CommonException(()); } } String zipPath = path + + zipName; (zipPath); ("main_path", zipPath); ("folder_path", path + + newName); ("zip_size_cent", "kb"); return new Result(pathMap); } @Override public Result fileDel(String downloadPath) { String absolutePath = ((), downloadPath); boolean b = (absolutePath); return new Result(b); } }
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.