当前位置:   article > 正文

文件在线压缩与解压|基于Springboot实现文件在线压缩与解压_springboot解压压缩包

springboot解压压缩包

作者主页:编程指南针

作者简介:Java领域优质创作者、CSDN博客专家 、掘金特邀作者、多年架构师设计经验、腾讯课堂常驻讲师

主要内容:Java项目、毕业设计、简历模板、学习资料、面试题库、技术互助

收藏点赞不迷路  关注作者有好处

文末获取源码 

项目编号:BS-XX-178

一,项目简介

   主要使用 gzip协议对上传到服务器的文件进行在线压缩和解压操作。

二,环境介绍

语言环境:Java:  jdk1.8

数据库:Mysql: mysql5.7

应用服务器:Tomcat:  tomcat8.5.31

开发工具:IDEA或eclipse

三,系统展示

用户登陆

 进入指定文件的目录:展示目 录下文件和文件夹列表

 在线对文件进行压缩或解压,也可以进行删除操作

 

四,核心代码展示

  1. package com.sdy.unzipSystem.controllers;
  2. import cn.hutool.core.io.FileUtil;
  3. import cn.hutool.core.lang.Console;
  4. import cn.hutool.core.util.StrUtil;
  5. import cn.hutool.core.util.ZipUtil;
  6. import com.sdy.unzipSystem.dto.DirDTO;
  7. import org.springframework.beans.factory.annotation.Value;
  8. import org.springframework.stereotype.Controller;
  9. import org.springframework.web.bind.annotation.RequestMapping;
  10. import org.springframework.web.bind.annotation.RequestParam;
  11. import org.springframework.web.bind.annotation.ResponseBody;
  12. import org.springframework.web.multipart.MultipartFile;
  13. import javax.servlet.http.HttpServletResponse;
  14. import javax.swing.filechooser.FileSystemView;
  15. import java.io.*;
  16. import java.net.URLEncoder;
  17. import java.util.ArrayList;
  18. import java.util.HashMap;
  19. import java.util.Map;
  20. @Controller
  21. @RequestMapping("file")
  22. public class FileController {
  23. @Value("${root.path}")
  24. private String rootPath;
  25. /**
  26. * 上传文件
  27. *
  28. * @param file
  29. * @param path
  30. * @return
  31. */
  32. @RequestMapping("upload")
  33. @ResponseBody
  34. public Map<String, String> upload(@RequestParam("file") MultipartFile file, @RequestParam("path") String path) {
  35. // 判断文件是否为空
  36. HashMap<String, String> message = new HashMap<>();
  37. if (!file.isEmpty()) {
  38. try {
  39. // 文件保存路径
  40. String filePath = path + "/" + file.getOriginalFilename();
  41. // 转存文件
  42. file.transferTo(new File(filePath));
  43. message.put("status", "ok");
  44. } catch (Exception e) {
  45. // e.printStackTrace();
  46. message.put("status", "error");
  47. }
  48. }
  49. return message;
  50. }
  51. /**
  52. * 下载
  53. *
  54. * @param response
  55. * @param path
  56. * @return
  57. * @throws Exception
  58. */
  59. @RequestMapping(value = "/download")
  60. public String downloads(HttpServletResponse response, @RequestParam("path") String path) throws Exception {
  61. String fileName = path.split("\\\\")[path.split("\\\\").length - 1];
  62. File file = new File(path);
  63. response.reset();
  64. response.setCharacterEncoding("UTF-8");
  65. response.setContentType("multipart/form-data");
  66. response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(fileName, "UTF-8"));
  67. FileInputStream input = new FileInputStream(file);
  68. OutputStream out = response.getOutputStream();
  69. byte[] buff = null;
  70. buff = new byte[1024];
  71. Long size = FileUtil.size(file);
  72. if (size >= 1024 * 20) {
  73. buff = new byte[1024 * 10];
  74. }
  75. if (size < 1024 * 20) {
  76. buff = new byte[size.intValue()];
  77. }
  78. if (size == 0) {
  79. buff = new byte[50];
  80. }
  81. int index = 0;
  82. while ((index = input.read(buff)) != -1) {
  83. out.write(buff, 0, index);
  84. out.flush();
  85. }
  86. out.close();
  87. input.close();
  88. return null;
  89. }
  90. /**
  91. * 得到系统根路径磁盘列表
  92. *
  93. * @return
  94. */
  95. @RequestMapping("getRootFileList")
  96. @ResponseBody
  97. public HashMap getRootFileList() {
  98. HashMap<String, Object> rs = new HashMap<>();
  99. FileSystemView sys = FileSystemView.getFileSystemView();
  100. File[] files = null;
  101. if (StrUtil.isNotBlank(rootPath)) {
  102. return getFileList(rootPath);
  103. }
  104. files = File.listRoots();
  105. ArrayList<DirDTO> list = new ArrayList<>();
  106. for (int i = 0; i < files.length; i++) {
  107. System.out.println(files[i] + " -- " + sys.getSystemTypeDescription(files[i]));
  108. DirDTO d = new DirDTO();
  109. d.setPath(files[i].toString());
  110. d.setSimpleName(files[i].toString());
  111. d.setType(files[i].isDirectory() ? "dir" : "file");
  112. d.setInfo(sys.getSystemTypeDescription(files[i]));
  113. if (!FileUtil.isDirectory(files[i])) {
  114. String readableSize = FileUtil.readableFileSize(files[i]);
  115. if ("0".equals(readableSize) || "".equals(readableSize)) {
  116. d.setReadableSize("-");
  117. } else {
  118. d.setReadableSize(readableSize);
  119. }
  120. } else {
  121. d.setReadableSize("-");
  122. }
  123. list.add(d);
  124. }
  125. rs.put("status", "ok");
  126. rs.put("rs", list);
  127. return rs;
  128. }
  129. /**
  130. * 得到一个目录下的文件列表
  131. *
  132. * @param path
  133. * @return
  134. */
  135. @RequestMapping("getFileList")
  136. @ResponseBody
  137. public HashMap getFileList(@RequestParam("path") String path) {
  138. HashMap<String, Object> rs = new HashMap<>();
  139. FileSystemView sys = FileSystemView.getFileSystemView();
  140. File root = FileUtil.file(path);
  141. root.listFiles();
  142. File[] files = root.listFiles();
  143. ArrayList<DirDTO> list = new ArrayList<>();
  144. for (int i = 0; i < files.length; i++) {
  145. System.out.println(files[i] + " -- " + sys.getSystemTypeDescription(files[i]));
  146. DirDTO d = new DirDTO();
  147. d.setPath(files[i].toString());
  148. d.setSimpleName(files[i].getName());
  149. d.setType(files[i].isDirectory() ? "dir" : "file");
  150. d.setInfo(sys.getSystemTypeDescription(files[i]));
  151. if (!FileUtil.isDirectory(files[i])) {
  152. String readableSize = FileUtil.readableFileSize(files[i]);
  153. if ("0".equals(readableSize) || "".equals(readableSize)) {
  154. d.setReadableSize("-");
  155. } else {
  156. d.setReadableSize(readableSize);
  157. }
  158. } else {
  159. d.setReadableSize("-");
  160. }
  161. list.add(d);
  162. }
  163. rs.put("status", "ok");
  164. rs.put("rs", list);
  165. return rs;
  166. }
  167. /**
  168. * 删除文件
  169. *
  170. * @param path
  171. * @return
  172. */
  173. @RequestMapping("delFile")
  174. @ResponseBody
  175. public HashMap delFile(@RequestParam("path") String path) {
  176. HashMap<String, Object> rs = new HashMap<>();
  177. Console.log("删除文件:" + path);
  178. FileUtil.del(path);
  179. return rs;
  180. }
  181. /**
  182. * 压缩文件或文件夹
  183. *
  184. * @param path
  185. * @return
  186. */
  187. @RequestMapping("package")
  188. @ResponseBody
  189. public HashMap packagePath(@RequestParam("path") String path) {
  190. HashMap<String, Object> rs = new HashMap<>();
  191. ZipUtil.zip(path);
  192. rs.put("status", "ok");
  193. return rs;
  194. }
  195. /**
  196. * 解压缩文件或文件夹
  197. *
  198. * @param path
  199. * @return
  200. */
  201. @RequestMapping("unPackage")
  202. @ResponseBody
  203. public HashMap unPackage(@RequestParam("path") String path) {
  204. HashMap<String, Object> rs = new HashMap<>();
  205. if (path.contains(".zip")) {
  206. ZipUtil.unzip(path);
  207. }
  208. rs.put("status", "ok");
  209. return rs;
  210. }
  211. /**
  212. * 新建文件夹
  213. *
  214. * @param path
  215. * @return
  216. */
  217. @RequestMapping("newPackage")
  218. @ResponseBody
  219. public HashMap newPackage(@RequestParam("path") String path, @RequestParam("name") String name) {
  220. HashMap<String, Object> rs = new HashMap<>();
  221. FileUtil.mkdir(path + "\\" + name);
  222. rs.put("status", "ok");
  223. return rs;
  224. }
  225. /**
  226. * 删除文件夹
  227. *
  228. * @param path
  229. * @return
  230. */
  231. @RequestMapping("delDir")
  232. @ResponseBody
  233. public HashMap delDir(@RequestParam("path") String path) {
  234. HashMap<String, Object> rs = new HashMap<>();
  235. Console.log("删除目录:" + path);
  236. FileUtil.del(path);
  237. rs.put("status", "ok");
  238. return rs;
  239. }
  240. /**
  241. * 复制整个文件和文件夹
  242. *
  243. * @param oldPath
  244. * @param newPath
  245. * @return
  246. */
  247. @ResponseBody
  248. @RequestMapping("copyFileOrDir")
  249. public HashMap<String, Object> copyFileOrDir(@RequestParam("oldPath") String oldPath, @RequestParam("newPath") String newPath) {
  250. HashMap<String, Object> rs = new HashMap<>();
  251. FileUtil.copy(oldPath, newPath, true);
  252. rs.put("status", "ok");
  253. return rs;
  254. }
  255. /**
  256. * 移动整个文件和文件夹
  257. *
  258. * @param oldPath
  259. * @param newPath
  260. * @return
  261. */
  262. @ResponseBody
  263. @RequestMapping("rmFileOrDir")
  264. public HashMap<String, Object> rmFileOrDir(@RequestParam("oldPath") String oldPath, @RequestParam("newPath") String newPath) {
  265. HashMap<String, Object> rs = new HashMap<>();
  266. FileUtil.copy(oldPath, newPath, true);
  267. FileUtil.del(oldPath);
  268. rs.put("status", "ok");
  269. return rs;
  270. }
  271. /**
  272. * 修改文件名或目录名
  273. *
  274. * @param path
  275. * @param newName
  276. * @return
  277. */
  278. @ResponseBody
  279. @RequestMapping("renameFileOrDir")
  280. public HashMap<String, Object> renameFileOrDir(@RequestParam("path") String path, @RequestParam("newName") String newName) {
  281. HashMap<String, Object> rs = new HashMap<>();
  282. FileUtil.rename(FileUtil.file(path), newName, false, true);
  283. rs.put("status", "ok");
  284. return rs;
  285. }
  286. }
  1. package com.sdy.unzipSystem.controllers;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.web.bind.annotation.PathVariable;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RequestParam;
  6. import javax.servlet.http.HttpServletRequest;
  7. @RequestMapping("page")
  8. @Controller
  9. public class PathController {
  10. @RequestMapping("/")
  11. public String index() {
  12. return "/index";
  13. }
  14. @RequestMapping("login")
  15. public String login() {
  16. return "login";
  17. }
  18. @RequestMapping("loginValiData")
  19. public String loginValiData(@RequestParam("pass") String pass, HttpServletRequest request){
  20. request.getSession().setAttribute("adminpass",pass);
  21. return "login";
  22. }
  23. @RequestMapping("{page}")
  24. public String info(@PathVariable("page") String page){
  25. return page;
  26. }
  27. }

五,项目总结

系统运行完整,界面简洁大方

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/码创造者/article/detail/984622
推荐阅读
相关标签
  

闽ICP备14008679号