Integrating FastDFS with Spring Boot for File Storage
Dependency Configuration
Add the FastDFS client dependency to your project:
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.26.2</version>
</dependency>
Client Configuration Class
Create a configuration class to enable FastDFS client functionality:
package com.example.config;
import com.github.tobato.fastdfs.FdfsClientConfig;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableMBeanExport;
import org.springframework.context.annotation.Import;
import org.springframework.jmx.support.RegistrationPolicy;
@Configuration
@Import(FdfsClientConfig.class)
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
public class FastDfsConfig {
}
Application Properties Configuration
Configure FastDFS connection settings in your application.yml:
fdfs:
connect-timeout: 1500
so-timeout: 3000
thumb-image:
height: 60
width: 60
tracker-list:
- 192.168.1.100:22122
app:
upload:
base-url: http://files.example.com/
allowed-types:
- image/jpeg
- image/png
- image/jpg
Upload Properties Configuration
Create a propertiees class to manage upload configurations:
package com.example.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import java.util.List;
@Data
@ConfigurationProperties(prefix = "app.upload")
public class UploadConfig {
private String baseUrl;
private List<String> allowedTypes;
}
File Upload Service Implementation
Implement the file upload service using FastDFS client:
package com.example.service;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import com.example.config.UploadConfig;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
@EnableConfigurationProperties(UploadConfig.class)
@Service
@Slf4j
public class FileUploadService {
@Autowired
private FastFileStorageClient storageClient;
@Autowired
private UploadConfig uploadConfig;
public String uploadFile(MultipartFile file) {
try {
// Validate file type
String contentType = file.getContentType();
if (!uploadConfig.getAllowedTypes().contains(contentType)) {
throw new RuntimeException("Unsupported file type");
}
// Validate file content
BufferedImage image = ImageIO.read(file.getInputStream());
if (image == null) {
log.error("Invalid file content");
throw new RuntimeException("Invalid file content");
}
// Extract file extension
String extension = StringUtils.substringAfterLast(
file.getOriginalFilename(), ".");
// Upload to FastDFS
StorePath storePath = storageClient.uploadFile(
file.getInputStream(),
file.getSize(),
extension,
null
);
return uploadConfig.getBaseUrl() + storePath.getFullPath();
} catch (IOException e) {
log.error("File upload failed", e);
throw new RuntimeException("Upload failed");
}
}
}
The service validates file types, checks image content, extracts file extensions, and uploads files to FastDFS storage, returning the complete file URL for access.