SSM框架分页展示Hdfs文件列表,返回上一级,进入下一级,上传文件,删除文件,下载文件前后端代码

时间: 2023-09-26 17:06:42 浏览: 64
### 回答1: 由于HDFS是一个分布式文件系统,它需要和Hadoop集群进行交互才能实现文件的上传、删除、下载等操作。本文提供的代码示例是基于Hadoop API进行开发的,使用SSM框架搭建Java Web应用实现分页展示HDFS文件列表、返回上一级、进入下一级、上传文件、删除文件、下载文件等操作。 前端代码: ``` <!-- 文件列表展示 --> <div class="panel-body"> <div class="table-responsive"> <table class="table table-striped"> <thead> <tr> <th>文件名</th> <th>大小</th> <th>修改时间</th> <<th>操作</th> </tr> </thead> <tbody> <c:forEach var="file" items="${fileList}"> <tr> <td>${file.name}</td> <td>${file.size}</td> <td>${file.modificationTime}</td> <td> <a href="#" onclick="enterDirectory('${file.path}')">进入</a> <a href="#" onclick="deleteFile('${file.path}')">删除</a> <a href="#" onclick="downloadFile('${file.path}')">下载</a> </td> </tr> </c:forEach> </tbody> </table> </div> </div> <!-- 返回上一级 --> <div class="panel-footer"> <button type="button" class="btn btn-default" onclick="backToParent()"> <i class="fa fa-level-up"></i> 返回上一级 </button> </div> <!-- 文件上传 --> <form id="uploadForm" enctype="multipart/form-data"> <div class="form-group"> <label for="file">上传文件:</label> <input type="file" name="file" id="file" class="form-control"> </div> <button type="button" class="btn btn-primary" onclick="uploadFile()"> <i class="fa fa-upload"></i> 上传文件 </button> </form> ``` 后端代码: ``` @Controller @RequestMapping("/hdfs") public class HdfsController { @Autowired private Configuration configuration; @GetMapping("/list") @ResponseBody public List<HdfsFile> listFiles(@RequestParam(required = false) String path, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize) throws IOException { FileSystem fs = FileSystem.get(configuration); List<HdfsFile> fileList = new ArrayList<>(); Path dirPath = new Path(path); if (fs.exists(dirPath)) { FileStatus[] fileStatuses = fs.listStatus(dirPath); for (FileStatus fileStatus : fileStatuses) { HdfsFile hdfsFile = new HdfsFile(); hdfsFile.setName(fileStatus.getPath().getName()); hdfsFile.setSize(FileUtils.byteCountToDisplaySize(fileStatus.getLen())); hdfsFile.setModificationTime(new Date(fileStatus.getModificationTime())); hdfsFile.setPath(fileStatus.getPath().toString()); hdfsFile.setDirectory(fileStatus.isDirectory()); fileList.add(hdfsFile); } } int startIndex = (pageNum - 1) * pageSize; int endIndex = Math.min(startIndex + pageSize, fileList.size()); return fileList.subList(startIndex, endIndex); } @PostMapping("/upload") @ResponseBody public ResponseResult uploadFile(@RequestParam("file") MultipartFile file, @RequestParam("path") String path) throws IOException { FileSystem fs = FileSystem.get(configuration); Path filePath = new Path(path + "/" + file.getOriginalFilename()); FSDataOutputStream outputStream = fs.create(filePath); IOUtils.copyBytes(file.getInputStream(), outputStream, configuration); IOUtils.closeStream(outputStream); return ResponseResult.success(); } @GetMapping("/download") public void downloadFile(@RequestParam("path") String path, HttpServletResponse response) throws IOException { FileSystem fs = FileSystem.get(configuration); Path filePath = new Path(path); if (fs.exists(filePath)) { response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment;filename=" + filePath.getName()); FSDataInputStream inputStream = fs.open(filePath); IOUtils.copyBytes(inputStream, response.getOutputStream(), configuration); IOUtils.closeStream(inputStream); } } @DeleteMapping("/delete") @ResponseBody public ResponseResult deleteFile(@RequestParam("path") String path) throws IOException { FileSystem fs = FileSystem.get(configuration); Path filePath = new Path(path); boolean success = fs.delete(filePath, true); return success ? ResponseResult.success() : ResponseResult.fail(); } @GetMapping("/back") public String backToParent(@RequestParam("path") String path) { Path parentPath = new Path(path).getParent(); return "redirect:/hdfs/list?path=" + parentPath.toString(); } @GetMapping("/enter") public String enterDirectory(@RequestParam("path") String path) { return "redirect:/hdfs/list?path=" + path; } } ``` 其中,`HdfsFile`是一个Java Bean类,用于封装HDFS文件的属性,包括文件名、大小、修改时间、路径、是否为目录等信息。`ResponseResult`是一个通用的响应结果类,用于封装操作的结果以及相应的提示信息。 在上述代码中,`listFiles()`方法用于分页展示HDFS文件列表,`uploadFile()`方法用于上传文件,`downloadFile()`方法用于下载文件,`deleteFile()`方法用于删除文件,`backToParent()`方法用于返回上一级目录,`enterDirectory()`方法用于进入下一级目录。 需要注意的是,由于Hadoop API是基于Java原生的IO操作进行开发的,因此需要在代码中显式地关闭流以避免资源泄漏。另外,由于HDFS是一个分布式文件系统,因此需要使用`FileSystem`类进行文件的操作,而不是Java原生的`File`类。 ### 回答2: 在SSM框架中,如果要实现HDFS文件列表的分页展示以及对文件的上一级和下一级的操作,文件的上传、删除和下载操作,我们需要编写前后端代码。 前端代码如下: 1. 展示HDFS文件列表的页面,可以使用HTML和CSS来设计文件列表的展示形式,使用JavaScript来实现请求和响应的操作。 2. 在页面中设置上一级和下一级的按钮,点击按钮时触发JavaScript函数,发送请求给后端,获取上一级或下一级的文件列表并展示在页面上。 3. 设置上传文件的按钮,用户点击按钮时可以选择文件,然后通过JavaScript发送请求给后端,将文件上传到HDFS,并在页面上展示上传成功的提示信息。 4. 设置删除文件的按钮,用户点击按钮时可以选择要删除的文件,然后通过JavaScript发送请求给后端,将文件从HDFS中删除,并在页面上展示删除成功的提示信息。 5. 设置下载文件的按钮,用户点击按钮时可以选择要下载的文件,然后通过JavaScript发送请求给后端,将文件从HDFS中下载到本地。 后端代码如下: 1. 在后端定义Controller类,处理前端发送的请求并返回相应的数据。 2. 编写Controller方法,用于处理获取文件列表、获取上一级和下一级文件列表、上传文件、删除文件、下载文件的请求。 3. 在方法中使用Hadoop的HDFS API进行文件操作,例如获取文件列表、上传文件、删除文件、下载文件等。 4. 设置分页功能,根据前端传递过来的页码和每页数量,查询对应页的文件列表并返回给前端展示。 以上就是实现SSM框架下分页展示HDFS文件列表,并实现返回上一级、进入下一级、上传文件、删除文件和下载文件的前后端代码。请注意,这只是一个简要的概述,具体的实现细节需要结合具体需求和项目来编写代码。 ### 回答3: SSM框架(Spring + Spring MVC + MyBatis)可以实现对HDFS文件列表的分页展示、返回上一级、进入下一级、上传文件、删除文件以及下载文件的功能。下面是前后端代码的示例: 1. 后端代码(Java): ``` // 文件管理Controller @Controller @RequestMapping("/file") public class FileController { @Autowired private HdfsService hdfsService; // 分页展示HDFS文件列表 @RequestMapping("/list") @ResponseBody public List<HdfsFile> getFileList(int pageNo, int pageSize) { return hdfsService.getFileList(pageNo, pageSize); } // 返回上一级目录 @RequestMapping("/parent") @ResponseBody public List<HdfsFile> getParentDir(String currentPath) { return hdfsService.getParentDir(currentPath); } // 进入下一级目录 @RequestMapping("/child") @ResponseBody public List<HdfsFile> getChildDir(String currentPath) { return hdfsService.getChildDir(currentPath); } // 上传文件 @RequestMapping("/upload") @ResponseBody public boolean uploadFile(MultipartFile file) { return hdfsService.uploadFile(file); } // 删除文件 @RequestMapping("/delete") @ResponseBody public boolean deleteFile(String filePath) { return hdfsService.deleteFile(filePath); } // 下载文件 @RequestMapping("/download") public void downloadFile(String filePath, HttpServletResponse response) { hdfsService.downloadFile(filePath, response); } } // HDFS服务接口 public interface HdfsService { List<HdfsFile> getFileList(int pageNo, int pageSize); List<HdfsFile> getParentDir(String currentPath); List<HdfsFile> getChildDir(String currentPath); boolean uploadFile(MultipartFile file); boolean deleteFile(String filePath); void downloadFile(String filePath, HttpServletResponse response); } // HDFS服务实现 @Service public class HdfsServiceImpl implements HdfsService { @Autowired private FileSystem hdfs; @Override public List<HdfsFile> getFileList(int pageNo, int pageSize) { // 通过HDFS API获取文件列表,并进行分页处理 // 返回文件列表 } @Override public List<HdfsFile> getParentDir(String currentPath) { // 获取当前路径的父级目录,并返回文件列表 } @Override public List<HdfsFile> getChildDir(String currentPath) { // 获取当前路径的子目录,并返回文件列表 } @Override public boolean uploadFile(MultipartFile file) { // 将上传的文件保存到HDFS中,返回上传结果 } @Override public boolean deleteFile(String filePath) { // 删除指定路径的文件或目录,返回删除结果 } @Override public void downloadFile(String filePath, HttpServletResponse response) { // 从HDFS中读取文件,并将文件流写入response中,实现文件下载 } } ``` 2. 前端代码(HTML + JavaScript): ``` <!-- 文件列表页面 --> <html> <head> <title>HDFS文件列表</title> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> </head> <body> <h1>HDFS文件列表</h1> <table id="fileTable"> <thead> <tr> <th>文件名</th> <th>大小</th> <th>操作</th> </tr> </thead> <tbody> <!-- 文件列表数据会动态填充到这里 --> </tbody> </table> <div> <button id="prevBtn">返回上一级</button> <button id="nextBtn">进入下一级</button> <input type="file" id="uploadBtn"> </div> <script> $(document).ready(function() { var pageNo = 1; var pageSize = 10; // 页面加载时获取文件列表 updateFileList(pageNo, pageSize); // 上一页按钮点击事件 $("#prevBtn").click(function() { pageNo--; if (pageNo < 1) { pageNo = 1; } updateFileList(pageNo, pageSize); }); // 下一页按钮点击事件 $("#nextBtn").click(function() { pageNo++; updateFileList(pageNo, pageSize); }); // 上传文件按钮点击事件 $("#uploadBtn").change(function() { var file = $(this).prop("files")[0]; uploadFile(file); }); }); // 更新文件列表 function updateFileList(pageNo, pageSize) { $.ajax({ url: "/file/list", type: "GET", data: { pageNo: pageNo, pageSize: pageSize }, success: function(data) { // 渲染文件列表数据 renderFileList(data); } }); } // 渲染文件列表数据 function renderFileList(data) { $("#fileTable tbody").empty(); for (var i = 0; i < data.length; i++) { var file = data[i]; var row = "<tr>" + "<td>" + file.name + "</td>" + "<td>" + file.size + "</td>" + "<td>" + "<button onclick=\"deleteFile('" + file.path + "')\">删除</button>" + "<button onclick=\"downloadFile('" + file.path + "')\">下载</button>" + "</td>" + "</tr>"; $("#fileTable tbody").append(row); } } // 删除文件 function deleteFile(filePath) { $.ajax({ url: "/file/delete", type: "POST", data: { filePath: filePath }, success: function(data) { if (data) { alert("文件删除成功"); updateFileList(pageNo, pageSize); } else { alert("文件删除失败"); } } }); } // 下载文件 function downloadFile(filePath) { window.location.href = "/file/download?filePath=" + encodeURIComponent(filePath); } // 上传文件 function uploadFile(file) { var formData = new FormData(); formData.append("file", file); $.ajax({ url: "/file/upload", type: "POST", data: formData, processData: false, contentType: false, success: function(data) { if (data) { alert("文件上传成功"); updateFileList(pageNo, pageSize); } else { alert("文件上传失败"); } } }); } </script> </body> </html> ``` 以上代码演示了如何使用SSM框架实现对HDFS文件列表的分页展示、返回上一级、进入下一级、上传文件、删除文件以及下载文件的功能。具体的实现要根据实际项目需求和环境来调整。

