增加Maven依赖:
<dependency> <groupId>com.github.tobato</groupId> <artifactId>fastdfs-client</artifactId> <version>1.26.5</version> </dependency>FastDFS client git:https://github.com/tobato/FastDFS_Client
yml文件中加入 fdfs: soTimeout: 1500 connectTimeout: 600 resHost: 127.0.0.1 storagePort: 80 serverPath: path thumbImage: #缩略图生成参数 width: 150 height: 150 trackerList: #TrackerList参数,支持多个 - 127.0.0.1:22122 #多个继续增加一行即可 携带 -
package com.configure;
import com.github.tobato.fastdfs.FdfsClientConfig;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.EnableMBeanExport;
import org.springframework.context.annotation.Import;
import org.springframework.jmx.support.RegistrationPolicy;
import org.springframework.stereotype.Component;
@Data
@Component
@Import(FdfsClientConfig.class)
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
@ConfigurationProperties(prefix = "fdfs")
public class FastDFSProperties {
private String resHost;
private String storagePort;
private String serverPath;
}
package com.configure;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties(FastDFSProperties.class)
public class FastDFSConfiguration {
}
package com.fastdfs;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.configure.FastDFSProperties;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
@Component
public class FastDFSClientWrapper {
@Autowired
private FastFileStorageClient storageClient;
@Autowired
private FastDFSProperties fastDFSProperties;
public String uploadFile(MultipartFile file) throws IOException {
StorePath storePath = storageClient.uploadFile(file.getInputStream(),file.getSize(), FilenameUtils.getExtension(file.getOriginalFilename()),null);
return getResAccessUrl(storePath);
}
/**
* 封装文件完整URL地址
* @param storePath
* @return
*/
private String getResAccessUrl(StorePath storePath) {
String fileUrl = "/"+ fastDFSProperties.getServerPath()+"/" + storePath.getFullPath();
return fileUrl;
}
}
package com.controller;
import com.google.common.collect.ImmutableMap;
import com.fastdfs.FastDFSClientWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
@RestController
@RequestMapping("/store")
@CrossOrigin(maxAge = 3600L)
public class FastDFSController {
@Autowired
private FastDFSClientWrapper dfsClient;
@PostMapping("/fdfs_upload")
public Object fdfsUpload(@RequestParam("file") MultipartFile file) {
if(file.isEmpty()){
return "File is Empty";
}
try {
String fileUrl= dfsClient.uploadFile(file);
return fileUrl;
} catch (IOException e) {
e.printStackTrace();
return "Error";
}
}
}
参考文章:
https://www.2cto.com/kf/201803/727053.html
CentOS7 FastDFS服务器搭建推荐(很详细):
https://www.cnblogs.com/chiangchou/p/fastdfs.html