您好,登錄后才能下訂單哦!
springboot集成fastDfs過程代碼怎么編寫,相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。
pom.xml 引入依賴
<dependency> <groupId>com.github.tobato</groupId> <artifactId>fastdfs-client</artifactId> <version>1.26.1-RELEASE</version></dependency>
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
<em id="__mceDel">FastDfsClientUtil 工具類<br></em>
@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<java.lang.String> */ public ResultData<String> 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<java.lang.String> */ public ResultData<String> 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<java.lang.String> */ public ResultData<String> 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<MateData> 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<String> 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過程代碼怎么編寫的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。