1.首先下載FastDFS文件系統的docker鏡像
docker search fastdfs
docker pull delron/fastdfs
2.使用docker鏡像構建tracker容器(跟蹤服務器,起到調度的作用):
docker run -d --network=host --name tracker -v /var/fdfs/tracker:/var/fdfs delron/fastdfs tracker
3.使用docker鏡像構建storage容器(存儲服務器,提供容量和備份服務):
docker run -d --network=host --name storage -e TRACKER_SERVER=ip:22122 -v /var/fdfs/storage:/var/fdfs -e GROUP_NAME=group1 delron/fastdfs storage
上面需要填寫你的tracker服務的ip地址,端口默認是22122。
4.此時兩個服務都以啟動,進行服務的配置。
進入storage容器,到storage的配置文件中配置http訪問的端口,配置文件在/etc/fdfs目錄下的storage.conf。
docker exec -it storage /bin/bash
默認端口是8888,也可以不進行更改。
5.配置nginx,在/usr/local/nginx目錄下,修改nginx.conf文件
默認配置如下:
也可以更改為如下所示:
-
location /group1/M00 {
-
alias /var/fdfs;
-
}
6.此時文件系統以搭建完畢,使用web模塊進行文件的上傳,將文件上傳至FastDFS文件系統
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.27.2</version>
</dependency>
server: port: 8082 spring: application: name: upfile-service servlet: multipart: max-file-size: 10MB max-request-size: 10MB fdfs: so-timeout: 6000 #超時時間 connect-timeout: 6000 #連接超時時間 thumb-image: #縮略圖 width: 20 height: 20 tracker-list: #蹤跡服務器tracker地址 - xxx.xx.xxx.xxxx:22122
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 FastClientImporter { }
import java.io.IOException; @RestController @RequestMapping(value = "upload") public class UpfileController { @Autowired private UpfileService upfileService; @RequestMapping(value = "image",method = RequestMethod.POST) public Result uploadImage(@RequestParam("file") MultipartFile file) throws IOException { String url= upfileService.uploadImage(file); if (StringUtils.isEmpty(url)){ return ResultUtil.error(601,"上傳失敗"); } return ResultUtil.success(url); } }
public interface UpfileService { String uploadImage(MultipartFile file) throws IOException; }
@Service public class UpfileServiceImpl implements UpfileService { private static final List<String> CONTENT_TYPES = Arrays.asList("image/gif", "image/jpeg", "image/jpg", "image/png"); private static final Logger LOGGER = LoggerFactory.getLogger(SpringApplication.class); @Autowired private FastFileStorageClient fastFileStorageClient; /* @Override public String uploadImage(MultipartFile file) throws IOException { //獲取文件名 String originalFilename = file.getOriginalFilename(); //校驗文件類型 String contentType = file.getContentType(); if (!CONTENT_TYPES.contains(contentType)) { LOGGER.info("文件類型不合法:" + originalFilename); return null; } //校驗文件內容 BufferedImage bufferedImage = ImageIO.read(file.getInputStream()); if (bufferedImage == null) { LOGGER.info("文件內容不合法:" + originalFilename); return null; } //保存到服務器 file.transferTo(new File("F:\\image\\" + originalFilename)); //返回url 進行回顯 return "http://localhost:800/" + originalFilename; }*/ @Override public String uploadImage(MultipartFile file) throws IOException { //獲取文件名 String originalFilename = file.getOriginalFilename(); //校驗文件類型 String contentType = file.getContentType(); if (!CONTENT_TYPES.contains(contentType)) { LOGGER.info("文件類型不合法:" + originalFilename); return null; } //校驗文件內容 BufferedImage bufferedImage = ImageIO.read(file.getInputStream()); if (bufferedImage == null) { LOGGER.info("文件內容不合法:" + originalFilename); return null; } //保存到服務器 //獲取文件后綴 String s = StringUtils.substringAfterLast(originalFilename, "."); StorePath storePath = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(), s, null); //返回url 進行回顯 return "http://xxx.xxx.xx.xxx:8888/" + storePath.getFullPath(); } }
7.文件存儲位置:
8.通過返回的地址給以訪問此資源
https://blog.csdn.net/weixin_38860565/article/details/90745727 感謝原作者:西門飄雪VIP提供。