java实现系统文件管理
java实现系统文件管理
环境:jdk17+springboot+Vue+ElementUI
背景:公司所做的项目需要别的系统向我们服务器上传文件,当我们需要查看这些文件什么时候上传的、文件数据是怎样的,只能去机房,排查问题效率较低,做此页面,可快速查看上传的文件信息,且可下载到本地查看。为了生产安全,不支持修改及上传文件,如果有的朋友想做,可自行查找资料。
需求:实现系统文件的查询及下载。展示系统的文件信息,如文件名、文件大小、最后更新时间及权限等。
注意
:本篇文章是以window系统做的样例,不过一般服务器都是在linux系统,只需将前端的初始地址换成linux地址,一般格式为:/home/app。
效果图
直接上代码,前端代码:
<template><div><div><el-col :xl="4" :lg="5"><el-input v-model="curPath" label-width='80px' size="small" type="text">当前位置:</el-input></el-col><el-button type="primary" size="small" @click="getParentData()" icon="el-icon-back">返回上级</el-button><el-button type="primary" size="small" @click="refresh()" icon="el-icon-refresh">刷新</el-button></div><el-table :data="fileList" v-loading="tableLoading"><el-table-column label="名称" prop="fileName"><!-- eslint-disable-next-line--><template slot-scope="scope"><el-button type="text" v-if="!scope.row.fileType" @click="getSonData(scope.row)">{{scope.row.fileName}}</el-button><span v-if="scope.row.fileType">{{ scope.row.fileName }}</span></template></el-table-column><el-table-column label="类型" align="center"><!-- eslint-disable-next-line--><template slot-scope="scope"><div>{{ typeName(scope.row.fileType) }}</div></template></el-table-column><el-table-column label="大小" prop="fileSize"></el-table-column><el-table-column label="更新时间" prop="lastModifiedDate"></el-table-column><el-table-column align="center" label="权限" width="120"><!-- eslint-disable-next-line--><template slot-scope="scope"><div>{{ getAuthority(scope.row) }}</div></template></el-table-column><el-table-column label="操作"><!-- eslint-disable-next-line--><template slot-scope="scope"><el-button size="mini" v-if="scope.row.fileType" type="text" @click="download(scope.row.path)">下载</el-button></template></el-table-column></el-table></div>
</template>
<script>
import axios from "axios";export default {name: 'App',data() {return {curPath: "D:\\",fileList: [],tableLoading: false,}},created() {this.getData()},methods: {getData: function () {const vm = thisconst params = {path: this.curPath}axios({method: 'get',url: "/sysFile/getSysFiles",params}).then(res => {const result = res.dataif (result && result.code === 200) {this.fileList = result.dataconsole.log(this.fileList)}vm.tableLoading = false})},getSonData(row) {this.curPath = row.paththis.getData()},getParentData() {if (this.curPath === '') {this.$message({type: 'warning',message: '没有上级!'})return}// linux系统中,将 \\ 改为 / 即可this.curPath = this.curPath.slice(0, this.curPath.lastIndexOf("\\"))this.getData()},refresh() {this.type = 0this.getData()},download(path) {const params = {path: path}axios({method: 'get',url: "/sysFile/downloadFile",responseType: 'blob',params}).then(res => {// linux系统中,将 \\ 改为 / 即可const fileName = path.slice(path.lastIndexOf("\\") + 1, path.length)const blob = new Blob([res.data])if ('download' in document.createElement('a')) {// 非IE下载console.log('非IE')const elink = document.createElement('a')elink.download = fileNameelink.style.display = 'none'elink.href = URL.createObjectURL(blob)document.body.appendChild(elink)elink.click()URL.revokeObjectURL(elink.href)// 释放URL 对象document.body.removeChild(elink)} else {// IE10+下载navigator.msSaveBlob(blob, fileName)}})},typeName(type) {if (type) {return "文件"}return "文件夹"},getAuthority(row) {let authority = ''if (row.canRead) {authority = authority + 'r'} else {authority = authority + '-'}if (row.canWrite) {authority = authority + 'w'} else {authority = authority + '-'}if (row.canExecute) {authority = authority + 'x'} else {authority = authority + '-'}return authority}}
}
</script><style>
.el-header, .el-footer {background-color: #B3C0D1;color: #333;text-align: center;line-height: 60px;
}.el-aside {background-color: #D3DCE6;color: #333;text-align: center;line-height: 200px;
}.el-main {background-color: #E9EEF3;color: #333;text-align: center;line-height: 160px;
}body > .el-container {margin-bottom: 40px;
}.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {line-height: 260px;
}.el-container:nth-child(7) .el-aside {line-height: 320px;
}
</style>
后端代码:
package org.wjg.onlinexml.controller;import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.wjg.onlinexml.po.Result;
import org.wjg.onlinexml.po.SysFileDo;import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;@RestController
public class SysFileController {private static final SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");@GetMapping("/getSysFiles")public Result getSysFiles(@RequestParam String path) {try {File files = null;if (!StringUtils.isEmpty(path)) {files = new File(path);} else {return Result.builder().code(200).msg("路径为空").build();}if (!files.exists()) {return Result.builder().code(200).msg("文件不存在").build();}List<SysFileDo> result = new ArrayList<>();for (File file : files.listFiles()) {SysFileDo sysFileDo = new SysFileDo();//文件名sysFileDo.setFileName(file.getName());//是否为文件sysFileDo.setFileType(file.isFile());//文件大小,文件夹大小一般形式为0,不过可以自己遍历文件夹下的内容计算该文件夹的大小sysFileDo.setFileSize(file.length() / 1024 + "KB");//是否可执行sysFileDo.setCanExecute(file.canExecute());//是否可读sysFileDo.setCanRead(file.canRead());//是否可写(以上三种权限跟实际可能会有偏差的)sysFileDo.setCanWrite(file.canWrite());// 最后修改时间sysFileDo.setLastModifiedDate(simple.format(new Date(file.lastModified())));//最后修改时间的时间戳,方便排序sysFileDo.setLastModified(file.lastModified());//当前路径sysFileDo.setPath(file.getAbsolutePath());result.add(sysFileDo);}Collections.sort(result, Comparator.comparing(SysFileDo::getLastModified));Collections.reverse(result);return Result.builder().code(200).msg("查询成功").data(result).build();} catch (Exception e) {e.printStackTrace();}return Result.builder().build();}@RequestMapping("/downloadFile")public ResponseEntity<byte[]> download(@RequestParam String path) throws IOException {File file = new File(path);HttpHeaders httpHeaders = new HttpHeaders();httpHeaders.setContentDispositionFormData("attachment", "");return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), httpHeaders, HttpStatus.CREATED);}}
实体类:
@Data
@NoArgsConstructor
public class SysFileDo {private String fileName;private boolean fileType;private String fileSize;private boolean canRead;private boolean canExecute;private boolean canWrite;private String lastModifiedDate;private long lastModified;private String path;
}
package org.wjg.onlinexml.po;import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;@Data
@Builder(toBuilder = true)
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> {private int code;private String msg;private T data;
}
好了,主要的代码就这些。还有两个依赖:
<!-- 处理文件上传的 Java 库 --><dependency><groupId>commons-fileupload</groupId><artifactId>commons-fileupload</artifactId><version>1.5</version></dependency><!-- Apache 的开源工具库,包含了许多实用的文件操作、流操作相关的功能和工具类,比如文件读写、文件和目录的操作、流的处理和转换 --><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.5</version></dependency>
相关文章:

java实现系统文件管理
java实现系统文件管理 环境:jdk17springbootVueElementUI 背景:公司所做的项目需要别的系统向我们服务器上传文件,当我们需要查看这些文件什么时候上传的、文件数据是怎样的,只能去机房,排查问题效率较低,…...
pytorch快速入门(一)—— 基本工具及平台介绍
前言 该pytorch学习笔记应该配合b站小土堆的《pytorch深度学习快速入门教程》使用 环境配置:Anaconda Python编译器:pycharm、jupyter 两大法宝函数 dir():知道包中有什么东西(函数 / 属性..…...

『功能项目』怪物的有限状态机【42】
本章项目成果展示 我们打开上一篇41项目优化 - 框架加载资源的项目, 本章要做的事情是按照框架的思想构建项目并完成怪物的自动巡逻状态,当主角靠近怪物时,怪物会朝向主角释放技能 首先新建脚本:BossCtrl.cs (通常把xxxCtrl.cs脚…...

【C++】模板进阶:深入解析模板特化
C语法相关知识点可以通过点击以下链接进行学习一起加油!命名空间缺省参数与函数重载C相关特性类和对象-上篇类和对象-中篇类和对象-下篇日期类C/C内存管理模板初阶String使用String模拟实现Vector使用及其模拟实现List使用及其模拟实现容器适配器Stack与Queue 本章将…...

Python数据分析-世界上最富有的1000人
一、研究背景 随着全球化的加速发展和技术的进步,财富分配问题日益成为全球关注的焦点。财富的不平等现象日益明显,少数极富有的个人掌握了全球大部分的财富资源。了解全球最富有个人的财富分布及其背后的行业和国家因素,对于分析全球经济趋…...
CSS中隐藏滚动条的同时保留滚动功能
在CSS中,我们可以通过一些技巧来隐藏滚动条,同时保留滚动功能。以下是几种常用的方法和具体的实现步骤。 1. 使用 overflow 和 ::-webkit-scrollbar 这种方法适用于大多数现代浏览器。通过设置 overflow 属性启用滚动,同时利用 ::-webkit-s…...

我的标志:奇特的头像
<!DOCTYPE html> <html lang="zh-CN"> <head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>与妖为邻</title><style>figu…...

中国空间计算产业链发展分析
2024中国空间计算产业链拆解 空间计算设备主要包括AR、VR、MR等终端设备。VR设备通常包括头戴式显示器(VR头盔)、手柄或追踪器等组件,用以完全封闭用户视野,营造虚拟环境体验。这些设备配备高分辨率显示屏、内置传感器和跟踪器。 …...

DAY14信息打点-JS 架构框架识别泄漏提取API 接口枚举FUZZ 爬虫插件项目
本课意义: 1.如何从表现中的JS提取价值信息 2.如何从地址中FUZZ提取未知的JS文件 3.如何从JS开放框架WebPack进行测试 一、JS 前端架构-识别&分析 在JS中寻找更多的URL地址,在JS代码逻辑(加密算法、APIKey配置、验证逻辑)中进…...

TS - tsconfig.json 和 tsconfig.node.json 的关系,如何在TS 中使用 JS 不报错
目录 1,前言2,二者关系2.1,使用 3,遇到的问题3.1,TS 中使用 JS 1,前言 通过 Vite 创建的 Vue3 TS 项目,根目录下会有 tsconfig.json 和 tsconfig.node.json 文件,并且存在引用关系…...

revisiting拉普拉斯模板
二维向量的二阶微分是Hessian矩阵,拉普拉斯算子是将两个独立的二阶微分求和,对二阶微分的近似。 我不认同冈萨雷斯的8邻域拉普拉斯模板。 MATLAB图像处理工具箱中fspecial函数’laplacian’参数给的拉普拉斯模板: 对于数字滤波器ÿ…...

深入分析计算机网络性能指标
速率带宽吞吐量时延时延带宽积往返时间RTT利用率丢包率图书推荐内容简介作者简介 速率 连接在计算机网络上的主机在数字信道上传送比特的速率,也称为比特率或数据率。 基本单位:bit/s(b/s、bps) 常用单位:kb/s&#x…...

pyflink 安装和测试
FPY Warning! 安装 apache-Flink # pip install apache-Flink -i https://pypi.tuna.tsinghua.edu.cn/simple/ Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple/ Collecting apache-FlinkDownloading https://pypi.tuna.tsinghua.edu.cn/packages/7f/a3/ad502…...
《网络故障处理案例:公司网络突然中断》
网络故障处理案例:公司网络突然中断 一、故障背景 某工作日上午,一家拥有 500 名员工的公司突然出现整个网络中断的情况。员工们无法访问互联网、内部服务器和共享文件,严重影响了工作效率。 二、故障现象 1. 所有员工的电脑…...
JavaSE:9、数组
1、一维数组 初始化 import com.test.*;public class Main {public static void main(String [] argv){int a[]{1,2};int b[]new int[]{1,0,2};// int b[]new int[3]{1,2,3}; ERROR 要么指定长度要么大括号里初始化数据算长度int[] c{1,2};int d[]new int[10];} }基本类型…...

【裸机装机系列】2.kali(ubuntu)-裸机安装kali并进行磁盘分区-2024.9最新
【前言】 2024年为什么弃用ubuntu,请参考我写的另一篇博文:为什么不用ubuntu,而选择基于debian的kali操作系统-2024.9最新 【镜像下载】 1、镜像下载地址 https://www.kali.org/get-kali/选择installer-image,进入界面下载相应的ISO文件 我…...

解决:Vue 中 debugger 不生效
目录 1,问题2,解决2.1,修改 webpack 配置2.2,修改浏览器设置 1,问题 在 Vue 项目中,可以使用 debugger 在浏览器中开启调试。但有时却不生效。 2,解决 2.1,修改 webpack 配置 通…...

Mac笔记本上查看/user/目录下的文件的几种方法
在Mac笔记本上查看/user/下的文件,可以通过多种方法实现。以下是一些常见的方法: 一、使用Finder 打开Finder:点击Dock栏中的Finder图标,或者使用快捷键Command F。 导航到用户目录: 在Finder的菜单栏中࿰…...
工程师 - ACPI和ACPICA的区别
ACPI(高级配置和电源接口)和 ACPICA(ACPI 组件架构)密切相关,但在系统电源管理和配置方面却有不同的作用。以下是它们的区别: ACPI(高级配置和电源接口) - 定义: ACPI 是…...
一文快速上手-create-vue脚手架
文章目录 初识 create-vuecreate-vue新建项目Vue.js 3 项目目录结构项目的运行和打包vite.config.js文件解析其他:webpack和Vite的区别 初识 create-vue create-vue类似于Vue CLI脚手架,可以快速创建vuejs 3项目,create-vue基于Vite。Vite支…...

利用最小二乘法找圆心和半径
#include <iostream> #include <vector> #include <cmath> #include <Eigen/Dense> // 需安装Eigen库用于矩阵运算 // 定义点结构 struct Point { double x, y; Point(double x_, double y_) : x(x_), y(y_) {} }; // 最小二乘法求圆心和半径 …...

深入剖析AI大模型:大模型时代的 Prompt 工程全解析
今天聊的内容,我认为是AI开发里面非常重要的内容。它在AI开发里无处不在,当你对 AI 助手说 "用李白的风格写一首关于人工智能的诗",或者让翻译模型 "将这段合同翻译成商务日语" 时,输入的这句话就是 Prompt。…...
论文解读:交大港大上海AI Lab开源论文 | 宇树机器人多姿态起立控制强化学习框架(二)
HoST框架核心实现方法详解 - 论文深度解读(第二部分) 《Learning Humanoid Standing-up Control across Diverse Postures》 系列文章: 论文深度解读 + 算法与代码分析(二) 作者机构: 上海AI Lab, 上海交通大学, 香港大学, 浙江大学, 香港中文大学 论文主题: 人形机器人…...

label-studio的使用教程(导入本地路径)
文章目录 1. 准备环境2. 脚本启动2.1 Windows2.2 Linux 3. 安装label-studio机器学习后端3.1 pip安装(推荐)3.2 GitHub仓库安装 4. 后端配置4.1 yolo环境4.2 引入后端模型4.3 修改脚本4.4 启动后端 5. 标注工程5.1 创建工程5.2 配置图片路径5.3 配置工程类型标签5.4 配置模型5.…...

使用分级同态加密防御梯度泄漏
抽象 联邦学习 (FL) 支持跨分布式客户端进行协作模型训练,而无需共享原始数据,这使其成为在互联和自动驾驶汽车 (CAV) 等领域保护隐私的机器学习的一种很有前途的方法。然而,最近的研究表明&…...

Cilium动手实验室: 精通之旅---20.Isovalent Enterprise for Cilium: Zero Trust Visibility
Cilium动手实验室: 精通之旅---20.Isovalent Enterprise for Cilium: Zero Trust Visibility 1. 实验室环境1.1 实验室环境1.2 小测试 2. The Endor System2.1 部署应用2.2 检查现有策略 3. Cilium 策略实体3.1 创建 allow-all 网络策略3.2 在 Hubble CLI 中验证网络策略源3.3 …...
鸿蒙中用HarmonyOS SDK应用服务 HarmonyOS5开发一个医院挂号小程序
一、开发准备 环境搭建: 安装DevEco Studio 3.0或更高版本配置HarmonyOS SDK申请开发者账号 项目创建: File > New > Create Project > Application (选择"Empty Ability") 二、核心功能实现 1. 医院科室展示 /…...

Python实现prophet 理论及参数优化
文章目录 Prophet理论及模型参数介绍Python代码完整实现prophet 添加外部数据进行模型优化 之前初步学习prophet的时候,写过一篇简单实现,后期随着对该模型的深入研究,本次记录涉及到prophet 的公式以及参数调优,从公式可以更直观…...
postgresql|数据库|只读用户的创建和删除(备忘)
CREATE USER read_only WITH PASSWORD 密码 -- 连接到xxx数据库 \c xxx -- 授予对xxx数据库的只读权限 GRANT CONNECT ON DATABASE xxx TO read_only; GRANT USAGE ON SCHEMA public TO read_only; GRANT SELECT ON ALL TABLES IN SCHEMA public TO read_only; GRANT EXECUTE O…...
C++ 基础特性深度解析
目录 引言 一、命名空间(namespace) C 中的命名空间 与 C 语言的对比 二、缺省参数 C 中的缺省参数 与 C 语言的对比 三、引用(reference) C 中的引用 与 C 语言的对比 四、inline(内联函数…...