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

时间: 2023-06-12 18:05:15 浏览: 50
1. 分页展示Hdfs文件列表 前端代码: ```html <!-- 分页插件 --> <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script> <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <table class="table table-hover"> <thead> <tr> <th>#</th> <th>文件名</th> <th>大小</th> <th>修改时间</th> <th>操作</th> </tr> </thead> <tbody> <c:forEach items="${hdfsFiles}" var="hdfsFile" varStatus="status"> <tr> <td>${status.index+1}</td> <td><a href="#">${hdfsFile.name}</a></td> <td>${hdfsFile.size}</td> <td>${hdfsFile.modificationTime}</td> <td> <button class="btn btn-danger" onclick="deleteFile('${hdfsFile.path}')">删除</button> <button class="btn btn-success" onclick="downloadFile('${hdfsFile.path}')">下载</button> </td> </tr> </c:forEach> </tbody> </table> <div class="text-center"> <ul class="pagination"> <c:choose> <c:when test="${pageInfo.pageNum == 1}"> <li class="disabled"><a href="#">首页</a></li> <li class="disabled"><a href="#">上一页</a></li> </c:when> <c:otherwise> <li><a href="?pageNum=1">首页</a></li> <li><a href="?pageNum=${pageInfo.prePage}">上一页</a></li> </c:otherwise> </c:choose> <c:forEach begin="${pageInfo.navigateFirstPage}" end="${pageInfo.navigateLastPage}" var="i"> <c:choose> <c:when test="${i == pageInfo.pageNum}"> <li class="active"><a href="#">${i}</a></li> </c:when> <c:otherwise> <li><a href="?pageNum=${i}">${i}</a></li> </c:otherwise> </c:choose> </c:forEach> <c:choose> <c:when test="${pageInfo.pageNum == pageInfo.pages}"> <li class="disabled"><a href="#">下一页</a></li> <li class="disabled"><a href="#">尾页</a></li> </c:when> <c:otherwise> <li><a href="?pageNum=${pageInfo.nextPage}">下一页</a></li> <li><a href="?pageNum=${pageInfo.pages}">尾页</a></li> </c:otherwise> </c:choose> </ul> </div> ``` 后端代码: ```java @RequestMapping("/listFiles") public ModelAndView listFiles(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize) throws Exception { ModelAndView mv = new ModelAndView("fileList"); FileSystem fs = FileSystem.get(new URI("hdfs://localhost:9000"), new Configuration(), "root"); RemoteIterator<LocatedFileStatus> fileStatusListIterator = fs.listFiles(new Path("/"), true); List<HdfsFile> hdfsFiles = new ArrayList<>(); while (fileStatusListIterator.hasNext()) { LocatedFileStatus fileStatus = fileStatusListIterator.next(); HdfsFile hdfsFile = new HdfsFile(); hdfsFile.setName(fileStatus.getPath().getName()); hdfsFile.setPath(fileStatus.getPath().toString()); hdfsFile.setSize(fileStatus.getLen()); hdfsFile.setModificationTime(fileStatus.getModificationTime()); hdfsFiles.add(hdfsFile); } PageHelper.startPage(pageNum, pageSize); PageInfo<HdfsFile> pageInfo = new PageInfo<>(hdfsFiles); mv.addObject("hdfsFiles", pageInfo.getList()); mv.addObject("pageInfo", pageInfo); return mv; } ``` 2. 上传文件 前端代码: ```html <form action="${pageContext.request.contextPath}/uploadFile" method="post" enctype="multipart/form-data"> <div class="form-group"> <label>选择文件</label> <input type="file" name="file"> </div> <button type="submit" class="btn btn-primary">上传</button> </form> ``` 后端代码: ```java @RequestMapping("/uploadFile") public String uploadFile(@RequestParam("file") MultipartFile file) throws IOException, URISyntaxException { FileSystem fs = FileSystem.get(new URI("hdfs://localhost:9000"), new Configuration(), "root"); Path dst = new Path("/" + file.getOriginalFilename()); FSDataOutputStream outputStream = fs.create(dst); IOUtils.copyBytes(file.getInputStream(), outputStream, 4096, true); return "redirect:/listFiles"; } ``` 3. 删除文件 前端代码: ```html <button class="btn btn-danger" onclick="deleteFile('${hdfsFile.path}')">删除</button> ``` 后端代码: ```java @RequestMapping("/deleteFile") public String deleteFile(String path) throws IOException, URISyntaxException { FileSystem fs = FileSystem.get(new URI("hdfs://localhost:9000"), new Configuration(), "root"); Path dst = new Path(path); fs.delete(dst, true); return "redirect:/listFiles"; } ``` 4. 下载文件 前端代码: ```html <button class="btn btn-success" onclick="downloadFile('${hdfsFile.path}')">下载</button> ``` 后端代码: ```java @RequestMapping("/downloadFile") public ResponseEntity<byte[]> downloadFile(String path) throws IOException, URISyntaxException { FileSystem fs = FileSystem.get(new URI("hdfs://localhost:9000"), new Configuration(), "root"); Path dst = new Path(path); FSDataInputStream inputStream = fs.open(dst); byte[] bytes = IOUtils.toByteArray(inputStream); HttpHeaders headers = new HttpHeaders(); headers.setContentDispositionFormData("attachment", dst.getName()); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); return new ResponseEntity<>(bytes, headers, HttpStatus.CREATED); } ```

相关推荐

最新推荐

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的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。