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

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

文末获取源码 

项目编号:BS-XX-178

一,项目简介

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

二,环境介绍

语言环境:Java:  jdk1.8

数据库:Mysql: mysql5.7

应用服务器:Tomcat:  tomcat8.5.31

开发工具:IDEA或eclipse

三,系统展示

用户登陆

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

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

四,核心代码展示

package com.sdy.unzipSystem.controllers;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ZipUtil;
import com.sdy.unzipSystem.dto.DirDTO;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.swing.filechooser.FileSystemView;
import java.io.*;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("file")
public class FileController {
    @Value("${root.path}")
    private String rootPath;

    /**
     * 上传文件
     *
     * @param file
     * @param path
     * @return
     */
    @RequestMapping("upload")
    @ResponseBody
    public Map<String, String> upload(@RequestParam("file") MultipartFile file, @RequestParam("path") String path) {
        // 判断文件是否为空
        HashMap<String, String> message = new HashMap<>();
        if (!file.isEmpty()) {
            try {
                // 文件保存路径
                String filePath = path + "/" + file.getOriginalFilename();
                // 转存文件
                file.transferTo(new File(filePath));
                message.put("status", "ok");
            } catch (Exception e) {
//                e.printStackTrace();
                message.put("status", "error");
            }
        }
        return message;
    }


    /**
     * 下载
     *
     * @param response
     * @param path
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/download")
    public String downloads(HttpServletResponse response, @RequestParam("path") String path) throws Exception {
        String fileName = path.split("\\\\")[path.split("\\\\").length - 1];
        File file = new File(path);
        response.reset();
        response.setCharacterEncoding("UTF-8");
        response.setContentType("multipart/form-data");
        response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(fileName, "UTF-8"));
        FileInputStream input = new FileInputStream(file);
        OutputStream out = response.getOutputStream();
        byte[] buff = null;
        buff = new byte[1024];
        Long size = FileUtil.size(file);
        if (size >= 1024 * 20) {
            buff = new byte[1024 * 10];
        }
        if (size < 1024 * 20) {
            buff = new byte[size.intValue()];
        }
        if (size == 0) {
            buff = new byte[50];
        }

        int index = 0;
        while ((index = input.read(buff)) != -1) {
            out.write(buff, 0, index);
            out.flush();
        }
        out.close();
        input.close();
        return null;
    }


    /**
     * 得到系统根路径磁盘列表
     *
     * @return
     */
    @RequestMapping("getRootFileList")
    @ResponseBody
    public HashMap getRootFileList() {
        HashMap<String, Object> rs = new HashMap<>();
        FileSystemView sys = FileSystemView.getFileSystemView();
        File[] files = null;
        if (StrUtil.isNotBlank(rootPath)) {
            return getFileList(rootPath);
        }
        files = File.listRoots();
        ArrayList<DirDTO> list = new ArrayList<>();
        for (int i = 0; i < files.length; i++) {
            System.out.println(files[i] + " -- " + sys.getSystemTypeDescription(files[i]));
            DirDTO d = new DirDTO();
            d.setPath(files[i].toString());
            d.setSimpleName(files[i].toString());
            d.setType(files[i].isDirectory() ? "dir" : "file");
            d.setInfo(sys.getSystemTypeDescription(files[i]));
            if (!FileUtil.isDirectory(files[i])) {
                String readableSize = FileUtil.readableFileSize(files[i]);
                if ("0".equals(readableSize) || "".equals(readableSize)) {
                    d.setReadableSize("-");
                } else {
                    d.setReadableSize(readableSize);
                }
            } else {
                d.setReadableSize("-");
            }
            list.add(d);
        }
        rs.put("status", "ok");
        rs.put("rs", list);
        return rs;
    }


    /**
     * 得到一个目录下的文件列表
     *
     * @param path
     * @return
     */
    @RequestMapping("getFileList")
    @ResponseBody
    public HashMap getFileList(@RequestParam("path") String path) {
        HashMap<String, Object> rs = new HashMap<>();
        FileSystemView sys = FileSystemView.getFileSystemView();
        File root = FileUtil.file(path);
        root.listFiles();
        File[] files = root.listFiles();
        ArrayList<DirDTO> list = new ArrayList<>();
        for (int i = 0; i < files.length; i++) {
            System.out.println(files[i] + " -- " + sys.getSystemTypeDescription(files[i]));
            DirDTO d = new DirDTO();
            d.setPath(files[i].toString());
            d.setSimpleName(files[i].getName());
            d.setType(files[i].isDirectory() ? "dir" : "file");
            d.setInfo(sys.getSystemTypeDescription(files[i]));
            if (!FileUtil.isDirectory(files[i])) {
                String readableSize = FileUtil.readableFileSize(files[i]);
                if ("0".equals(readableSize) || "".equals(readableSize)) {
                    d.setReadableSize("-");
                } else {
                    d.setReadableSize(readableSize);
                }
            } else {
                d.setReadableSize("-");
            }
            list.add(d);
        }
        rs.put("status", "ok");
        rs.put("rs", list);
        return rs;
    }