相关推荐

最新推荐

recommend-type

yolov5-face-landmarks-opencv

yolov5检测人脸和关键点,只依赖opencv库就可以运行,程序包含C++和Python两个版本的。 本套程序根据https://github.com/deepcam-cn/yolov5-face 里提供的训练模型.pt文件。转换成onnx文件, 然后使用opencv读取onnx文件做前向推理,onnx文件从百度云盘下载,下载 链接:https://pan.baidu.com/s/14qvEOB90CcVJwVC5jNcu3A 提取码:duwc 下载完成后,onnx文件存放目录里,C++版本的主程序是main_yolo.cpp,Python版本的主程序是main.py 。此外,还有一个main_export_onnx.py文件,它是读取pytorch训练模型.pt文件生成onnx文件的。 如果你想重新生成onnx文件,不能直接在该目录下运行的,你需要把文件拷贝到https://github.com/deepcam-cn/yolov5-face 的主目录里运行,就可以生成onnx文件。
recommend-type

setuptools-0.6c8-py2.5.egg

文件操作、数据分析和网络编程等。Python社区提供了大量的第三方库,如NumPy、Pandas和Requests,极大地丰富了Python的应用领域,从数据科学到Web开发。Python库的丰富性是Python成为最受欢迎的编程语言之一的关键原因之一。这些库不仅为初学者提供了快速入门的途径,而且为经验丰富的开发者提供了强大的工具,以高效率、高质量地完成复杂任务。例如,Matplotlib和Seaborn库在数据可视化领域内非常受欢迎,它们提供了广泛的工具和技术,可以创建高度定制化的图表和图形,帮助数据科学家和分析师在数据探索和结果展示中更有效地传达信息。
recommend-type

