SSM框架展示HDFS文件列表,上传文件,删除文件,下载文件前后端代码
时间: 2023-06-13 08:04:27 浏览: 76
基于SSM文件的上传和下载
前端代码:
文件列表展示:
```html
<table id="fileListTable">
<thead>
<tr>
<th>文件名</th>
<th>大小</th>
<th>修改日期</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr v-for="file in fileList">
<td>{{ file.name }}</td>
<td>{{ file.size }}</td>
<td>{{ file.modificationTime }}</td>
<td>
<a @click="deleteFile(file)">删除</a>
<a :href="'/download?path=' + file.path">下载</a>
</td>
</tr>
</tbody>
</table>
```
上传文件:
```html
<form enctype="multipart/form-data">
<input type="file" name="file"/>
<button type="button" @click="uploadFile">上传</button>
</form>
```
后端代码:
文件列表展示:
```java
@RequestMapping("/list")
@ResponseBody
public List<FileVO> getFileList(String path) throws IOException {
FileSystem fs = FileSystem.get(new Configuration());
FileStatus[] fileStatuses = fs.listStatus(new Path(path));
List<FileVO> fileList = new ArrayList<>();
for (FileStatus fileStatus : fileStatuses) {
FileVO fileVO = new FileVO();
fileVO.setName(fileStatus.getPath().getName());
fileVO.setPath(fileStatus.getPath().toString());
fileVO.setSize(fileStatus.getLen());
fileVO.setModificationTime(fileStatus.getModificationTime());
fileList.add(fileVO);
}
return fileList;
}
```
上传文件:
```java
@RequestMapping("/upload")
public String uploadFile(MultipartFile file) throws IOException {
FileSystem fs = FileSystem.get(new Configuration());
String fileName = file.getOriginalFilename();
Path path = new Path("/upload/" + fileName);
FSDataOutputStream outputStream = fs.create(path);
outputStream.write(file.getBytes());
outputStream.close();
return "redirect:/index.html";
}
```
删除文件:
```java
@RequestMapping("/delete")
public String deleteFile(String path) throws IOException {
FileSystem fs = FileSystem.get(new Configuration());
Path filePath = new Path(path);
fs.delete(filePath, false);
return "redirect:/index.html";
}
```
下载文件:
```java
@RequestMapping("/download")
public void downloadFile(String path, HttpServletResponse response) throws IOException {
FileSystem fs = FileSystem.get(new Configuration());
Path filePath = new Path(path);
FSDataInputStream inputStream = fs.open(filePath);
OutputStream outputStream = response.getOutputStream();
response.setHeader("Content-Disposition", "attachment; filename=" + filePath.getName());
IOUtils.copyBytes(inputStream, outputStream, 4096, true);
}
```
文件 VO 类:
```java
public class FileVO {
private String name;
private String path;
private long size;
private long modificationTime;
// getter and setter
}
```
阅读全文