spring boot 2.0 与FASTDFS进行整合
只支持在spring-boot 2.0以及以上版本中使用
1.pom.xml 里引入FASTDFS的依赖,toobato与fastdfs作者一起,将fastdfs的功能进行了重构与简化
<!-- 高性能分布式文件服务器 -->
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.26.2</version>
</dependency>
2.引入spring-test的依赖,里面有将可以将文件对象转换为MutipartFile的方法
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
3.创建一个FastdfsImporter 类(随便起名),在类里面1定义如下配置,就可以将作者的fastDFS中的bean进行引入
@Configuration @Import(FdfsClientConfig.class)//@Import可以导入一个带有@configuration注解的类; // 解决jmx重复注册bean的问题 @EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING) public class FastdfsImporter { // 导入依赖组件 }
4.spring boot 配置文件中引入如下
fdfs.soTimeout=1501
fdfs.connectTimeout=601
#image size ,在方法中可以使用生成自定义的图片大小
fdfs.thumbImage.width=80
fdfs.thumbImage.height=80
# fastdfs address and port
fdfs.trackerList[0]=192.168.241.129:22122
5.接下来,提供给两个类,直接复制到自己的程序中,进行调用即可
----------------FastDFSClient----------------------直接用此类方法就可以将文件传入fastdfs图片服务器中
package com.project.util;import java.io.ByteArrayInputStream; import java.io.IOException; import java.nio.charset.Charset;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 com.github.tobato.fastdfs.domain.StorePath; import com.github.tobato.fastdfs.exception.FdfsUnsupportStorePathException; import com.github.tobato.fastdfs.service.FastFileStorageClient;@Component public class FastDFSClient {@Autowired private FastFileStorageClient storageClient;// @Autowired // private AppConfig appConfig; // 项目参数配置/** * 上传文件 * * @param file * 文件对象 * @return 文件访问地址 * @throws IOException */ public String uploadFile(MultipartFile file) throws IOException { StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(), FilenameUtils.getExtension(file.getOriginalFilename()), null);return storePath.getPath(); }public String uploadFile2(MultipartFile file) throws IOException { StorePath storePath = storageClient.uploadImageAndCrtThumbImage(file.getInputStream(), file.getSize(), FilenameUtils.getExtension(file.getOriginalFilename()), null);return storePath.getPath(); }public String uploadQRCode(MultipartFile file) throws IOException { StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(), "png", null);return storePath.getPath(); }public String uploadFace(MultipartFile file) throws IOException { StorePath storePath = storageClient.uploadImageAndCrtThumbImage(file.getInputStream(), file.getSize(), "png", null);return storePath.getPath(); }public String uploadBase64(MultipartFile file) throws IOException { StorePath storePath = storageClient.uploadImageAndCrtThumbImage(file.getInputStream(), file.getSize(), "png", null);return storePath.getPath(); }/** * 将一段字符串生成一个文件上传 * * @param content * 文件内容 * @param fileExtension * @return */ 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 storePath.getPath(); }// 封装图片完整URL地址 // private String getResAccessUrl(StorePath storePath) { // String fileUrl = AppConstants.HTTP_PRODOCOL + appConfig.getResHost() + ":" + appConfig.getFdfsStoragePort() // + "/" + storePath.getFullPath(); // return fileUrl; // }/** * 删除文件 * * @param fileUrl * 文件访问地址 * @return */ public void deleteFile(String fileUrl) { if (StringUtils.isEmpty(fileUrl)) { return; } try { StorePath storePath = StorePath.praseFromUrl(fileUrl); storageClient.deleteFile(storePath.getGroup(), storePath.getPath()); } catch (FdfsUnsupportStorePathException e) { e.getMessage(); } } }
------------------ FileUtils----------提供了对文件操作,可以将file 对象转换为MutipartFile,base64字符串转换为文件(根据需要引入此类)
package com.project.util;import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL;import org.springframework.mock.web.MockMultipartFile; import org.springframework.stereotype.Service; import org.springframework.util.Base64Utils; import org.springframework.web.multipart.MultipartFile;@Service public class FileUtils { /** * 根据url拿取file * * @param url * @param suffix * 文件后缀名 */ public static File createFileByUrl(String url, String suffix) { byte[] byteFile = getImageFromNetByUrl(url); if (byteFile != null) { File file = getFileFromBytes(byteFile, suffix); return file; } else { return null; } }/** * 根据地址获得数据的字节流 * * @param strUrl * 网络连接地址 * @return */ private static byte[] getImageFromNetByUrl(String strUrl) { try { URL url = new URL(strUrl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5 * 1000); InputStream inStream = conn.getInputStream();// 通过输入流获取图片数据 byte[] btImg = readInputStream(inStream);// 得到图片的二进制数据 return btImg; } catch (Exception e) { e.printStackTrace(); } return null; }/** * 从输入流中获取数据 * * @param inStream * 输入流 * @return * @throws Exception */ private static byte[] readInputStream(InputStream inStream) throws Exception { ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while ((len = inStream.read(buffer)) != -1) { outStream.write(buffer, 0, len); } inStream.close(); return outStream.toByteArray(); }// 创建临时文件 private static File getFileFromBytes(byte[] b, String suffix) { BufferedOutputStream stream = null; File file = null; try { file = File.createTempFile("pattern", "." + suffix); System.out.println("临时文件位置:" + file.getCanonicalPath()); FileOutputStream fstream = new FileOutputStream(file); stream = new BufferedOutputStream(fstream); stream.write(b); } catch (Exception e) { e.printStackTrace(); } finally { if (stream != null) { try { stream.close(); } catch (IOException e) { e.printStackTrace(); } } } return file; }public static MultipartFile createImg(String url) { try { // File转换成MutipartFile File file = FileUtils.createFileByUrl(url, "jpg"); FileInputStream inputStream = new FileInputStream(file); MultipartFile multipartFile = new MockMultipartFile(file.getName(), inputStream); return multipartFile; } catch (IOException e) { e.printStackTrace(); return null; } }/使用spring-test/ public static MultipartFile fileToMultipart(String filePath) { try { // File转换成MutipartFile File file = new File(filePath); FileInputStream inputStream = new FileInputStream(file); MultipartFile multipartFile = new MockMultipartFile(file.getName(), "png", "image/png", inputStream); return multipartFile; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } }public static void main(String[] args) { // WebFileUtils.createFileByUrl("http://122.152.205.72:88/group1/M00/00/01/CpoxxFr7oIaAZ0rOAAC0d3GKDio580.png", // "png"); // WebFileUtils.createImg("http://122.152.205.72:88/group1/M00/00/01/CpoxxFr7oIaAZ0rOAAC0d3GKDio580.png"); }public static boolean base64ToFile(String filePath, String base64Data) throws Exception { String dataPrix = ""; String data = "";if(base64Data == null || "".equals(base64Data)){ return false; }else{ String [] d = base64Data.split("base64,"); if(d != null && d.length == 2){ dataPrix = d[0]; data = d[1]; }else{ return false; } }// 因为BASE64Decoder的jar问题,此处使用spring框架提供的工具包 byte[] bs = Base64Utils.decodeFromString(data); // 使用apache提供的工具类操作流 org.apache.commons.io.FileUtils.writeByteArrayToFile(new File(filePath), bs);return true; } }
转载于:https://www.cnblogs.com/iscys/p/9500892.html
spring boot 2.0 与FASTDFS进行整合相关推荐
- Elasticsearch-06 Spring Boot 2.0.9整合ElasticSearch5.6.16
文章目录 概述 官方JAVA API文档 工程 pom.xml es配置文件 Es配置类 控制层 简单查询 新增数据 删除数据 更新数据 复合查询 其他 新建索引 删除索引 判断index中某个typ ...
- spring boot 2.0.3.RELEASE 整合 shiro bug 记录
spring boot 2.0.3.RELEASE 和 shiro 结合的 bug 纪要: 1.shiro 的过滤器 会出现在所有的过滤链中,尽管该请求不包含在shiro过滤规则中,尽管不会进入shi ...
- 【译】Spring Boot 2.0 官方迁移指南
前提 希望本文档将帮助您把应用程序迁移到 Spring Boot 2.0. 在你开始之前 首先,Spring Boot 2.0 需要 Java 8 或更高版本.不再支持 Java 6 和 7 了. 在 ...
- Spring Boot 2.0正式发布,升还是不升呢?
Spring帝国 Spring几乎是每一位Java开发人员都耳熟能详的开发框架,不论您是一名初出茅庐的程序员还是经验丰富的老司机,都会对其有一定的了解或使用经验.在现代企业级应用架构中,Spring技 ...
- SpringBoot2.0(一):【重磅】Spring Boot 2.0权威发布
就在昨天Spring Boot2.0.0.RELEASE正式发布,今天早上在发布Spring Boot2.0的时候还出现一个小插曲,将Spring Boot2.0同步到Maven仓库的时候出现了错误, ...
- Spring Boot 2.0 迁移指南
点击上方"朱小厮的博客",选择"设为星标" 回复"666"获取新整理的1000+GB资料 前提 本文档将帮助您把应用程序迁移到 Spring ...
- (转)Spring Boot 2(一):【重磅】Spring Boot 2.0权威发布
http://www.ityouknow.com/springboot/2018/03/01/spring-boot-2.0.html 就在今天Spring Boot2.0.0.RELEASE正式发布 ...
- Spring Cloud F Spring Boot 2.0 版本升级说明书
Spring Boot 2.0 需要 Java 8 或更高版本.不再支持 Java 6 和 7 了 在 Spring Boot 2.0 中,许多配置属性被重新命名/删除,开发人员需要更新 依赖版本 以 ...
- 聊聊 Spring Boot 2.0 的 WebFlux
https://zhuanlan.zhihu.com/p/30813274 首发于极乐科技 写文章登录 聊聊 Spring Boot 2.0 的 WebFlux 泥瓦匠BYSocket 4 个月前 聊 ...
最新文章
- 任务栏窗口和状态图标的闪动 z
- 颠覆认知:SRE 到底是干啥的?
- 简简单单的正则表单验证练习
- 上传文件到服务器并显示,J2EE如何实现Servlet上传文件到服务器并相应显示功能...
- php module类,总结php artisan module常用命令
- http协议与php关系,PHP中的HTTP协议
- 传输设备,光端机的应用及故障分析
- 尚学堂科技_马士兵_设计模式
- Spring Cloud Config 规范 1
- Mac OSX使用隐藏文件夹
- HDFS的读写限流方案
- 易门一中2021年高考成绩查询,附属易门中学2021届高二年级教师参加“中国高考评价体系下的2021年一轮备考策略”直播会...
- leetcode121、122、123
- easyswoole验证码的使用
- UWA学堂|开发流程模块
- java新闻发布系统源代码_Java新闻发布系统源代码
- Python干货:破解40大机器学习面试题(包含初中高级)
- 比较火的NFT数字艺术品交易平台
- SAST、DAST、IAST几种测试工具的比较
- Python Matplotlib绘图的正确打开方式
热门文章
- 计算机基础知识第三章测试,计计算机应用基础第三章测试题
- c语言函数可变长参数,一种使用变长参数为C程序构造灵活回调函数的方法
- Ant的使用 - 简单介绍
- UML类图(Class Diagram)中类与类之间的关系及表示方式
- latex转为html效果好吗,latex2html
- C++ 偏微分数值计算库_ESYSim仿真器介绍之一 C++库介绍
- 系统学习NLP(二十三)--浅谈Attention机制的理解
- finalshell文件列表不显示_软网推荐:文件变动我知晓
- android 连续调用方法是,android – SwitchPreferences多次调用onPreferenceChange()方法
- php下载功能,js php实现无刷新下载功能