springboot集成fastDfs过程代码怎么编写,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
为桂林等地区用户提供了全套网页设计制作服务,及桂林网站建设行业解决方案。主营业务为成都网站设计、成都网站建设、桂林网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!
pom.xml 引入依赖
com.github.tobato fastdfs-client 1.26.1-RELEASE
application.properties 配置
# fastDfs配置fdfs.connect-timeout=600fdfs.so-timeout=1500fdfs.trackerList=192.168.1.207:22122fdfs.thumbImage.height=150fdfs.thumbImage.width=150spring.jmx.enabled=falsefdfs.pool.max-total=200storage.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@Componentpublic class FastDfsClientUtil { @Autowired private FastFileStorageClient storageClient; /** * @Author AlanMa * @Description MultipartFile类型的文件上传ַ * @Date 2019/11/12 * @Param 下载地址 * @return com.hiynn.data.visual.file.vo.ResultData */ 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(); } } /** * @Author AlanMa * @Description 普通的文件上传 * @Date 2019/11/12 * @Param 下载地址 * @return com.hiynn.data.visual.file.vo.ResultData */ 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(); } } /** * @Author AlanMa * @Description 带输入流形式的文件上传 * @Date 2019/11/12 * @Param [is, size, fileName] * @return com.hiynn.data.visual.file.vo.ResultData */ public ResultData uploadFileStream(InputStream is, long size, String fileName) { StorePath path = storageClient.uploadFile(is, size, fileName, null); return ResultDataUtil.setSuccessResult(path.getFullPath()); } /** * @Author AlanMa * @Description 将一段文本文件写到fastdfs的服务器上 * @Date 2019/11/12 * @Param [content, fileExtension] * @return java.lang.String */ 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(); } /** * @Author AlanMa * @Description 删除文件 * @Date 2019/11/12 * @Param [fileUrl] * @return com.hiynn.data.visual.file.vo.ResultData */ 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(); } }//// /**// * @Author AlanMa// * @Description 上传文件图片// * @Date 2019/11/12// * @Param [is, size, fileExtName, metaData]// * @return java.lang.String// */// 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; }}
看完上述内容,你们掌握springboot集成fastDfs过程代码怎么编写的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注创新互联行业资讯频道,感谢各位的阅读!
分享名称:springboot集成fastDfs过程代码怎么编写
网页网址:
http://cdkjz.cn/article/gejjgh.html