SSM框架展示HDFS文件列表,上传文件,删除文件,下载文件前后端代码
时间: 2023-06-12 10:05:57 浏览: 41
1. 展示HDFS文件列表的后端代码
```
@Controller
@RequestMapping("/hdfs")
public class HdfsController {
@Autowired
private HdfsService hdfsService;
@GetMapping("/list")
public ModelAndView listFiles() throws IOException {
ModelAndView mav = new ModelAndView("hdfs");
List<String> files = hdfsService.listFiles("/");
mav.addObject("files", files);
return mav;
}
}
```
2. 展示HDFS文件列表的前端代码
```
<table>
<thead>
<tr>
<th>文件名</th>
<th>大小</th>
<th>修改时间</th>
</tr>
</thead>
<tbody>
<c:forEach items="${files}" var="file">
<tr>
<td>${file.name}</td>
<td>${file.size}</td>
<td>${file.modificationTime}</td>
</tr>
</c:forEach>
</tbody>
</table>
```
3. 上传文件的后端代码
```
@Controller
@RequestMapping("/hdfs")
public class HdfsController {
@Autowired
private HdfsService hdfsService;
@PostMapping("/upload")
public String uploadFile(@RequestParam("file") MultipartFile file) throws IOException {
String fileName = file.getOriginalFilename();
Path path = new Path("/" + fileName);
FSDataOutputStream outputStream = hdfsService.createFile(path);
InputStream inputStream = file.getInputStream();
IOUtils.copy(inputStream, outputStream);
outputStream.close();
return "redirect:/hdfs/list";
}
}
```
4. 上传文件的前端代码
```
<form action="/hdfs/upload" method="POST" enctype="multipart/form-data">
<input type="file" name="file" />
<button type="submit">上传文件</button>
</form>
```
5. 删除文件的后端代码
```
@Controller
@RequestMapping("/hdfs")
public class HdfsController {
@Autowired
private HdfsService hdfsService;
@GetMapping("/delete")
public String deleteFile(@RequestParam("path") String path) throws IOException {
hdfsService.deleteFile(path);
return "redirect:/hdfs/list";
}
}
```
6. 下载文件的后端代码
```
@Controller
@RequestMapping("/hdfs")
public class HdfsController {
@Autowired
private HdfsService hdfsService;
@GetMapping("/download")
public ResponseEntity<byte[]> downloadFile(@RequestParam("path") String path) throws IOException {
InputStream inputStream = hdfsService.openFile(path);
byte[] bytes = IOUtils.toByteArray(inputStream);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.setContentDispositionFormData("attachment", path);
headers.setContentLength(bytes.length);
inputStream.close();
return new ResponseEntity<>(bytes, headers, HttpStatus.OK);
}
}
```
7. 下载文件的前端代码
```
<a href="/hdfs/download?path=${file.name}">下载</a>
```