赞
踩
1. 引入 redis 依赖包
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.26.7</version>
</dependency>
2.yml文件中配置fastdfs
##############################################################
#
#fastdfs的配置
#
fdfs:
connect-timeout: 30 #连接超时实际
so-timeout: 30 #读取超时实际
tracker-list: 192.168.1.6:22122 #tracker服务所在的ip地址和端口号
3.实现springboot客户端代码实现
(1)创建service接口
import org.springframework.web.multipart.MultipartFile;
public interface FdfsService {
public String upload(MultipartFile file, String fileExtName) throws Exception;
}
(2)创建service接口实现类
import com.github.tobato.fastdfs.domain.fdfs.StorePath; import com.github.tobato.fastdfs.service.FastFileStorageClient; import com.imooc.service.FdfsService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; @Service public class FdfsServiceImpl implements FdfsService { @Autowired private FastFileStorageClient fastFileStorageClient; @Override public String upload(MultipartFile file, String fileExtName) throws Exception { StorePath storePath = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(), fileExtName, null); String fullPath = storePath.getFullPath(); return fullPath; } }
(3)创建controller类
import com.imooc.service.FdfsService; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.HashMap; import java.util.Map; @RestController @RequestMapping("fdfs") public class CenterUserController{ @Autowired private FdfsService fdfsService; @PostMapping("/uploadFace") public Object uploadFace( @RequestParam String userId, MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws Exception { Map<String, Object> result = new HashMap<>(); // 开始文件上传 if (file != null) { //1.获取文件名 String fileName = file.getOriginalFilename(); //2.判断文件名 if (StringUtils.isNotBlank(fileName)) { //3重组文件名 String fileNameArr[] = fileName.split("\\."); // 获取文件的后缀名 String suffix = fileNameArr[fileNameArr.length - 1]; if (!suffix.equalsIgnoreCase("png") && !suffix.equalsIgnoreCase("jpg") && !suffix.equalsIgnoreCase("jpeg") ) { return result.put("msg","图片格式不正确!"); } String fullPath = fdfsService.upload(file, suffix); System.out.println(fullPath); }else { return result.put("msg","文件不能为空"); } } return result.put("msg","success"); } }
(4)测试
a.需要写一段html来上传图片,这里就不展示了。
b.上传成功后,会在控制台输出完整路径:test/M00/00/00/wKgBB18v1a6AB1gpAAFkK4jbFUs729.jpg
c.浏览器中访问:http://192.168.1.7:8888/test/M00/00/00/wKgBB18v1a6AB1gpAAFkK4jbFUs729.jpg
界面显示刚刚上传的图片。
FastDFS仅仅适用于简单的,功能单一的分布式文件系统。对于功能复杂,数据多变的文件系统而言,FastDFS不仅运维成本高,并且开发复杂度也非常高。所以下面会介绍第三方云存储解决方案。
具体方案请参见:https://www.aliyun.com/product/oss?spm=5176.12825654.eofdhaal5.13.3dbd2c4aWzD2T9
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。