javaSSM框架展示HDFS文件列表,上传文件,删除文件,下载文件前后端代码
时间: 2023-06-13 08:04:24 浏览: 100
基于SSM文件的上传和下载
这里只提供简单的代码示例供参考,具体实现需要根据自己的实际情况进行调整。
展示HDFS文件列表:
后端代码:
```java
@RequestMapping("/listHdfsFiles")
@ResponseBody
public List<String> listHdfsFiles() throws Exception {
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://localhost:9000"), conf);
RemoteIterator<LocatedFileStatus> files = fs.listFiles(new Path("/"), true);
List<String> fileList = new ArrayList<>();
while (files.hasNext()) {
LocatedFileStatus file = files.next();
fileList.add(file.getPath().toString());
}
return fileList;
}
```
前端代码:
```javascript
<script>
$(document).ready(function () {
$.ajax({
type: "GET",
url: "/listHdfsFiles",
success: function (data) {
for (var i = 0; i < data.length; i++) {
$("#fileList").append("<li>" + data[i] + "</li>");
}
},
error: function (xhr, status, error) {
console.log(xhr.responseText);
}
});
});
</script>
<ul id="fileList"></ul>
```
上传文件:
后端代码:
```java
@RequestMapping(value = "/uploadHdfsFile", method = RequestMethod.POST)
@ResponseBody
public String uploadHdfsFile(@RequestParam("file") MultipartFile file) throws Exception {
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://localhost:9000"), conf);
Path path = new Path("/" + file.getOriginalFilename());
FSDataOutputStream outputStream = fs.create(path);
outputStream.write(file.getBytes());
outputStream.close();
return "success";
}
```
前端代码:
```html
<form id="uploadForm" enctype="multipart/form-data">
<input type="file" name="file" /><br/>
<input type="button" value="上传" onclick="uploadFile()" />
</form>
<script>
function uploadFile() {
var formData = new FormData($("#uploadForm")[0]);
$.ajax({
type: "POST",
url: "/uploadHdfsFile",
data: formData,
processData: false,
contentType: false,
success: function (data) {
console.log(data);
},
error: function (xhr, status, error) {
console.log(xhr.responseText);
}
});
}
</script>
```
删除文件:
后端代码:
```java
@RequestMapping(value = "/deleteHdfsFile", method = RequestMethod.POST)
@ResponseBody
public String deleteHdfsFile(@RequestParam("filePath") String filePath) throws Exception {
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://localhost:9000"), conf);
Path path = new Path(filePath);
fs.delete(path, false);
return "success";
}
```
前端代码:
```javascript
function deleteFile(filePath) {
$.ajax({
type: "POST",
url: "/deleteHdfsFile",
data: {"filePath": filePath},
success: function (data) {
console.log(data);
},
error: function (xhr, status, error) {
console.log(xhr.responseText);
}
});
}
```
下载文件:
后端代码:
```java
@RequestMapping(value = "/downloadHdfsFile", method = RequestMethod.POST)
@ResponseBody
public void downloadHdfsFile(@RequestParam("filePath") String filePath, HttpServletResponse response) throws Exception {
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://localhost:9000"), conf);
Path path = new Path(filePath);
FSDataInputStream inputStream = fs.open(path);
OutputStream outputStream = response.getOutputStream();
IOUtils.copyBytes(inputStream, outputStream, conf);
inputStream.close();
outputStream.close();
}
```
前端代码:
```javascript
function downloadFile(filePath) {
var xhr = new XMLHttpRequest();
xhr.open("POST", "/downloadHdfsFile", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.responseType = "blob";
xhr.onload = function () {
var blob = this.response;
var a = document.createElement("a");
var url = window.URL.createObjectURL(blob);
a.href = url;
a.download = filePath.substring(filePath.lastIndexOf("/") + 1);
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
};
xhr.send("filePath=" + filePath);
}
```
阅读全文