模拟找D盘的文件

server/2024/10/19 13:28:59/

需求:有这样一个需求,通过找D盘中图片,然后进行预览。当点击的是文件夹则能进入到下一级,如果点击的是图片则进行显示预览。java部分看不懂,就粘贴了一部分。

前端代码;
css部分:

<style>#files button {margin-right: 10px;background-color: #0b4180;color: white;}#files button:hover {background-color: #00B83F;color: #0C0C0C;}#file-list li:hover {cursor: pointer;color: red;}#showImg {width: 400px;height: 400px;}</style><div id="files"><h1>文件浏览</h1><button id="backfirst">返回上一级</button><button id="closeImg">关闭图片</button><ul id="file-list"></ul><img id="showImg" style="display: none;"></div>

JS部分:

javascript>javascript">const baseURL = 'http://localhost:15112/browse/'; // 修改为你的后端服务器地址,(注:这里服务器路径我删了一部分)let currentPathStack = ['files']; // 使用栈来记录当前路径$(document).ready(function () {fetchFiles();});function fetchFiles() {const path = currentPathStack[currentPathStack.length - 1]; // 获取当前路径console.log('当前路径:', path)$.ajax({type: 'GET',url: `${baseURL}${path}`,success: function (data) {const fileList = $('#file-list');fileList.empty(); // 清空之前的列表console.log('原始数据', data)data.forEach(item => {const li = $('<li></li></br>');li.text(item.name);li.on('click', function (e) {//e.stopPropagation(); // 阻止事件冒泡,防止点击 li 时触发 ul 的点击事件if (item.directory == true) {currentPathStack.push(`${path}-${item.name}`); // 将当前文件夹路径推入栈中$('#backfirst').prop('disabled', false); // 启用返回按钮$('#showImg').hide();$('#closeImg').hide();fetchFiles(); // 递归调用以加载新目录的内容} else if (item.directory == false) {$('#showImg').attr('src', `data:image/jpeg;base64,${item.data}`);$('#showImg').show(); // 显示图片// 显示关闭图片的按钮$('#closeImg').show();}});fileList.append(li);});// 如果栈中不止一个元素(即不在根目录),则启用返回按钮$('#backfirst').prop('disabled', currentPathStack.length <= 1);},error: function (error) {console.error('请求失败:', error);}});}// 返回上一级目录$('#backfirst').click(function () {if (!$(this).is(':disabled')) { // 确保按钮没有被禁用currentPathStack.pop(); // 弹出当前路径// 在返回上一级目录时,隐藏图片并隐藏关闭按钮$('#showImg').hide();$('#closeImg').hide();fetchFiles(); // 加载上一级目录的内容}});// 关闭图片按钮的点击事件$('#closeImg').click(function () {// 隐藏图片并隐藏关闭按钮$('#showImg').hide();$('#closeImg').hide();});

后端部分:
controller.java文件夹

java"> @GetMapping("/browse/{path:.+}")@ResponseBodypublic List<FileItem> browse(@PathVariable String path) throws IOException {File dir = new File("D:\\"+path.replaceAll("-","/"));//这里用-转为/,我直接用斜杠不行,所以后端直接转换了一下,就能用了List<FileItem> items = new ArrayList<>();for (File file : dir.listFiles()) {if (file.isDirectory()) {items.add(new FileItem(file.getName(), true, file.getAbsolutePath()));} else {items.add(new FileItem(file.getName(), false, encodeImageToBase64(file)));}}return items;}private String encodeImageToBase64(File imageFile) throws IOException {// 将图片文件转换为Base64编码的字符串try (FileInputStream fis = new FileInputStream(imageFile)) {byte[] buffer = new byte[1024];ByteArrayOutputStream baos = new ByteArrayOutputStream();int bytesRead;while ((bytesRead = fis.read(buffer)) != -1) {baos.write(buffer, 0, bytesRead);}byte[] imageBytes = baos.toByteArray();return Base64.getEncoder().encodeToString(imageBytes);}}

