创建FastDFS文件夹并上传软件
安装所需要的环境依赖
解压编译安装libfastcommon
安装FastDFS本体
将conf下的所有配置文件拷贝到/etc/fdfs下
以上是安装的是在node1机器上(tracker), 在node2机器(storage)重复上面的操作.
修改tracker配置文件
创建base_path所制定的文件夹
启动tracker服务
配置storage
在node2下
启动storage
上传文件测试
在/home下放一张imooc.png图片,然后执行
如果看到下面图片的信息表示上传成功了
安装fastdfs-nginx模块
在node2节点
把/usr/local/include的local去除
安装Nginx
./configure
–prefix=/usr/local/nginx
–pid-path=/var/run/nginx/nginx.pid
–lock-path=/var/lock/nginx.lock
–error-log-path=/var/log/nginx/error.log
–http-log-path=/var/log/nginx/access.log
–with-http_gzip_static_module
–http-client-body-temp-path=/var/temp/nginx/client
–http-proxy-temp-path=/var/temp/nginx/proxy
–http-fastcgi-temp-path=/var/temp/nginx/fastcgi
–http-uwsgi-temp-path=/var/temp/nginx/uwsgi
–http-scgi-temp-path=/var/temp/nginx/scgi
–add-module=/usr/local/src/FastDFS/fastdfs-nginx-module-1.22/src
启动Nginx
此时 访问http://192.168.174.129:8888/imooc/M00/00/00/wKiugWLNDyKALsnEAAucnpyATjI502_big.png
就能访问到之前上传的imooc.png图片了
整合SpringBoot项目实战
controller
@RestController @RequestMapping("fdfs") public class CenterUserController extends BaseController { @Autowired private FileResource fileResource; @Autowired private CenterUserService centerUserService; @Autowired private FdfsService fdfsService; @PostMapping("uploadFace") public IMOOCJSONResult uploadFace( String userId, MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws Exception { String path = ""; // 开始文件上传 if (file != null) { // 获得文件上传的文件名称 String fileName = file.getOriginalFilename(); if (StringUtils.isNotBlank(fileName)) { // 文件重命名 imooc-face.png -> ["imooc-face", "png"] String fileNameArr[] = fileName.split("\\."); // 获取文件的后缀名 String suffix = fileNameArr[fileNameArr.length - 1]; if (!suffix.equalsIgnoreCase("png") && !suffix.equalsIgnoreCase("jpg") && !suffix.equalsIgnoreCase("jpeg") ) { return IMOOCJSONResult.errorMsg("图片格式不正确!"); } path = fdfsService.upload(file, suffix); // 使用FastDFS // path = fdfsService.uploadOSS(file, userId, suffix); // 使用阿里云存储OSS System.out.println(path); } } else { return IMOOCJSONResult.errorMsg("文件不能为空!"); } if (StringUtils.isNotBlank(path)) { String finalUserFaceUrl = fileResource.getHost() + path; // 使用FastDFS // String finalUserFaceUrl = fileResource.getOssHost() + path; // 使用阿里云存储OSS // 将图片的URL存储到user表中 Users userResult = centerUserService.updateUserFace(userId, finalUserFaceUrl); UsersVO usersVO = conventUsersVO(userResult); CookieUtils.setCookie(request, response, "user", JsonUtils.objectToJson(usersVO), true); } else { return IMOOCJSONResult.errorMsg("上传头像失败"); } return IMOOCJSONResult.ok(); } }
FileResource
@Component @PropertySource("classpath:file.properties") @ConfigurationProperties(prefix = "file") public class FileResource { private String host; private String endpoint; private String accessKeyId; private String accessKeySecret; private String bucketName; private String objectName; private String ossHost; public String getOssHost() { return ossHost; } public void setOssHost(String ossHost) { this.ossHost = ossHost; } public String getHost() { return host; } public void setHost(String host) { this.host = host; } public String getEndpoint() { return endpoint; } public void setEndpoint(String endpoint) { this.endpoint = endpoint; } public String getAccessKeyId() { return accessKeyId; } public void setAccessKeyId(String accessKeyId) { this.accessKeyId = accessKeyId; } public String getAccessKeySecret() { return accessKeySecret; } public void setAccessKeySecret(String accessKeySecret) { this.accessKeySecret = accessKeySecret; } public String getBucketName() { return bucketName; } public void setBucketName(String bucketName) { this.bucketName = bucketName; } public String getObjectName() { return objectName; } public void setObjectName(String objectName) { this.objectName = objectName; } }
file.properties
centerUserService.updateUserFace
FdfsServiceImpl
import com.aliyun.oss.OSS; import com.aliyun.oss.OSSClientBuilder; import com.github.tobato.fastdfs.domain.fdfs.StorePath; import com.github.tobato.fastdfs.service.FastFileStorageClient; import com.imooc.resourse.FileResource; import com.imooc.service.FdfsService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import java.io.InputStream; @Service public class FdfsServiceImpl implements FdfsService { @Autowired private FastFileStorageClient fastFileStorageClient; @Autowired private FileResource fileResource; @Override public String upload(MultipartFile file, String fileExtName) throws Exception { StorePath storePath = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(), fileExtName, null); String path = storePath.getFullPath(); return path; } @Override public String uploadOSS(MultipartFile file, String userId, String fileExtName) throws Exception { // 构建ossClient OSS ossClient = new OSSClientBuilder() .build(fileResource.getEndpoint(), fileResource.getAccessKeyId(), fileResource.getAccessKeySecret()); InputStream inputStream = file.getInputStream(); // 文件上传路径构建 String myObjectName = fileResource.getObjectName() + "/" + userId + "/" + userId + "." + fileExtName; ossClient.putObject(fileResource.getBucketName(), myObjectName, inputStream); ossClient.shutdown(); return myObjectName; } }
效果测试