    /**
     * 删除文件
     *
     * @param path
     * @return
     */
    @RequestMapping("delFile")
    @ResponseBody
    public HashMap delFile(@RequestParam("path") String path) {
        HashMap<String, Object> rs = new HashMap<>();
        Console.log("删除文件:" + path);
        FileUtil.del(path);
        return rs;
    }

    /**
     * 压缩文件或文件夹
     *
     * @param path
     * @return
     */
    @RequestMapping("package")
    @ResponseBody
    public HashMap packagePath(@RequestParam("path") String path) {
        HashMap<String, Object> rs = new HashMap<>();
        ZipUtil.zip(path);
        rs.put("status", "ok");
        return rs;
    }


    /**
     * 解压缩文件或文件夹
     *
     * @param path
     * @return
     */
    @RequestMapping("unPackage")
    @ResponseBody
    public HashMap unPackage(@RequestParam("path") String path) {
        HashMap<String, Object> rs = new HashMap<>();
        if (path.contains(".zip")) {
            ZipUtil.unzip(path);
        }
        rs.put("status", "ok");
        return rs;
    }

    /**
     * 新建文件夹
     *
     * @param path
     * @return
     */
    @RequestMapping("newPackage")
    @ResponseBody
    public HashMap newPackage(@RequestParam("path") String path, @RequestParam("name") String name) {
        HashMap<String, Object> rs = new HashMap<>();
        FileUtil.mkdir(path + "\\" + name);
        rs.put("status", "ok");
        return rs;
    }


    /**
     * 删除文件夹
     *
     * @param path
     * @return
     */
    @RequestMapping("delDir")
    @ResponseBody
    public HashMap delDir(@RequestParam("path") String path) {
        HashMap<String, Object> rs = new HashMap<>();
        Console.log("删除目录:" + path);
        FileUtil.del(path);
        rs.put("status", "ok");
        return rs;
    }


    /**
     * 复制整个文件和文件夹
     *
     * @param oldPath
     * @param newPath
     * @return
     */
    @ResponseBody
    @RequestMapping("copyFileOrDir")
    public HashMap<String, Object> copyFileOrDir(@RequestParam("oldPath") String oldPath, @RequestParam("newPath") String newPath) {
        HashMap<String, Object> rs = new HashMap<>();
        FileUtil.copy(oldPath, newPath, true);
        rs.put("status", "ok");
        return rs;
    }

    /**
     * 移动整个文件和文件夹
     *
     * @param oldPath
     * @param newPath
     * @return
     */
    @ResponseBody
    @RequestMapping("rmFileOrDir")
    public HashMap<String, Object> rmFileOrDir(@RequestParam("oldPath") String oldPath, @RequestParam("newPath") String newPath) {
        HashMap<String, Object> rs = new HashMap<>();
        FileUtil.copy(oldPath, newPath, true);
        FileUtil.del(oldPath);
        rs.put("status", "ok");
        return rs;
    }


    /**
     * 修改文件名或目录名
     *
     * @param path
     * @param newName
     * @return
     */
    @ResponseBody
    @RequestMapping("renameFileOrDir")
    public HashMap<String, Object> renameFileOrDir(@RequestParam("path") String path, @RequestParam("newName") String newName) {
        HashMap<String, Object> rs = new HashMap<>();
        FileUtil.rename(FileUtil.file(path), newName, false, true);
        rs.put("status", "ok");
        return rs;
    }

}
package com.sdy.unzipSystem.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

@RequestMapping("page")
@Controller
public class PathController {

    @RequestMapping("/")
    public String index() {
        return "/index";
    }

    @RequestMapping("login")
    public String login() {
        return "login";
    }

    @RequestMapping("loginValiData")
    public String loginValiData(@RequestParam("pass") String pass, HttpServletRequest request){
        request.getSession().setAttribute("adminpass",pass);
        return "login";
    }

    @RequestMapping("{page}")
    public String info(@PathVariable("page") String page){
        return page;
    }
}

五,项目总结

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

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

编程千纸鹤

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值