In the previous article, FastDFS was installed on the server

CentOS FastDFS installation, configure the Tracker and Storage services

This article documents adding FastDFS uploads to the SpringBoot project

1. Add dependencies to POM files

<! -- https://mvnrepository.com/artifact/com.github.tobato/fastdfs-client --> <dependency> < the groupId > com. Making. Tobato < / groupId > < artifactId > fastdfs - client < / artifactId > < version > 1.27.2 < / version > < / dependency >Copy the code

2. Application Adds the FastDFS configuration information

FDFS: # connection timeout time connect-timeout: 30 # read time so-timeout: 30 # tracker Service config address list tracker-list: 192.168.10.235:22122Copy the code

Note: The tracker-list is the IP address of the tracker server, not the IP address of the storage server

3. controller

@Autowired public UploadService uploadService; Public String uploadFace(MultipartFile file) throws Exception {// Obtains the name of the uploaded file. String originalFilename = file.getOriginalFilename(); If (stringutils. isBlank(originalFilename)) {return "File cannot be empty, please select a file and upload again!" ; } String[] fileNameArr = originalFilename.split("\\."); // File suffix = fileNameArr[filenamearr.leng-1]; // Determine whether the suffix conforms to if (! suffix.equalsIgnoreCase("png") && ! suffix.equalsIgnoreCase("jpg") && ! Suffix.equalsignorecase (" JPEG ")) {return "File picture format not supported!" ; } String filePath = uploadService.uploadFastDfs(file, suffix); If (stringutils. isBlank(filePath)) {return "File upload failed!" ; } return filePath; }Copy the code

4. service

Public interface UploadService {/** ** Upload file using FastDFS ** @param file File to be uploaded * @param fileExtName File extension * @return file path * @ioException File upload exception */ public String uploadFastDfs(MultipartFile file, String fileExtName) throws IOException; } @Service public class UploadServiceImpl implements UploadService { @Autowired public FastFileStorageClient fastFileStorageClient; @Override public String uploadFastDfs(MultipartFile file, String fileExtName) throws IOException { StorePath storePath = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(), fileExtName, null); return storePath.getFullPath(); }}Copy the code