5-3.py

5-3
recommend-type

Java八股文.pdf

"Java八股文"是一个在程序员社群中流行的术语,特别是在准备技术面试时。它指的是一系列在Java编程面试中经常被问到的基础知识点、理论概念和技术细节。这个术语的命名来源于中国古代科举考试中的“八股文”,一种具有固定格式和套路的文章形式。 在Java编程的上下文中,"Java八股文"通常包括以下几个方面:"Java八股文"是一个在程序员社群中流行的术语,特别是在准备技术面试时。它指的是一系列在Java编程面试中经常被问到的基础知识点、理论概念和技术细节。这个术语的命名来源于中国古代科举考试中的“八股文”,一种具有固定格式和套路的文章形式。 在Java编程的上下文中,"Java八股文"通常包括以下几个方面:"Java八股文"是一个在程序员社群中流行的术语,特别是在准备技术面试时。它指的是一系列在Java编程面试中经常被问到的基础知识点、理论概念和技术细节。这个术语的命名来源于中国古代科举考试中的“八股文”,一种具有固定格式和套路的文章形式。 在Java编程的上下文中,"Java八股文"通常包括以下几个方面:"Java八股文"是一个在程序员社群中流行的术语,特别是在准备技术面试时。它
recommend-type

麦肯锡咨询顾问必备宝典.ppt

麦肯锡咨询顾问必备宝典.ppt
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。