织梦cms零基础做网站,海安网站设计公司,app 官方网站 案例,上海p2p网站建设java实现系统文件管理
环境#xff1a;jdk17springbootVueElementUI 背景#xff1a;公司所做的项目需要别的系统向我们服务器上传文件#xff0c;当我们需要查看这些文件什么时候上传的、文件数据是怎样的#xff0c;只能去机房#xff0c;排查问题效率较低#xff0c;…java实现系统文件管理
环境jdk17springbootVueElementUI 背景公司所做的项目需要别的系统向我们服务器上传文件当我们需要查看这些文件什么时候上传的、文件数据是怎样的只能去机房排查问题效率较低做此页面可快速查看上传的文件信息且可下载到本地查看。为了生产安全不支持修改及上传文件如果有的朋友想做可自行查找资料。 需求实现系统文件的查询及下载。展示系统的文件信息如文件名、文件大小、最后更新时间及权限等。 注意本篇文章是以window系统做的样例不过一般服务器都是在linux系统只需将前端的初始地址换成linux地址一般格式为/home/app。 效果图 直接上代码前端代码
templatedivdivel-col :xl4 :lg5el-input v-modelcurPath label-width80px sizesmall typetext当前位置/el-input/el-colel-button typeprimary sizesmall clickgetParentData() iconel-icon-back返回上级/el-buttonel-button typeprimary sizesmall clickrefresh() iconel-icon-refresh刷新/el-button/divel-table :datafileList v-loadingtableLoadingel-table-column label名称 propfileName!-- eslint-disable-next-line--template slot-scopescopeel-button typetext v-if!scope.row.fileType clickgetSonData(scope.row){{scope.row.fileName}}/el-buttonspan v-ifscope.row.fileType{{ scope.row.fileName }}/span/template/el-table-columnel-table-column label类型 aligncenter!-- eslint-disable-next-line--template slot-scopescopediv{{ typeName(scope.row.fileType) }}/div/template/el-table-columnel-table-column label大小 propfileSize/el-table-columnel-table-column label更新时间 proplastModifiedDate/el-table-columnel-table-column aligncenter label权限 width120!-- eslint-disable-next-line--template slot-scopescopediv{{ getAuthority(scope.row) }}/div/template/el-table-columnel-table-column label操作!-- eslint-disable-next-line--template slot-scopescopeel-button sizemini v-ifscope.row.fileType typetext clickdownload(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 noneelink.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}}
}
/scriptstyle
.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();}ListSysFileDo 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 ResponseEntitybyte[] download(RequestParam String path) throws IOException {File file new File(path);HttpHeaders httpHeaders new HttpHeaders();httpHeaders.setContentDispositionFormData(attachment, );return new ResponseEntitybyte[](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 ResultT {private int code;private String msg;private T data;
}
好了主要的代码就这些。还有两个依赖 !-- 处理文件上传的 Java 库 --dependencygroupIdcommons-fileupload/groupIdartifactIdcommons-fileupload/artifactIdversion1.5/version/dependency!-- Apache 的开源工具库,包含了许多实用的文件操作、流操作相关的功能和工具类比如文件读写、文件和目录的操作、流的处理和转换 --dependencygroupIdcommons-io/groupIdartifactIdcommons-io/artifactIdversion2.5/version/dependency