1 Environmental preparation
Resources | Remarks |
---|---|
FastDFS deployment environment | If not deployed, refer to the blog post FastDFS installation configuration and testing |
IDEA2021 | Development tools depend on your own preferences |
The FastDFS deployment information I deployed here is:
server information | IP address |
---|---|
tracker server | 192.168.51.5 |
storage server | 192.168.51.6 |
2 pom file configuration
Create a new springboot project and introduce official fastdfs dependencies
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.26.7</version>
</dependency>
2 application file configuration
############################################################
#
# 分布式文件系统 FastDFS 配置
#
###########################################################
fdfs:
connect-timeout: 30 # 连接tracker服务器超时时长
so-timeout: 30 # socket连接超时时长
tracker-list: 192.168.51.5:22122 # tracker服务所在的IP地址和端口号 ( #TrackerList参数,支持多个,我这里只有一个,如果有多个在下方加- x.x.x.x:port)
thumb-image: #缩略图生成参数,可选
height: 150
width: 150
3 Test file upload
3.1 Business layer
FastDFSService
package com.auskat.service;
import org.springframework.web.multipart.MultipartFile;
/**
* 类文件: FastDFSServer
* <p>
* <p>
* 类描述:
* <p>
* 作 者: AusKa_T
* <p>
* 日 期: 2021/3/11 0011
* <p>
* 时 间: 15:01
* <p>
*/
public interface FastDFSServer {
/**
* 上传文件到fastdfs
* @param file 文件流
* @param fileExtName 文件后缀名称
* @return 上传路径
* @throws Exception 异常信息
*/
public String upload(MultipartFile file, String fileExtName) throws Exception;
}
FastDFSServiceImpl
package com.auskat.service.impl;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectResult;
import com.auskat.config.FileResource;
import com.auskat.service.FastDFSService;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.InputStream;
import java.net.URL;
/**
* 类文件: FastDFSServerImpl
* <p>
* <p>
* 类描述:
* <p>
* 作 者: AusKa_T
* <p>
* 日 期: 2021/3/11 0011
* <p>
* 时 间: 15:02
* <p>
*/
@Service
public class FastDFSServiceImpl implements FastDFSService {
@Autowired
FastFileStorageClient fastFileStorageClient;
/**
* 上传文件到fastdfs
* @param file 文件流
* @param fileExtName 文件后缀名称
* @return 上传路径
* @throws Exception 异常信息
*/
@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;
}
}
3.2 Control layer
FastDFSController
package com.auskat.controller;
import com.auskat.service.FastDFSService;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
/**
* 类文件: FastDFSController
* <p>
* <p>
* 类描述:
* <p>
* 作 者: AusKa_T
* <p>
* 日 期: 2020/12/7 0007
* <p>
* 时 间: 14:46
* <p>
*/
@RestController
@RequestMapping("fdfs")
public class FastDFSController {
@Autowired
FastDFSService fastDFSService;
/**
* 上传文件到FastDFS
* @param file 文件流
* @param request 请求
* @param response 响应
* @return 结果
* @throws Exception 异常信息
*/
@PostMapping("uploadFile")
public String uploadFace(
@RequestParam MultipartFile file,
HttpServletRequest request, HttpServletResponse response) throws Exception {
String path = "";
// 开始文件上传
if (file != null) {
// 获取文件上传的文件名称
String fileName = file.getOriginalFilename();
if (StringUtils.isNotBlank(fileName)) {
String[] fileNameArr = fileName.split("\\.");
// 获取文件的后缀名
String suffix = fileNameArr[fileNameArr.length - 1];
path = fastDFSService.upload(file, suffix);
System.out.println(path);
}
return "上传成功!";
}else {
return "文件不能为空! ";
}
}
4 Related information
- The blog post is not easy, everyone who has worked so hard to pay attention and praise, thank you