用springboot+hdfs+mysql实现文件上传下载
时间: 2023-06-15 10:04:57 浏览: 115
下面是Spring Boot集成HDFS和MySQL实现文件上传和下载的示例代码:
1. 配置文件
application.properties:
```
#HDFS配置
hadoop.hdfs.uri=hdfs://localhost:9000
hadoop.hdfs.user.name=hadoop
#MySQL配置
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#上传文件存储路径
upload.path=/usr/local/uploads/
```
2. 实体类
FileEntity.java:
```java
@Entity
@Table(name = "file")
@Data
public class FileEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false)
private String name;
@Column(nullable = false)
private String path;
@Column(nullable = false)
private String type;
@Column(nullable = false)
private Long size;
@Column(name = "create_time", nullable = false)
private Date createTime;
}
```
3. HDFS工具类
HdfsUtils.java:
```java
@Component
public class HdfsUtils {
@Value("${hadoop.hdfs.uri}")
private String hdfsUri;
@Value("${hadoop.hdfs.user.name}")
private String hdfsUserName;
private FileSystem fileSystem;
@PostConstruct
public void init() throws IOException {
Configuration configuration = new Configuration();
configuration.set("fs.defaultFS", hdfsUri);
fileSystem = FileSystem.get(configuration);
}
public void uploadFile(String srcPath, String destPath) throws IOException {
Path src = new Path(srcPath);
Path dst = new Path(destPath);
fileSystem.copyFromLocalFile(src, dst);
}
public void downloadFile(String srcPath, String destPath) throws IOException {
Path src = new Path(srcPath);
Path dst = new Path(destPath);
fileSystem.copyToLocalFile(src, dst);
}
public void deleteFile(String path) throws IOException {
Path src = new Path(path);
fileSystem.delete(src, true);
}
}
```
4. 文件服务接口
FileService.java:
```java
public interface FileService {
FileEntity save(MultipartFile file) throws IOException;
Resource loadFileAsResource(Long id) throws FileNotFoundException;
void delete(Long id) throws IOException;
Page<FileEntity> findByPage(int pageNum, int pageSize);
}
```
5. 文件服务实现类
FileServiceImpl.java:
```java
@Service
public class FileServiceImpl implements FileService {
private final String uploadPath = System.getProperty("user.dir") + "/uploads/";
@Autowired
private FileRepository fileRepository;
@Autowired
private HdfsUtils hdfsUtils;
@Override
public FileEntity save(MultipartFile file) throws IOException {
String fileName = file.getOriginalFilename();
String fileType = fileName.substring(fileName.lastIndexOf(".") + 1);
String filePath = uploadPath + fileName;
File destFile = new File(filePath);
file.transferTo(destFile);
String hdfsFilePath = "/upload/" + fileName;
hdfsUtils.uploadFile(filePath, hdfsFilePath);
FileEntity fileEntity = new FileEntity();
fileEntity.setName(fileName);
fileEntity.setPath(hdfsFilePath);
fileEntity.setType(fileType);
fileEntity.setSize(file.getSize());
fileEntity.setCreateTime(new Date());
return fileRepository.save(fileEntity);
}
@Override
public Resource loadFileAsResource(Long id) throws FileNotFoundException {
FileEntity fileEntity = fileRepository.findById(id).orElseThrow(() -> new FileNotFoundException("文件不存在"));
String filePath = fileEntity.getPath();
String fileName = fileEntity.getName();
Path path = new Path(filePath);
try {
FileSystem fs = FileSystem.get(URI.create(hdfsUtils.getHdfsUri()), hdfsUtils.getConfiguration(),
hdfsUtils.getHdfsUserName());
FSDataInputStream inputStream = fs.open(path);
return new InputStreamResource(inputStream);
} catch (IOException e) {
e.printStackTrace();
throw new FileNotFoundException(e.getMessage());
}
}
@Override
public void delete(Long id) throws IOException {
FileEntity fileEntity = fileRepository.findById(id).orElseThrow(() -> new FileNotFoundException("文件不存在"));
String filePath = fileEntity.getPath();
hdfsUtils.deleteFile(filePath);
fileRepository.deleteById(id);
}
@Override
public Page<FileEntity> findByPage(int pageNum, int pageSize) {
Pageable pageable = PageRequest.of(pageNum, pageSize, Sort.Direction.DESC, "createTime");
return fileRepository.findAll(pageable);
}
}
```
6. 文件控制器
FileController.java:
```java
@RestController
@RequestMapping("/file")
public class FileController {
@Autowired
private FileService fileService;
@PostMapping
public ResponseEntity<?> uploadFile(@RequestParam("file") MultipartFile file) throws IOException {
FileEntity fileEntity = fileService.save(file);
URI location = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}")
.buildAndExpand(fileEntity.getId()).toUri();
return ResponseEntity.created(location).build();
}
@GetMapping("/{id}")
public ResponseEntity<Resource> downloadFile(@PathVariable Long id) throws FileNotFoundException {
Resource resource = fileService.loadFileAsResource(id);
return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION,
"attachment; filename=\"" + resource.getFilename() + "\"").body(resource);
}
@DeleteMapping("/{id}")
public ResponseEntity<?> deleteFile(@PathVariable Long id) throws IOException {
fileService.delete(id);
return ResponseEntity.noContent().build();
}
@GetMapping("/list")
public ResponseEntity<Page<FileEntity>> getList(@RequestParam(defaultValue = "0") int pageNum,
@RequestParam(defaultValue = "10") int pageSize) {
Page<FileEntity> page = fileService.findByPage(pageNum, pageSize);
return ResponseEntity.ok(page);
}
}
```
7. 文件存储仓库
FileRepository.java:
```java
public interface FileRepository extends JpaRepository<FileEntity, Long> {
}
```
这样,文件上传和下载的功能就完成了。启动应用程序,可以使用POSTMAN或其他客户端上传、下载和删除文件了。
阅读全文