这篇文章主要介绍了springboot集成fastDfs过程代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
pom.xml 引入依赖
com.github.tobato fastdfs-client1.26.1-RELEASE
application.properties 配置
# fastDfs配置 fdfs.connect-timeout=600 fdfs.so-timeout=1500 fdfs.trackerList=192.168.1.207:22122 fdfs.thumbImage.height=150 fdfs.thumbImage.width=150 spring.jmx.enabled=false fdfs.pool.max-total=200 storage.resHost=http://192.168.1.207/ storage.resPort=8888
DfsAutoConfig.java 自动注入
@Configuration
@import(FdfsClientConfig.class)
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
public class DfsAutoConfig {
}
DfsResConfig 配置映射关系
@Data
@Component
@ConfigurationProperties("storage")
public class DfsResConfig {
private String resHost;
private String resPort;
}F
FastDfsClientUtil 工具类
@Slf4j
@Component
public class FastDfsClientUtil {
@Autowired
private FastFileStorageClient storageClient;
public ResultData uploadFile(MultipartFile file){
try{
StorePath path = storageClient.uploadFile(file.getInputStream(), file.getSize(),
FilenameUtils.getExtension(file.getOriginalFilename()), null);
return ResultDataUtil.setSuccessResult(path.getFullPath());
}catch (Exception e){
e.printStackTrace();
return ResultDataUtil.setFailedResult();
}
}
public ResultData uploadFile(File file){
try{
FileInputStream inputStream = new FileInputStream(file);
StorePath path = storageClient.uploadFile(inputStream, file.length(),
FilenameUtils.getExtension(file.getName()), null);
return ResultDataUtil.setSuccessResult(path.getFullPath());
}catch (Exception e){
e.printStackTrace();
return ResultDataUtil.setFailedResult();
}
}
public ResultData uploadFileStream(InputStream is, long size, String fileName) {
StorePath path = storageClient.uploadFile(is, size, fileName, null);
return ResultDataUtil.setSuccessResult(path.getFullPath());
}
public String uploadFile(String content, String fileExtension) {
byte[] buff = content.getBytes(Charset.forName("UTF-8"));
ByteArrayInputStream stream = new ByteArrayInputStream(buff);
StorePath path = storageClient.uploadFile(stream, buff.length, fileExtension, null);
return path.getFullPath();
}
public ResultData deleteFile(String fileUrl) {
if (StringUtils.isEmpty(fileUrl)) {
return ResultDataUtil.setFailedResult();
}
try {
StorePath storePath = StorePath.praseFromUrl(fileUrl);
storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
return ResultDataUtil.setSuccessResult();
} catch (FdfsUnsupportStorePathException e) {
e.printStackTrace();
log.warn(e.getMessage());
return ResultDataUtil.setFailedResult();
}
}
//
//
// public String upfileImage(InputStream is, long size, String fileExtName, Set metaData) {
// StorePath path = storageClient.uploadImageAndCrtThumbImage(is, size, fileExtName, metaData);
// return path.getFullPath();
// }
}
测试
@Slf4j
@RestController
@RequestMapping("/dfs")
public class FileDfsController extends baseController {
@Autowired
private FastDfsClientUtil fastDfsClientUtil;
@Autowired
private DfsResConfig dfsResConfig;
@PostMapping("/single")
public ResultData singleUpload(@RequestParam("file") MultipartFile file){
ResultData resultData = fastDfsClientUtil.uploadFile(file);
if (Objects.equals(ResultEnum.SUCCESS.getCode(), resultData.getCode())) {
String url = String.format("%s:%s/%s",dfsResConfig.getResHost(),dfsResConfig.getResPort(),resultData.getData());
return ResultDataUtil.setSuccessResult(url);
}
return resultData;
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。



