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

时间: 2023-06-11 18:09:07 浏览: 50
1. 分页展示Hdfs文件列表的后端代码: ``` // HdfsFileController.java @RestController @RequestMapping("/hdfs") public class HdfsFileController { @Autowired private HdfsFileService hdfsFileService; @GetMapping("/files") public Page<HdfsFile> getHdfsFiles(@RequestParam("page") int page, @RequestParam("size") int size) { return hdfsFileService.getHdfsFiles(page, size); } } ``` ``` // HdfsFileService.java @Service public class HdfsFileService { @Value("${hdfs.basePath}") private String basePath; @Autowired private FileSystem fileSystem; public Page<HdfsFile> getHdfsFiles(int page, int size) { try { Path path = new Path(basePath); FileStatus[] fileStatuses = fileSystem.listStatus(path); List<HdfsFile> hdfsFiles = Arrays.stream(fileStatuses) .map(fileStatus -> new HdfsFile(fileStatus.getPath().getName(), fileStatus.getLen())) .collect(Collectors.toList()); return new PageImpl<>(hdfsFiles, PageRequest.of(page, size), hdfsFiles.size()); } catch (IOException e) { throw new RuntimeException("Failed to list HDFS files", e); } } } ``` 2. 分页展示Hdfs文件列表的前端代码: ``` <!-- hdfs-file-list.vue --> <template> <div> <h2>Hdfs File List</h2> <table> <thead> <tr> <th>File Name</th> <th>File Size</th> </tr> </thead> <tbody> <tr v-for="file in files" :key="file.name"> <td>{{ file.name }}</td> <td>{{ file.size }}</td> </tr> </tbody> </table> <div> <button @click="prevPage">Prev</button> <button @click="nextPage">Next</button> </div> </div> </template> <script> import axios from 'axios'; export default { data() { return { files: [], page: 0, size: 10, }; }, mounted() { this.getFiles(); }, methods: { getFiles() { axios.get(`/hdfs/files?page=${this.page}&size=${this.size}`) .then((response) => { this.files = response.data.content; }); }, prevPage() { if (this.page > 0) { this.page -= 1; this.getFiles(); } }, nextPage() { this.page += 1; this.getFiles(); }, }, }; </script> ``` 3. 上传文件的后端代码: ``` // HdfsFileController.java @RestController @RequestMapping("/hdfs") public class HdfsFileController { @Autowired private HdfsFileService hdfsFileService; @PostMapping("/files") public void uploadFile(@RequestParam("file") MultipartFile file) { hdfsFileService.uploadFile(file); } } ``` ``` // HdfsFileService.java @Service public class HdfsFileService { @Value("${hdfs.basePath}") private String basePath; @Autowired private FileSystem fileSystem; public void uploadFile(MultipartFile file) { try { Path path = new Path(basePath + "/" + file.getOriginalFilename()); FSDataOutputStream outputStream = fileSystem.create(path); IOUtils.copy(file.getInputStream(), outputStream); } catch (IOException e) { throw new RuntimeException("Failed to upload file to HDFS", e); } } } ``` 4. 上传文件的前端代码: ``` <!-- hdfs-file-upload.vue --> <template> <div> <h2>Upload File to HDFS</h2> <input type="file" ref="fileInput" /> <button @click="uploadFile">Upload</button> </div> </template> <script> import axios from 'axios'; export default { methods: { uploadFile() { const file = this.$refs.fileInput.files[0]; const formData = new FormData(); formData.append('file', file); axios.post('/hdfs/files', formData) .then(() => { alert('File uploaded successfully'); }) .catch((error) => { alert(`Failed to upload file: ${error.message}`); }); }, }, }; </script> ``` 5. 删除文件的后端代码: ``` // HdfsFileController.java @RestController @RequestMapping("/hdfs") public class HdfsFileController { @Autowired private HdfsFileService hdfsFileService; @DeleteMapping("/files/{fileName:.+}") public void deleteFile(@PathVariable String fileName) { hdfsFileService.deleteFile(fileName); } } ``` ``` // HdfsFileService.java @Service public class HdfsFileService { @Value("${hdfs.basePath}") private String basePath; @Autowired private FileSystem fileSystem; public void deleteFile(String fileName) { try { Path path = new Path(basePath + "/" + fileName); boolean deleted = fileSystem.delete(path, false); if (!deleted) { throw new FileNotFoundException("File not found in HDFS: " + fileName); } } catch (IOException e) { throw new RuntimeException("Failed to delete file from HDFS", e); } } } ``` 6. 删除文件的前端代码: ``` <!-- hdfs-file-list.vue --> <template> <div> <h2>Hdfs File List</h2> <table> <thead> <tr> <th>File Name</th> <th>File Size</th> <th>Actions</th> </tr> </thead> <tbody> <tr v-for="file in files" :key="file.name"> <td>{{ file.name }}</td> <td>{{ file.size }}</td> <td> <button @click="deleteFile(file.name)">Delete</button> </td> </tr> </tbody> </table> <div> <button @click="prevPage">Prev</button> <button @click="nextPage">Next</button> </div> </div> </template> <script> import axios from 'axios'; export default { data() { return { files: [], page: 0, size: 10, }; }, mounted() { this.getFiles(); }, methods: { getFiles() { axios.get(`/hdfs/files?page=${this.page}&size=${this.size}`) .then((response) => { this.files = response.data.content; }); }, deleteFile(fileName) { axios.delete(`/hdfs/files/${fileName}`) .then(() => { this.getFiles(); alert('File deleted successfully'); }) .catch((error) => { alert(`Failed to delete file: ${error.message}`); }); }, prevPage() { if (this.page > 0) { this.page -= 1; this.getFiles(); } }, nextPage() { this.page += 1; this.getFiles(); }, }, }; </script> ``` 7. 下载文件的后端代码: ``` // HdfsFileController.java @RestController @RequestMapping("/hdfs") public class HdfsFileController { @Autowired private HdfsFileService hdfsFileService; @GetMapping("/files/{fileName:.+}") public ResponseEntity<Resource> downloadFile(@PathVariable String fileName) { Resource resource = hdfsFileService.downloadFile(fileName); return ResponseEntity.ok() .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName + "\"") .body(resource); } } ``` ``` // HdfsFileService.java @Service public class HdfsFileService { @Value("${hdfs.basePath}") private String basePath; @Autowired private FileSystem fileSystem; public Resource downloadFile(String fileName) { try { Path path = new Path(basePath + "/" + fileName); FSDataInputStream inputStream = fileSystem.open(path); ByteArrayResource resource = new ByteArrayResource(IOUtils.toByteArray(inputStream)); return resource; } catch (IOException e) { throw new RuntimeException("Failed to download file from HDFS", e); } } } ``` 8. 下载文件的前端代码: ``` <!-- hdfs-file-list.vue --> <template> <div> <h2>Hdfs File List</h2> <table> <thead> <tr> <th>File Name</th> <th>File Size</th> <th>Actions</th> </tr> </thead> <tbody> <tr v-for="file in files" :key="file.name"> <td>{{ file.name }}</td> <td>{{ file.size }}</td> <td> <button @click="downloadFile(file.name)">Download</button> <button @click="deleteFile(file.name)">Delete</button> </td> </tr> </tbody> </table> <div> <button @click="prevPage">Prev</button> <button @click="nextPage">Next</button> </div> </div> </template> <script> import axios from 'axios'; export default { data() { return { files: [], page: 0, size: 10, }; }, mounted() { this.getFiles(); }, methods: { getFiles() { axios.get(`/hdfs/files?page=${this.page}&size=${this.size}`) .then((response) => { this.files = response.data.content; }); }, downloadFile(fileName) { axios.get(`/hdfs/files/${fileName}`, { responseType: 'blob' }) .then((response) => { const url = window.URL.createObjectURL(new Blob([response.data])); const link = document.createElement('a'); link.href = url; link.setAttribute('download', fileName); document.body.appendChild(link); link.click(); }) .catch((error) => { alert(`Failed to download file: ${error.message}`); }); }, deleteFile(fileName) { axios.delete(`/hdfs/files/${fileName}`) .then(() => { this.getFiles(); alert('File deleted successfully'); }) .catch((error) => { alert(`Failed to delete file: ${error.message}`); }); }, prevPage() { if (this.page > 0) { this.page -= 1; this.getFiles(); } }, nextPage() { this.page += 1; this.getFiles(); }, }, }; </script> ```

