「Spring」SpringBoot 整合 FastDFS Posted on 2020-08-02 | In Spring 1. 在maven项目pom.xml中添加依赖12345<dependency> <groupId>com.github.tobato</groupId> <artifactId>fastdfs-client</artifactId> <version>1.27.2</version></dependency> 2. 在application.yml中添加配置123456789#FDFS配置fdfs: so-timeout: 5000 #上传的超时时间 connect-timeout: 2000 #连接超时时间 thumb-image: #缩略图生成参数 width: 150 height: 150 tracker-list: #TrackerList参数,支持多个 - 192.168.2.100:22122 3. 编写FastDFS工具类123456789101112131415161718192021222324252627@Componentpublic class FastDfsUtil { private static ThumbImageConfig thumbImageConfig; private static FastFileStorageClient fastFileStorageClient; private static FdfsWebServer fdfsWebServer; public FastDfsUtil(ThumbImageConfig thumbImageConfig, FastFileStorageClient fastFileStorageClient, FdfsWebServer fdfsWebServer) { FastDfsUtil.thumbImageConfig = thumbImageConfig; FastDfsUtil.fastFileStorageClient = fastFileStorageClient; FastDfsUtil.fdfsWebServer = fdfsWebServer; } /** * @param multipartFile 文件对象 * @return 返回文件地址 * @author qbanxiaoli * @description 上传文件 */ @SneakyThrows public static String uploadFile(MultipartFile multipartFile) { StorePath storePath = fastFileStorageClient.uploadFile(multipartFile.getInputStream(), multipartFile.getSize(), FilenameUtils.getExtension(multipartFile.getOriginalFilename()), null); return storePath.getFullPath(); } // 其他代码...} 4. 编写测试Controller12345678910111213141516171819202122232425262728293031@Slf4j@RestControllerpublic class FileController { /** * 上传图片并保存 * @param file * @param request * @return * @throws IOException */ @PostMapping("/upload") public Map<String, Object> uploadFile(MultipartFile[] file, HttpServletRequest request) throws IOException { Map<String, Object> map = new HashMap<String, Object>(); if (file != null && file.length > 0) { String saveFile = null; for (int i = 0; i < file.length; i++) { MultipartFile partFile = file[i]; // 保存文件 saveFile = FastDfsUtil.uploadFile(partFile);; saveFile = "http://192.168.2.100/" + saveFile; } map.put("data", saveFile); map.put("msg", "上传成功"); log.info(">>>>>>>>>>>>>>>>图片上传成功:" + saveFile); } else { map.put("msg", "上传失败"); log.info(">>>>>>>>>>>>>>>>图片上传失败"); } return map; }} 5. 编写前端页面代码1234567891011121314151617181920212223242526272829303132333435363738394041424344454647<body> <div> 图片上传: <input type="file" name="file" value="" id="input-file" accept="image/png,image/jpeg,image/gif,image/jpg"> </div> <div> 图片回显: <img id="upload-file" src="" alt="" width="300"> </div><script charset="utf-8" type="text/javascript" src="/lib/jquery-3.5.1.min.js"></script><script charset="utf-8" type="text/javascript">$('#input-file').on("change", function() { var file = this.files[0]; var formData = new FormData(); formData.append('file', file); // 开始上传 fileUpload("/upload", formData); // 测试读取图片(本地显示) var fileReader = new FileReader(); fileReader.readAsDataURL(file); fileReader.onloadend = function(fEvent){ var src = fEvent.target.result; console.log(src); $("#upload-file").attr("src", src); }});function fileUpload(url, formData){ $.ajax({ url: url, type: 'POST', cache: false, data: formData, processData: false, contentType: false, dataType: "json", success: function (res) { console.log(res); }, error: function (xhr, type, errorThrown) { console.log("照片上传失败") } });}</script> 6. 浏览器访问页面测试 demo源码地址:https://gitee.com/chaoo/fastdfs-demo.git