引入依赖
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.26.7</version>
</dependency>
添加配置
fdfs:
# 连接的超时时间
connect-timeout: 3000
# 读取的超时时间
so-timeout: 3000
#tracker服务所在的ip地址和端口号
tracker-list: 10.240.3x.xx2:22122
Controller
@RestController
@RequestMapping("/img")
public class ImgController {
@Autowired
private ImgServer imgServer;
@PostMapping("/push")
public ResponseData push(@RequestParam("file") MultipartFile file){
return imgServer.push(file);
}
}
实现
@Service
@Slf4j
public class ImgServerImpl implements ImgServer {
@Autowired
FastFileStorageClient fastFileStorageClient;
@Override
public ResponseData<String> push(MultipartFile file) {
if (file.isEmpty()) {
return ResponseData.failureResponse(UploadFileCode.UPLOAD_FILE_CODE_1000);
}
try {
log.info("开始上传 {}", file.getOriginalFilename());
String fileSuffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")+1);
StorePath storePath = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(),fileSuffix, null);
String path = storePath.getFullPath();
log.info("上传成功");
return ResponseData.successResponse(path);
} catch (IOException e) {
log.error(e.toString(), e);
return ResponseData.failureResponse(UploadFileCode.UPLOAD_FILE_CODE_1001.getCode(), e.toString());
}
}
}
测试
网友评论