SSM框架分页展示HDFS文件列表,上传文件,删除文件,下载文件前后端代码
时间: 2023-06-12 13:05:41 浏览: 82
SSM框架是Spring + SpringMVC + MyBatis的简称,这里我假设你已经熟悉了SSM框架的基本使用。
展示HDFS文件列表
后端代码:
```java
@RequestMapping("/file/list")
public String listHdfsFiles(Model model, @RequestParam(value = "path", defaultValue = "/") String path) throws IOException {
FileSystem fs = FileSystem.get(new Configuration());
FileStatus[] statuses = fs.listStatus(new Path(path));
List<Map<String, Object>> fileList = new ArrayList<>();
for (FileStatus status : statuses) {
Map<String, Object> fileInfo = new HashMap<>();
fileInfo.put("name", status.getPath().getName());
fileInfo.put("isDir", status.isDirectory());
fileInfo.put("size", status.getLen());
fileInfo.put("modifiedTime", status.getModificationTime());
fileList.add(fileInfo);
}
model.addAttribute("fileList", fileList);
model.addAttribute("path", path);
return "file/list";
}
```
前端页面:
```html
<table>
<thead>
<tr>
<th>文件名</th>
<th>类型</th>
<th>大小</th>
<th>修改时间</th>
</tr>
</thead>
<tbody>
<c:forEach items="${fileList}" var="file">
<tr>
<td>${file.name}</td>
<td>${file.isDir ? '文件夹' : '文件'}</td>
<td>${file.size}</td>
<td>${new Date(file.modifiedTime)}</td>
</tr>
</c:forEach>
</tbody>
</table>
```
上传文件
后端代码:
```java
@RequestMapping("/file/upload")
public String uploadHdfsFile(@RequestParam("file") MultipartFile file, @RequestParam("path") String path) throws IOException {
FileSystem fs = FileSystem.get(new Configuration());
if (!fs.exists(new Path(path))) {
fs.mkdirs(new Path(path));
}
String fileName = file.getOriginalFilename();
Path destPath = new Path(path + "/" + fileName);
if (fs.exists(destPath)) {
fs.delete(destPath, false);
}
FSDataOutputStream outputStream = fs.create(destPath);
IOUtils.copy(file.getInputStream(), outputStream);
outputStream.close();
return "redirect:/file/list?path=" + path;
}
```
前端页面:
```html
<form method="post" action="${pageContext.request.contextPath}/file/upload" enctype="multipart/form-data">
<input type="file" name="file" required>
<input type="text" name="path" value="<c:out value='${path}'/>" hidden>
<button type="submit">上传</button>
</form>
```
删除文件
后端代码:
```java
@RequestMapping("/file/delete")
public String deleteHdfsFile(@RequestParam("path") String path) throws IOException {
FileSystem fs = FileSystem.get(new Configuration());
fs.delete(new Path(path), true);
Path parentPath = new Path(path).getParent();
return "redirect:/file/list?path=" + (parentPath == null ? "/" : parentPath.toString());
}
```
前端页面:
```html
<a href="${pageContext.request.contextPath}/file/delete?path=${file.name}">删除</a>
```
下载文件
后端代码:
```java
@RequestMapping("/file/download")
public void downloadHdfsFile(HttpServletRequest request, HttpServletResponse response, @RequestParam("path") String path) throws IOException {
FileSystem fs = FileSystem.get(new Configuration());
FSDataInputStream inputStream = fs.open(new Path(path));
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=\"" + new Path(path).getName() + "\"");
IOUtils.copy(inputStream, response.getOutputStream());
inputStream.close();
}
```
前端页面:
```html
<a href="${pageContext.request.contextPath}/file/download?path=${file.name}">下载</a>
```
阅读全文