Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write springboot Integrated fastDfs process Code

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Springboot integration fastDfs process code how to write, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

Pom.xml introduces dependency

Com.github.tobato fastdfs-client 1.26.1-RELEASE

Application.properties configuration

# fastDfs configure 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 automatic injection

@ Configuration@Import (FdfsClientConfig.class) @ EnableMBeanExport (registration = RegistrationPolicy.IGNORE_EXISTING) public class DfsAutoConfig {}

DfsResConfig configuration mapping relationship

@ Data@Component@ConfigurationProperties ("storage") public class DfsResConfig {private String resHost; private String resPort;} F

FastDfsClientUtil utility class

@ Slf4j@Componentpublic class FastDfsClientUtil {@ Autowired private FastFileStorageClient storageClient; / * * @ Author AlanMa * @ Description MultipartFile type file upload * @ Date 2019-11-12 * @ Param download address * @ 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 ordinary file upload * @ Date 2019-11-12 * @ Param download address * @ 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 upload files with input stream * @ 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 write a text file to fastdfs server * @ 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 Delete the file * @ 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 upload file picture / / * @ Date 2019Accord 1112Accord / * @ 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 (); / /}}

test

@ 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;}}

After reading the above, have you mastered the method of how to write springboot integrated fastDfs process code? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report