安装文档见 centOS安装FastDFS
Java使用方法:
- 引入依赖
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
</dependency>
2.引入配置类
@Configuration
@Import(FdfsClientConfig.class)
// 解决jmx重复注册bean的问题
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
public class FastClientImporter {
}
3.编写配置类
fdfs:
so-timeout: 1501
connect-timeout: 601
thumb-image: # 缩略图
width: 60
height: 60
tracker-list: # tracker地址
- address:port
4.类中
@Autowired
private FastFileStorageClient storageClient;
@PostMapping("/image")
public ResponseEntity<String> uploadImage(@RequestParam("file") MultipartFile file) {
StorePath storePath = null;
try {
storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(), "jpg", null);
} catch (IOException e) {
e.printStackTrace();
}
// 返回200,并且携带url路径
return ResponseEntity.ok(storePath.getFullPath());
}
return : group1/M00/00/00/wKgemFwODaGAZ0OZAADJSKSfgD0811.jpg
访问地址: address/group1/M00/00/00/wKgemFwODaGAZ0OZAADJSKSfgD0811.jpg