ssm查看hdfs文件,上传文件,下载文件,删除文件前后端代码

时间: 2023-08-31 22:26:08 浏览: 53
### 回答1: 以下是SSM框架中查看、上传、下载、删除HDFS文件的前后端代码。 ### 查看HDFS文件 前端代码: ```html <!-- index.jsp --> <form id="form" enctype="multipart/form-data" method="post" action="showFile"> <label for="path">文件路径:</label> <input type="text" name="path" id="path"> <input type="submit" value="查看"> </form> ``` 后端代码: ```java // FileController.java @Controller public class FileController { private final FileSystem fileSystem = HdfsUtil.getFileSystem(); @RequestMapping("showFile") @ResponseBody public String showFile(@RequestParam("path") String path) { try { FSDataInputStream inputStream = fileSystem.open(new Path(path)); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); StringBuilder builder = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { builder.append(line).append("\n"); } reader.close(); inputStream.close(); return builder.toString(); } catch (IOException e) { e.printStackTrace(); return "读取文件失败!"; } } } ``` ### 上传HDFS文件 前端代码: ```html <!-- index.jsp --> <form id="form" enctype="multipart/form-data" method="post" action="uploadFile"> <label for="file">上传文件:</label> <input type="file" name="file" id="file"> <input type="submit" value="上传"> </form> ``` 后端代码: ```java // FileController.java @Controller public class FileController { private final FileSystem fileSystem = HdfsUtil.getFileSystem(); @RequestMapping("uploadFile") @ResponseBody public String uploadFile(@RequestParam("file") MultipartFile file) { try { String fileName = file.getOriginalFilename(); Path path = new Path("/" + fileName); FSDataOutputStream outputStream = fileSystem.create(path); outputStream.write(file.getBytes()); outputStream.close(); return "文件上传成功!"; } catch (IOException e) { e.printStackTrace(); return "文件上传失败!"; } } } ``` ### 下载HDFS文件 前端代码: ```html <!-- index.jsp --> <form id="form" enctype="multipart/form-data" method="post" action="downloadFile"> <label for="path">文件路径:</label> <input type="text" name="path" id="path"> <input type="submit" value="下载"> </form> ``` 后端代码: ```java // FileController.java @Controller public class FileController { private final FileSystem fileSystem = HdfsUtil.getFileSystem(); @RequestMapping("downloadFile") public void downloadFile(@RequestParam("path") String path, HttpServletResponse response) { try { FSDataInputStream inputStream = fileSystem.open(new Path(path)); response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment; filename=" + new Path(path).getName()); IOUtils.copyBytes(inputStream, response.getOutputStream(), 4096, false); inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } ``` ### 删除HDFS文件 前端代码: ```html <!-- index.jsp --> <form id="form" enctype="multipart/form-data" method="post" action="deleteFile"> <label for="path">文件路径:</label> <input type="text" name="path" id="path"> <input type="submit" value="删除"> </form> ``` 后端代码: ```java // FileController.java @Controller public class FileController { private final FileSystem fileSystem = HdfsUtil.getFileSystem(); @RequestMapping("deleteFile") @ResponseBody public String deleteFile(@RequestParam("path") String path) { try { boolean result = fileSystem.delete(new Path(path), true); return result ? "文件删除成功!" : "文件删除失败!"; } catch (IOException e) { e.printStackTrace(); return "文件删除失败!"; } } } ``` 其中,`HdfsUtil`是一个工具类,用于获取HDFS的FileSystem实例。具体实现可参考以下代码: ```java // HdfsUtil.java public class HdfsUtil { private static final String HDFS_URI = "hdfs://localhost:9000"; private static final Configuration CONFIGURATION = new Configuration(); private static FileSystem fileSystem; static { try { fileSystem = FileSystem.get(URI.create(HDFS_URI), CONFIGURATION); } catch (IOException e) { e.printStackTrace(); } } public static FileSystem getFileSystem() { return fileSystem; } } ``` ### 回答2: 为了实现SSM查看HDFS文件、上传文件、下载文件、删除文件的功能,我们需要在前后端代码中分别实现相应的操作。 首先,后端代码: 1. 查看HDFS文件: ```java @Controller @RequestMapping("/hdfs") public class HdfsController { @Autowired private HdfsService hdfsService; @GetMapping("/view") @ResponseBody public List<String> viewHdfsFiles() { return hdfsService.getHdfsFiles(); } } ``` 2. 上传文件: ```java @Controller @RequestMapping("/hdfs") public class HdfsController { @Autowired private HdfsService hdfsService; @PostMapping("/upload") @ResponseBody public String uploadFile(@RequestParam("file") MultipartFile file) { return hdfsService.uploadFile(file); } } ``` 3. 下载文件: ```java @Controller @RequestMapping("/hdfs") public class HdfsController { @Autowired private HdfsService hdfsService; @GetMapping("/download") public ResponseEntity<Resource> downloadFile(@RequestParam("path") String path) { return hdfsService.downloadFile(path); } } ``` 4. 删除文件: ```java @Controller @RequestMapping("/hdfs") public class HdfsController { @Autowired private HdfsService hdfsService; @GetMapping("/delete") @ResponseBody public String deleteFile(@RequestParam("path") String path) { return hdfsService.deleteFile(path); } } ``` 然后,前端代码: 1. 查看HDFS文件: ```html <script> $(document).ready(function() { $.ajax({ url: "/hdfs/view", type: "GET", success: function(data) { // 处理返回的文件列表data } }); }); </script> ``` 2. 上传文件: ```html <form id="uploadForm" method="POST" enctype="multipart/form-data"> <input type="file" name="file" accept=".txt,.csv"> <button type="submit">上传</button> </form> <script> $("#uploadForm").submit(function(e) { e.preventDefault(); var formData = new FormData(this); $.ajax({ url: "/hdfs/upload", type: "POST", data: formData, contentType: false, processData: false, success: function(data) { // 处理上传结果data } }); }); </script> ``` 3. 下载文件: ```html <input type="text" id="downloadPath"> <button onclick="downloadFile()">下载</button> <script> function downloadFile() { var path = $("#downloadPath").val(); window.open("/hdfs/download?path=" + path); } </script> ``` 4. 删除文件: ```html <input type="text" id="deletePath"> <button onclick="deleteFile()">删除</button> <script> function deleteFile() { var path = $("#deletePath").val(); $.ajax({ url: "/hdfs/delete?path=" + path, type: "GET", success: function(data) { // 处理删除结果data } }); } </script> ``` 以上就是SSM查看HDFS文件、上传文件、下载文件、删除文件的前后端代码实现。请根据实际情况进行适当的修改和调整。 ### 回答3: 以下是一个简单的SSM项目的前后端代码,实现了查看HDFS文件、上传文件、下载文件、删除文件的功能。 后端代码: 1. HDFSUtil.java ```java import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.springframework.stereotype.Component; import java.io.InputStream; import java.io.OutputStream; @Component public class HDFSUtil { private Configuration configuration; private FileSystem fileSystem; public HDFSUtil() throws Exception { configuration = new Configuration(); configuration.set("fs.defaultFS", "hdfs://localhost:9000"); // 设置HDFS地址 fileSystem = FileSystem.get(configuration); } public void close() throws Exception { if (fileSystem != null) { fileSystem.close(); } } public void uploadFile(InputStream inputStream, String destPath) throws Exception { OutputStream outputStream = fileSystem.create(new Path(destPath)); byte[] buffer = new byte[1024]; int length; while ((length = inputStream.read(buffer)) > 0) { outputStream.write(buffer, 0, length); } outputStream.close(); inputStream.close(); } public void downloadFile(String srcPath, OutputStream outputStream) throws Exception { InputStream inputStream = fileSystem.open(new Path(srcPath)); byte[] buffer = new byte[1024]; int length; while ((length = inputStream.read(buffer)) > 0) { outputStream.write(buffer, 0, length); } outputStream.close(); inputStream.close(); } public void deleteFile(String path) throws Exception { fileSystem.delete(new Path(path), true); } } ``` 2. FileController.java ```java import org.apache.commons.io.IOUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @Controller @RequestMapping("/file") public class FileController { @Autowired private HDFSUtil hdfsUtil; @RequestMapping(value = "/upload", method = RequestMethod.POST) @ResponseBody public String uploadFile(@RequestParam("file") MultipartFile file) { try { InputStream inputStream = file.getInputStream(); hdfsUtil.uploadFile(inputStream, "/hdfs/path/" + file.getOriginalFilename()); inputStream.close(); return "上传成功"; } catch (Exception e) { e.printStackTrace(); return "上传失败"; } } @RequestMapping(value = "/download", method = RequestMethod.GET) public void downloadFile(@RequestParam("path") String path, HttpServletResponse response) { try { OutputStream outputStream = response.getOutputStream(); response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment; filename=\"" + path + "\""); hdfsUtil.downloadFile("/hdfs/path/" + path, outputStream); outputStream.flush(); outputStream.close(); } catch (Exception e) { e.printStackTrace(); } } @RequestMapping(value = "/delete", method = RequestMethod.DELETE) @ResponseBody public String deleteFile(@RequestParam("path") String path) { try { hdfsUtil.deleteFile("/hdfs/path/" + path); return "删除成功"; } catch (Exception e) { e.printStackTrace(); return "删除失败"; } } @RequestMapping(value = "/list", method = RequestMethod.GET) @ResponseBody public String listFiles() { try { // 使用HDFS API获取文件列表 // ... return "文件列表"; } catch (Exception e) { e.printStackTrace(); return "获取文件列表失败"; } } } ``` 前端代码: 1. upload.html ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>HDFS文件上传</title> </head> <body> <form method="post" enctype="multipart/form-data" action="/file/upload"> <input type="file" name="file"/> <button type="submit">上传</button> </form> </body> </html> ``` 2. download.html ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>HDFS文件下载</title> </head> <body> <a href="/file/download?path=filename">下载</a> </body> </html> ``` 3. delete.html ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>HDFS文件删除</title> </head> <body> <form method="post" action="/file/delete"> <input type="text" name="path"/> <button type="submit">删除</button> </form> </body> </html> ``` 注意:上述代码中的文件路径 "/hdfs/path/" 需要根据实际的HDFS路径进行修改。另外,还需要添加相应的依赖,如Spring MVC、Spring、MyBatis、Hadoop HDFS等。

