SpringBoot + Hadoop + HDFS + Vue 实现一个简单的文件管理系统
由于篇幅所限,以下仅展示核心代码和配置,不包含完整的前后端代码和配置。
后端代码(SpringBoot + Hadoop)
pom.xml
依赖配置(仅列出相关依赖):
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs-client</artifactId>
<version>你的Hadoop版本</version>
</dependency>
</dependencies>
FileController.java
:
@RestController
@RequestMapping("/api/file")
public class FileController {
@Autowired
private FileService fileService;
@PostMapping("/upload")
public ResponseEntity<String> upload(@RequestParam("file") MultipartFile file) {
return fileService.upload(file);
}
@GetMapping("/list")
public ResponseEntity<List<FileInfo>> list() {
return fileService.list();
}
@DeleteMapping("/delete/{fileName}")
public ResponseEntity<String> delete(@PathVariable("fileName") String fileName) {
return fileService.delete(fileName);
}
}
FileService.java
:
@Service
public class FileService {
@Autowired
private FileSystem fileSystem;
public ResponseEntity<String> upload(MultipartFile file) {
try {
Path path = new Path("/" + file.getOriginalFilename());
fileSystem.copyFromLocalFile(new Path(file.getOriginalFilename()));
return ResponseEntity.ok("File uploaded successfully.");
} catch (IOException e) {
e.printStackTrace();
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Upload failed.");
}
}
public ResponseEntity<List<FileInfo>> list() {
List<FileInfo> fileList = new ArrayList<>();
try {
FileStatus[] listStatus = fileSystem.listStatus(new Path("/"));
for (FileStatus fileStatus : listStatus) {
fileList.add(new FileInfo(fileStatus.getPath().getName(), fileStatus.getLen()));
}
return Respons
评论已关闭