SSM框架展示HDFS文件列表,上传文件,删除文件,下载文件前后端代码
时间: 2023-06-12 10:05:55 浏览: 53
1. 展示HDFS文件列表
前端代码:
```html
<!-- 文件列表展示 -->
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>文件名</th>
<th>大小</th>
<th>修改时间</th>
<th>操作</th>
</tr>
</thead>
<tbody id="fileList">
<!-- 文件列表数据动态生成 -->
</tbody>
</table>
```
后端代码:
```java
@RequestMapping("/listFiles")
@ResponseBody
public List<Map<String, Object>> listFiles(String path) throws Exception {
List<Map<String, Object>> fileList = new ArrayList<>();
FileSystem fs = FileSystem.get(new Configuration());
Path hdfsPath = new Path(path);
FileStatus[] fileStatuses = fs.listStatus(hdfsPath);
for (FileStatus fileStatus : fileStatuses) {
Map<String, Object> fileMap = new HashMap<>();
fileMap.put("name", fileStatus.getPath().getName());
fileMap.put("size", fileStatus.getLen());
fileMap.put("modificationTime", fileStatus.getModificationTime());
fileList.add(fileMap);
}
fs.close();
return fileList;
}
```
2. 上传文件
前端代码:
```html
<!-- 文件上传 -->
<form id="uploadForm" enctype="multipart/form-data">
<input type="file" name="file"/>
<button type="submit" class="btn btn-primary">上传</button>
</form>
```
后端代码:
```java
@RequestMapping("/uploadFile")
@ResponseBody
public String uploadFile(@RequestParam("file") MultipartFile file, String path) throws Exception {
FileSystem fs = FileSystem.get(new Configuration());
Path hdfsPath = new Path(path + "/" + file.getOriginalFilename());
FSDataOutputStream outputStream = fs.create(hdfsPath);
IOUtils.copy(file.getInputStream(), outputStream);
outputStream.close();
fs.close();
return "上传成功";
}
```
3. 删除文件
前端代码:
```html
<!-- 文件删除 -->
<button class="btn btn-danger" onclick="deleteFile()">删除</button>
```
后端代码:
```java
@RequestMapping("/deleteFile")
@ResponseBody
public String deleteFile(String path) throws Exception {
FileSystem fs = FileSystem.get(new Configuration());
Path hdfsPath = new Path(path);
boolean result = fs.delete(hdfsPath, false);
fs.close();
if (result) {
return "删除成功";
} else {
return "删除失败";
}
}
```
4. 下载文件
前端代码:
```html
<!-- 文件下载 -->
<a class="btn btn-success" href="#" onclick="downloadFile()">下载</a>
```
后端代码:
```java
@RequestMapping("/downloadFile")
@ResponseBody
public String downloadFile(HttpServletResponse response, String path) throws Exception {
FileSystem fs = FileSystem.get(new Configuration());
Path hdfsPath = new Path(path);
if (fs.exists(hdfsPath)) {
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=" + hdfsPath.getName());
FSDataInputStream inputStream = fs.open(hdfsPath);
IOUtils.copy(inputStream, response.getOutputStream());
inputStream.close();
fs.close();
return "下载成功";
} else {
fs.close();
return "文件不存在";
}
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)