相关推荐

最新推荐

recommend-type

infrared-remote-candroid studiodemo

android studio下载
recommend-type

【新质生产力】新质生产力赋能智能制造数字化解决方案.pptx

【新质生产力】新质生产力赋能智能制造数字化解决方案.pptx
recommend-type

基于matlab实现的用于应用布格重力异常数据反演地下异常密度体.rar

基于matlab实现的用于应用布格重力异常数据反演地下异常密度体.rar
recommend-type

node-v8.10.0-linux-x64.tar.xz

Node.js,简称Node,是一个开源且跨平台的JavaScript运行时环境,它允许在浏览器外运行JavaScript代码。Node.js于2009年由Ryan Dahl创立,旨在创建高性能的Web服务器和网络应用程序。它基于Google Chrome的V8 JavaScript引擎,可以在Windows、Linux、Unix、Mac OS X等操作系统上运行。 Node.js的特点之一是事件驱动和非阻塞I/O模型,这使得它非常适合处理大量并发连接,从而在构建实时应用程序如在线游戏、聊天应用以及实时通讯服务时表现卓越。此外,Node.js使用了模块化的架构,通过npm(Node package manager,Node包管理器),社区成员可以共享和复用代码,极大地促进了Node.js生态系统的发展和扩张。 Node.js不仅用于服务器端开发。随着技术的发展,它也被用于构建工具链、开发桌面应用程序、物联网设备等。Node.js能够处理文件系统、操作数据库、处理网络请求等,因此,开发者可以用JavaScript编写全栈应用程序,这一点大大提高了开发效率和便捷性。 在实践中,许多大型企业和组织已经采用Node.js作为其Web应用程序的开发平台,如Netflix、PayPal和Walmart等。它们利用Node.js提高了应用性能,简化了开发流程,并且能更快地响应市场需求。
recommend-type

