ssm框架展示hdfs文件前后端代码
时间: 2023-06-13 18:05:14 浏览: 148
前端代码:
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>展示HDFS文件</title>
</head>
<body>
<div>
<form>
<label for="path">请输入文件路径:</label>
<input type="text" name="path" id="path" />
<button type="button" onclick="showFile()">展示</button>
</form>
</div>
<div>
<pre id="fileContent"></pre>
</div>
<script>
function showFile() {
let path = document.getElementById("path").value;
if (!path) {
alert("请输入文件路径!");
} else {
let xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
let fileContent = document.getElementById("fileContent");
fileContent.textContent = xhr.responseText;
} else {
alert("展示文件失败!");
}
}
};
xhr.open("GET", "/showFile?path=" + path, true);
xhr.send();
}
}
</script>
</body>
</html>
```
后端代码:
```java
@Controller
public class FileController {
@RequestMapping(value = "/showFile", produces = "text/plain; charset=utf-8")
@ResponseBody
public String showFile(String path) {
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://localhost:9000");
FileSystem fs = null;
Path filePath = new Path(path);
try {
fs = FileSystem.get(conf);
if (fs.exists(filePath)) {
FSDataInputStream in = fs.open(filePath);
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
reader.close();
in.close();
fs.close();
return sb.toString();
} else {
fs.close();
return "文件不存在!";
}
} catch (IOException e) {
e.printStackTrace();
return "展示文件失败!";
}
}
}
```
以上代码使用了SSM框架,其中前端使用了HTML和JavaScript,后端使用了SpringMVC和Hadoop的Java API。用户在前端输入需要展示的文件路径,点击“展示”按钮,前端通过Ajax发送GET请求到后端,后端根据路径读取HDFS中的文件内容,将内容返回给前端展示在页面上。
阅读全文