SpringBoot集成FastDFS依赖如何实现文件上传
这篇文章主要介绍SpringBoot集成FastDFS依赖如何实现文件上传,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
1、引入依赖
简单说一下这个依赖部分,目前大部分都是采用的如下依赖:
<!-- https://mvnrepository.com/artifact/net.oschina.zcx7878/fastdfs-client-java --><dependency> <groupId>net.oschina.zcx7878</groupId> <artifactId>fastdfs-client-java</artifactId> <version>1.27.0.0</version></dependency>
本着不重复造轮子,且为了使用方便我们可以去GitHub找一个集成好的依赖:
https://github.com/tobato/FastDFS_Client
<dependency> <groupId>com.github.tobato</groupId> <artifactId>fastdfs-client</artifactId> <version>1.27.2</version></dependency>
2、将Fdfs配置引入项目
只需要创建一个配置类就可以了:
@Configuration@Import(FdfsClientConfig.class)@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)public class ComponetImport { // 导入依赖组件}
参考截图:
3、在application.yml当中配置Fdfs相关参数
根据自己情况修改相应ip地址及端口号:
server: port: 8080ip: 10.211.55.4 # 根据自己FastDFS服务器修改fdfs: so-timeout: 1501 connect-timeout: 601 thumb-image: #缩略图生成参数 width: 150 height: 150 tracker-list: #TrackerList参数,支持多个 - 10.211.55.4:22122 web-server-url: http://${ip}:8888/
4、client封装工具类
创建FastDFSClient.java包装工具类,方便后面使用:
import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;import com.github.tobato.fastdfs.domain.fdfs.StorePath;import com.github.tobato.fastdfs.domain.proto.storage.DownloadByteArray;import com.github.tobato.fastdfs.exception.FdfsUnsupportStorePathException;import com.github.tobato.fastdfs.service.FastFileStorageClient;import org.apache.commons.io.FilenameUtils;import org.apache.commons.lang3.StringUtils;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import org.springframework.web.multipart.MultipartFile;import java.io.ByteArrayInputStream;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.nio.charset.Charset;@Componentpublic class FastDFSClient { @Autowired private FastFileStorageClient storageClient; @Autowired private FdfsWebServer fdfsWebServer; public String uploadFile(MultipartFile file) throws IOException { StorePath storePath = storageClient.uploadFile(file.getInputStream(),file.getSize(), FilenameUtils.getExtension(file.getOriginalFilename()),null); return getResAccessUrl(storePath); } public String uploadFile(File file) throws IOException { FileInputStream inputStream = new FileInputStream (file); StorePath storePath = storageClient.uploadFile(inputStream,file.length(), FilenameUtils.getExtension(file.getName()),null); return getResAccessUrl(storePath); } public String uploadFile(String content, String fileExtension) { byte[] buff = content.getBytes(Charset.forName("UTF-8")); ByteArrayInputStream stream = new ByteArrayInputStream(buff); StorePath storePath = storageClient.uploadFile(stream,buff.length, fileExtension,null); return getResAccessUrl(storePath); } private String getResAccessUrl(StorePath storePath) { String fileUrl = fdfsWebServer.getWebServerUrl() + storePath.getFullPath(); return fileUrl; } public void deleteFile(String fileUrl) { if (StringUtils.isEmpty(fileUrl)) { return; } try { StorePath storePath = StorePath.parseFromUrl(fileUrl); storageClient.deleteFile(storePath.getGroup(), storePath.getPath()); } catch (FdfsUnsupportStorePathException e) { System.out.println(e.getMessage()); } } public byte[] downloadFile(String fileUrl) throws IOException { String group = fileUrl.substring(0, fileUrl.indexOf("/")); String path = fileUrl.substring(fileUrl.indexOf("/") + 1); DownloadByteArray downloadByteArray = new DownloadByteArray(); byte[] bytes = storageClient.downloadFile(group, path, downloadByteArray); return bytes; }}
5、创建Conttoler测试类
5.1 文件上传测试
@RestController@RequestMapping("/file")public class FileUploadController { @Autowired private FastDFSClient fastDFSClient; @RequestMapping("/upload") public String uploadFile(MultipartFile file) throws IOException { return fastDFSClient.uploadFile(file); }}
执行效果截图:
5.2、下载文件测试
@RestController@RequestMapping("/file")public class FileUploadController { @Autowired private FastDFSClient fastDFSClient; @RequestMapping("/download") public void downloadFile(String fileUrl, HttpServletResponse response) throws IOException { byte[] bytes = fastDFSClient.downloadFile(fileUrl); response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode("sb.xlsx", "UTF-8")); response.setCharacterEncoding("UTF-8"); ServletOutputStream outputStream = null; try { outputStream = response.getOutputStream(); outputStream.write(bytes); } catch (IOException e) { e.printStackTrace(); } finally { try { outputStream.flush(); outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } }}
测试下载路径:
http://127.0.0.1:8080/file/download?fileUrl=group1/M00/00/00/CtM3BF84r4SAEPDgAABoGL78QcY682.jpg
拼接的参数为:group1/M00/00/00/CtM3BF84r4SAEPDgAABoGL78QcY682.jpg
大家想修改路径的话,需要同步修改 downloadFile() 方法里的分隔方式。
springboot是什么
springboot一种全新的编程规范,其设计目的是用来简化新Spring应用的初始搭建以及开发过程,SpringBoot也是一个服务于框架的框架,服务范围是简化配置文件。
以上是“SpringBoot集成FastDFS依赖如何实现文件上传”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注编程网行业资讯频道!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341