javaSSM框架展示HDFS文件列表,上传文件,删除文件,下载文件前后端代码
时间: 2023-06-13 10:04:45 浏览: 50
以下是Java SSM框架展示HDFS文件列表、上传文件、删除文件、下载文件前后端代码的示例:
1. 展示HDFS文件列表
后端代码:
```
@Controller
@RequestMapping("/hdfs")
public class HdfsController {
@Autowired
private FileSystem fs;
@RequestMapping("/list")
@ResponseBody
public List<String> listFiles(String path) throws IOException {
if (StringUtils.isBlank(path)) {
path = "/";
}
Path hdfsPath = new Path(path);
RemoteIterator<LocatedFileStatus> fileStatusRemoteIterator = fs.listFiles(hdfsPath, true);
List<String> fileList = new ArrayList<>();
while (fileStatusRemoteIterator.hasNext()) {
LocatedFileStatus fileStatus = fileStatusRemoteIterator.next();
fileList.add(fileStatus.getPath().toString());
}
return fileList;
}
}
```
前端代码:
```
<script>
$.ajax({
url: "/hdfs/list",
type: "get",
data: {
path: "/"
},
success: function (data) {
var html = "";
for (var i = 0; i < data.length; i++) {
html += "<div>" + data[i] + "</div>";
}
$("#file-list").html(html);
}
});
</script>
<div id="file-list"></div>
```
2. 上传文件
后端代码:
```
@Controller
@RequestMapping("/hdfs")
public class HdfsController {
@Autowired
private FileSystem fs;
@RequestMapping("/upload")
@ResponseBody
public String uploadFile(MultipartFile file, String path) throws IOException {
if (StringUtils.isBlank(path)) {
path = "/";
}
Path hdfsPath = new Path(path);
FSDataOutputStream outputStream = fs.create(hdfsPath);
IOUtils.copy(file.getInputStream(), outputStream);
outputStream.close();
return "success";
}
}
```
前端代码:
```
<form id="upload-form">
<input type="file" name="file"/>
<input type="text" name="path" value="/"/>
<button type="submit">上传</button>
</form>
<script>
$("#upload-form").submit(function (event) {
event.preventDefault();
var formData = new FormData(this);
$.ajax({
url: "/hdfs/upload",
type: "post",
data: formData,
processData: false,
contentType: false,
success: function (data) {
alert("上传成功");
}
});
});
</script>
```
3. 删除文件
后端代码:
```
@Controller
@RequestMapping("/hdfs")
public class HdfsController {
@Autowired
private FileSystem fs;
@RequestMapping("/delete")
@ResponseBody
public String deleteFile(String path) throws IOException {
if (StringUtils.isBlank(path)) {
return "path is empty";
}
Path hdfsPath = new Path(path);
boolean result = fs.delete(hdfsPath, true);
if (result) {
return "success";
} else {
return "fail";
}
}
}
```
前端代码:
```
<form id="delete-form">
<input type="text" name="path" value="/"/>
<button type="submit">删除</button>
</form>
<script>
$("#delete-form").submit(function (event) {
event.preventDefault();
var path = $(this).find("input[name=path]").val();
$.ajax({
url: "/hdfs/delete",
type: "post",
data: {
path: path
},
success: function (data) {
if (data == "success") {
alert("删除成功");
} else {
alert("删除失败");
}
}
});
});
</script>
```
4. 下载文件
后端代码:
```
@Controller
@RequestMapping("/hdfs")
public class HdfsController {
@Autowired
private FileSystem fs;
@RequestMapping("/download")
public void downloadFile(HttpServletResponse response, String path) throws IOException {
if (StringUtils.isBlank(path)) {
return;
}
Path hdfsPath = new Path(path);
FSDataInputStream inputStream = fs.open(hdfsPath);
OutputStream outputStream = response.getOutputStream();
IOUtils.copy(inputStream, outputStream);
inputStream.close();
outputStream.flush();
}
}
```
前端代码:
```
<a href="/hdfs/download?path=/test.txt">下载</a>
```