相关推荐

最新推荐

recommend-type

基于jsp+servlet+mysql的javaweb健身房俱乐部系统

包括系统管理后台和前端动态网页的设计搭建。系统管理后台提供给俱乐部员工使用,可以对俱乐部的课程、器材、房间等进行管理维护;前端网页主要提供给消费者使用,可以在线浏览课程、预约上课等。 技术栈:JavaScript,Mysql 数据库,JSP、tomcat、HTML、CSS。
recommend-type

Tomcat安装配置基础详细教程讲解.docx

tomcat安装及配置教程 Tomcat安装配置基础详细教程讲解.docx
recommend-type

51单片机智能百叶窗项目

51单片机智能百叶窗项目
recommend-type

PHP课程网站络管理系统

PHP课程网站络管理系统
recommend-type

2023年上半年度TikTok行业白皮书电子版.zip

2023年上半年度TikTok行业白皮书电子版.zip
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

MATLAB柱状图在信号处理中的应用:可视化信号特征和频谱分析

![matlab画柱状图](https://img-blog.csdnimg.cn/3f32348f1c9c4481a6f5931993732f97.png) # 1. MATLAB柱状图概述** MATLAB柱状图是一种图形化工具,用于可视化数据中不同类别或组的分布情况。它通过绘制垂直条形来表示每个类别或组中的数据值。柱状图在信号处理中广泛用于可视化信号特征和进行频谱分析。 柱状图的优点在于其简单易懂,能够直观地展示数据分布。在信号处理中,柱状图可以帮助工程师识别信号中的模式、趋势和异常情况,从而为信号分析和处理提供有价值的见解。 # 2. 柱状图在信号处理中的应用 柱状图在信号处理
recommend-type

从键盘输入一段英文字符串,其中包含多个字母‘h',请编写程序利用正则表达式,将英文字符串中的’h'全部改为‘H’

好的,我可以帮您解决这个问题。 您可以使用 Python 编写一个程序来实现这个功能。具体步骤如下: 1. 首先导入 re 模块,这个模块是 Python 中用于处理正则表达式的模块。 2. 然后定义一个包含英文字符串的变量,例如: text = "This is a string containing multiple h's." 3. 接下来,使用 re.sub() 方法来进行替换操作。re.sub() 方法的第一个参数是正则表达式,第二个参数是替换的字符或字符串,第三个参数是被替换的字符串。在这个例子里,我们需要将所有的小写字母 h 替换成大写字母 H,所以正则表达式可以写成
recommend-type

JSBSim Reference Manual

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