请用springboot+vue编写查看文件内容的代码
时间: 2023-04-04 16:01:37 浏览: 58
可以的,以下是使用 Spring Boot 和 Vue.js 编写查看文件内容的代码示例:
Spring Boot 后端代码:
```java
@RestController
@RequestMapping("/api")
public class FileController {
@GetMapping("/file")
public String getFileContent(@RequestParam("path") String path) throws IOException {
File file = new File(path);
if (!file.exists()) {
return "文件不存在";
}
BufferedReader reader = new BufferedReader(new FileReader(file));
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
reader.close();
return sb.toString();
}
}
```
Vue.js 前端代码:
```html
<template>
<div>
<input type="text" v-model="path" placeholder="请输入文件路径" />
<button @click="getFileContent">查看文件内容</button>
<pre>{{ content }}</pre>
</div>
</template>
<script>
export default {
data() {
return {
path: "",
content: "",
};
},
methods: {
getFileContent() {
fetch(`/api/file?path=${this.path}`)
.then((res) => res.text())
.then((data) => {
this.content = data;
})
.catch((err) => {
console.error(err);
});
},
},
};
</script>
```
这个示例可以让用户输入文件路径,然后通过后端 API 获取文件内容并在前端展示。