基于Yolov5目标检测和deepsort目标跟踪无人机跟踪.zip

无人机最强算法源码,易于部署和学习交流使用
recommend-type

RTL8188FU-Linux-v5.7.4.2-36687.20200602.tar(20765).gz

REALTEK 8188FTV 8188eus 8188etv linux驱动程序稳定版本, 支持AP,STA 以及AP+STA 共存模式。 稳定支持linux4.0以上内核。
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

系统函数是1+5*z^(-1)+5*z^(-2)+z^(-3) ,给出Matlab中求该系统频率响应的代码

假设系统函数为H(z),则其频率响应为H(w),可以通过以下代码求解: ``` syms z w H = 1 + 5*z^(-1) + 5*z^(-2) + z^(-3); % 定义系统函数 Hw = subs(H, z, exp(1i*w)); % 将z用e^(jw)代替 Hw = simplify(Hw); % 化简 absHw = abs(Hw); % 求幅度响应 angleHw = angle(Hw); % 求相位响应 ``` 其中,`simplify`函数用于化简表达式,`abs`函数用于求绝对值,`angle`函数用于求相位。
recommend-type

c++校园超市商品信息管理系统课程设计说明书(含源代码) (2).pdf

校园超市商品信息管理系统课程设计旨在帮助学生深入理解程序设计的基础知识,同时锻炼他们的实际操作能力。通过设计和实现一个校园超市商品信息管理系统,学生掌握了如何利用计算机科学与技术知识解决实际问题的能力。在课程设计过程中,学生需要对超市商品和销售员的关系进行有效管理,使系统功能更全面、实用,从而提高用户体验和便利性。 学生在课程设计过程中展现了积极的学习态度和纪律,没有缺勤情况,演示过程流畅且作品具有很强的使用价值。设计报告完整详细,展现了对问题的深入思考和解决能力。在答辩环节中,学生能够自信地回答问题,展示出扎实的专业知识和逻辑思维能力。教师对学生的表现予以肯定,认为学生在课程设计中表现出色,值得称赞。 整个课程设计过程包括平时成绩、报告成绩和演示与答辩成绩三个部分,其中平时表现占比20%,报告成绩占比40%,演示与答辩成绩占比40%。通过这三个部分的综合评定,最终为学生总成绩提供参考。总评分以百分制计算,全面评估学生在课程设计中的各项表现,最终为学生提供综合评价和反馈意见。 通过校园超市商品信息管理系统课程设计,学生不仅提升了对程序设计基础知识的理解与应用能力,同时也增强了团队协作和沟通能力。这一过程旨在培养学生综合运用技术解决问题的能力,为其未来的专业发展打下坚实基础。学生在进行校园超市商品信息管理系统课程设计过程中,不仅获得了理论知识的提升,同时也锻炼了实践能力和创新思维,为其未来的职业发展奠定了坚实基础。 校园超市商品信息管理系统课程设计的目的在于促进学生对程序设计基础知识的深入理解与掌握,同时培养学生解决实际问题的能力。通过对系统功能和用户需求的全面考量,学生设计了一个实用、高效的校园超市商品信息管理系统,为用户提供了更便捷、更高效的管理和使用体验。 综上所述,校园超市商品信息管理系统课程设计是一项旨在提升学生综合能力和实践技能的重要教学活动。通过此次设计,学生不仅深化了对程序设计基础知识的理解,还培养了解决实际问题的能力和团队合作精神。这一过程将为学生未来的专业发展提供坚实基础,使其在实际工作中能够胜任更多挑战。