文件在线压缩与解压系统

简介: 文件在线压缩与解压系统

项目编号:BS-XX-178

一,环境介绍

语言环境:Java:  jdk1.8

数据库:Mysql: mysql5.7

应用服务器:Tomcat:  tomcat8.5.31

开发工具:IDEA或eclipse

二,项目简介

本项目主要实现一个在线文件压缩和解压的在线平台系统,完成对文件的相关管理功能。主要实现对文件的上传下载,在线压缩,使用ZIP协议将文件或文件夹压缩为ZIP格式的压缩包文件。系统功能目标明确,可以在此基础上进行相应的扩展处理。

三,系统展示

用户登录

登录后进入文件根路径

进入相应文件夹

在线压缩

可以进行上传下载和删除等

四,核心代码展示

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;
    }
}

五,相关作品展示

基于Java开发、Python开发、PHP开发、C#开发等相关语言开发的实战项目

基于Nodejs、Vue等前端技术开发的前端实战项目

基于微信小程序和安卓APP应用开发的相关作品

基于51单片机等嵌入式物联网开发应用

基于各类算法实现的AI智能应用

基于大数据实现的各类数据管理和推荐系统

相关文章
|
7月前
|
移动开发 JavaScript 前端开发
在项目中引入和使用 SCSS 文件
在项目中引入和使用 SCSS 文件
779 156
|
11月前
|
人工智能 架构师 数据挖掘
提示工程:与你AI对话的“超能力”
提示工程:与你AI对话的“超能力”
481 93
|
存储
【机组期末速成】指令系统|机器指令概述|操作数类型与操作类型|寻址方式|指令格式
【机组期末速成】指令系统|机器指令概述|操作数类型与操作类型|寻址方式|指令格式
624 1
|
存储 人工智能 文字识别
VideoRAG:长视频理解的检索增强生成技术,支持多模态信息提取,能与任何 LVLM 兼容
VideoRAG 是一种用于长视频理解的检索增强生成技术,通过提取视频中的视觉对齐辅助文本,帮助大型视频语言模型更好地理解和处理长视频内容。
1273 10
VideoRAG:长视频理解的检索增强生成技术,支持多模态信息提取,能与任何 LVLM 兼容
|
存储 Kubernetes 监控
在K8S中,ELK是如何实现及如何优化的ES?
在K8S中,ELK是如何实现及如何优化的ES?
|
消息中间件 资源调度 安全
操作系统----临界区,临界资源,互斥量,互斥对象
操作系统----临界区,临界资源,互斥量,互斥对象
894 2
|
前端开发 JavaScript UED
window.open()用法详解
window.open()用法详解
1515 0
|
存储 缓存 数据安全/隐私保护
移动应用中的离线模式是一种重要的功能
【5月更文挑战第16天】移动应用的离线模式通过数据缓存和存储确保无网时仍能使用部分功能。数据同步采用延迟策略,用户更改信息后在网络恢复时同步至服务器。为保障安全,敏感数据加密存储并定期备份。开发者还需关注用户体验、电量性能及错误处理,以实现稳定可靠的离线模式,提升用户体验。
2199 0
|
Ubuntu 网络安全 C语言
vscode 编译多个当前目录下cpp文件,报错未定义标识符的问题
vscode 编译多个当前目录下cpp文件,报错未定义标识符的问题
1531 0
|
JSON API 数据格式
python对话通义千问
python对话通义千问
3354 1