Family! , In the last article, we talked about how to use docker-compose to quickly deploy fastdfs. In today’s article, I will introduce how to seamlessly integrate Spring Boot with FastDFS to efficiently manage and operate file storage. Through this integration, you will be able to easily implement functions such as file upload and download in Spring Boot applications. let’s start
Add FastDFS dependency
First, we need to add the FastDFS dependency to the pom.xml file of the Spring Boot project. In this example, we use com.github.tobato.fastdfs:fastdfs-client as a dependency of the FastDFS client. Please make sure you have added the following dependencies in the pom.xml file:
<!-- fastdfs --> <dependency> <groupId>com.github.tobato</groupId> <artifactId>fastdfs-client</artifactId> <version>1.27.2</version> </dependency>
Configure FastDFS connection information
Next, we need to add the FastDFS connection information to the configuration file of the Spring Boot project. Add the following configuration to the application.yml file:
fdfs: outurl: http://192.168.10.106:8088/ # read time soTimeout: 1500 # Connection timeout connectTimeout: 691 # Thumbnails thumbImage: # Width width: 150 # high height: 150 # tracker list trackerList: - 192.168.10.106:22122
Implement FastDFS file operation service
code show as below:
FastDFSClientWrapper
import com.github.tobato.fastdfs.domain.fdfs.StorePath; import com.github.tobato.fastdfs.domain.proto.storage.DownloadByteArray; import com.github.tobato.fastdfs.service.FastFileStorageClient; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import javax.annotation.Resource; import java.io.ByteArrayInputStream; import java.io.IOException; @Component public class FastDFSClientWrapper {<!-- --> private final Logger logger = LoggerFactory. getLogger(FastDFSClientWrapper. class); @Resource private FastFileStorageClient fastFileStorageClient; /** * File Upload * * @param bytes file bytes * @param fileSize file size * @param extension file extension * @return fastDfs path */ public String uploadFile(byte[] bytes, long fileSize, String extension) {<!-- --> ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes); StorePath storePath = fastFileStorageClient.uploadFile(byteArrayInputStream, fileSize, extension, null); System.out.println(storePath.getGroup() + "===" + storePath.getPath() + "======" + storePath.getFullPath()); return storePath. getFullPath(); } /** * download file * * @param fileUrl file URL * @return file bytes * @throws IOException */ public byte[] downloadFile(String fileUrl) throws IOException {<!-- --> String group = fileUrl. substring(0, fileUrl. indexOf("/")); String path = fileUrl. substring(fileUrl. indexOf("/") + 1); DownloadByteArray downloadByteArray = new DownloadByteArray(); byte[] bytes = fastFileStorageClient.downloadFile(group, path, downloadByteArray); return bytes; } /** * Delete Files * * @param fileUrl file URL * @return file bytes * @throws IOException */ public void deleteFile(String fileUrl) throws IOException {<!-- --> fastFileStorageClient.deleteFile( fileUrl); } }
FastdfsConfig
import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Data @Component @ConfigurationProperties(prefix = "fdfs") public class FastdfsConfig {<!-- --> /** * fastdfs external domain name */ private String outurl; }
FastDFSService
import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import java.io.IOException; @Slf4j @Service public class FastDFSService {<!-- --> @Resource private FastDFSClientWrapper fastDFSClientWrapper ; @Resource private FastdfsConfig fastdfsConfig; public String uploadFile(MultipartFile file){<!-- --> try {<!-- --> byte[] bytes = file. getBytes(); String originalFileName = file. getOriginalFilename(); String extension = originalFileName. substring(originalFileName. lastIndexOf(".") + 1); String fileName = file. getName(); long fileSize = file. getSize(); log.info("File upload file attributes [originalFileName:{}, fileName:{}, fileSize:{}, extension:{}, bytes.length:{}]", originalFileName, fileName, fileSize, extension, bytes.length ); String url = fastDFSClientWrapper.uploadFile(bytes, fileSize, extension); String resultUrl = fastdfsConfig.getOuturl() + url; log.info("File address: {}", resultUrl); return resultUrl; } catch (IOException e) {<!-- --> log.error("fastdfs failed to upload files, {}", e); } return null; } /** * download file * * @param fileUrl file URL * @return file bytes * @throws IOException */ public byte[] downloadFile(String fileUrl) throws IOException {<!-- --> byte[] bytes = fastDFSClientWrapper.downloadFile(fileUrl); return bytes; } /** * download file * * @param fileUrl file URL * @return file bytes * @throws IOException */ public void deleteFile(String fileUrl) throws IOException {<!-- --> fastDFSClientWrapper.deleteFile(fileUrl); } }
FileController
import cn.xj.file.fastdfs.FastDFSService; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; @RestController public class FileController {<!-- --> @Resource private FastDFSService fastDFSService; @PostMapping("/file/upload") public String upload(MultipartFile file){<!-- --> return fastDFSService.uploadFile(file); } }
Test file upload and view
upload picture
Copy the address to the browser to view:
pdf upload
Copy the address to the browser to view:
File Upload:
Copy the address to the browser to download:
Conclusion
In this article, we introduced how to use Spring Boot to integrate FastDFS to realize file upload, download and delete operations. With this integration, you can easily manage and manipulate file storage in Spring Boot applications. I hope this article is helpful to you. If you have any questions or questions, please leave a message to communicate