fileItem.java文件夹

java">public class FileItem {String name;boolean isDirectory;String data; // 对于图片,存储Base64编码的字符串;对于文件夹,存储路径public FileItem(String name, boolean isDirectory, String data) {this.name = name;//文件及文件夹名字this.isDirectory = isDirectory;//判断是文件夹还是文件(图片)this.data = data;//路径}public String getName() {return name;}public void setName(String name) {this.name = name;}public boolean isDirectory() {return isDirectory;}public void setDirectory(boolean directory) {isDirectory = directory;}public String getData() {return data;}public void setData(String data) {this.data = data;}
}

效果图:
在这里插入图片描述在这里插入图片描述


http://www.ppmy.cn/server/23483.html

相关文章

vue:使用:element 中弹框中获取table高度无效

场景&#xff1a;dialog 弹框中想要获取里面table的高度&#xff0c;但是直接用 tableRef.value?.$el.offsetHeight 获取无效。 原因&#xff1a;dialog中有个弹框打开的加载动画。需要用监听 opeined 【Dialog 打开动画结束时的回调】等动画加载后然后进行高度获取 一、dem…

快速构建Spring boot项目

1、Idea里新建项目 2、创建HelloController 3、运行 4、开发环境热部署 pom.xml 查看目前已有的依赖 配置properties 设置 ctrlshiftalt/ 新版本的compiler.automake.allow.when.app.running已经不在registry里面了&#xff0c;在settings里面的Advanced settings里面Allow au…

indexedDB---浏览器内建的数据库(学习记录)

IndexedDB IndexedDB 是一个浏览器内建的数据库&#xff0c;它比 localStorage 强大得多。 通过支持多种类型的键&#xff0c;来存储几乎可以是任何类型的值。支撑事务的可靠性。支持键值范围查询、索引。和 localStorage 相比&#xff0c;它可以存储更大的数据量。 要注意的…

AI大模型探索之路-训练篇4:大语言模型训练数据集概览

系列文章目录&#x1f6a9; AI大模型探索之路-训练篇1&#xff1a;大语言模型微调基础认知 AI大模型探索之路-训练篇2&#xff1a;大语言模型预训练基础认知 AI大模型探索之路-训练篇3&#xff1a;大语言模型全景解读 文章目录 系列文章目录&#x1f6a9;前言一、常用的预训练…

宁盾LDAP统一用户认证与单点登录:构建高效安全的企业身份认证

在信息化时代&#xff0c;企业面临着众多的应用系统和数据资源&#xff0c;如何有效地管理和保护这些资源&#xff0c;确保信息安全和高效利用&#xff0c;成为了企业信息化建设的核心问题。LDAP统一用户认证和单点登录&#xff08;SSO&#xff09;作为一种高效、安全的身份验证…

Java DAG并行开源框架

Java DAG并行开源框架指的是一种用于编写高性能、高可靠性、高可扩展性数据流处理程序的工具集&#xff0c;它提供了一系列算法和工具&#xff0c;帮助我们快速构建复杂的数据处理管道。以下是一些常见的Java DAG并行开源框架&#xff1a; Apache Flink&#xff1a;Flink是一个…

vue2使用change事件监听不了回车事件的问题

在 vue2 项目中使用 el-input 的 change 监听&#xff0c;数据不发生变化时&#xff0c;回车事件和失去焦点不生效 输入框会一直显示 只有数据发生变化时才生效 <el-input v-model"editedText" change"endEditing" ref"input"></el-inp…

SpringTask 基于springboot

介绍 Spring Task 是Spring框架提供的任务调度工具&#xff0c;可以按照约定的时间自动执行某个代码逻辑 应用场景&#xff1a;信用卡每月还款提醒,银行贷款每月还款提醒,火车票售票系统处理未支付订单,入职纪念日为用户发送通知,只要是需要定时处理的场景都可以使用Spring T…