使用docker安裝fastDFS


1.安裝fastdfs必要組件 tracker  什么都不用改

docker run -d --network=host --name tracker -v /var/fdfs/tracker:/var/fdfs delron/fastdfs tracker

2.安裝fastdfs存儲地址 storage 除了ip其他的都不用改

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

3.進入容器修改配置  一定要,要不然鏈接不上

docker exec -it tracker bash

4.修改配置

vi /etc/fdfs/client.conf

將配置 tracker_server=你自己的ip:22122

到這其實fastDFS就配好了

創建項目測試

我這里用的是**springBoot**進行整合。**swagger-ui**進行圖片上傳

1.pom依賴

 1         <!-- FastDFS依賴 -->
 2         <dependency>
 3             <groupId>com.github.tobato</groupId>
 4             <artifactId>fastdfs-client</artifactId>
 5             <version>1.26.5</version>
 6         </dependency>
 7         <!-- Swagger2 核心依賴 -->
 8         <dependency>
 9             <groupId>io.springfox</groupId>
10             <artifactId>springfox-swagger2</artifactId>
11             <version>2.6.1</version>
12         </dependency>
13         <dependency>
14             <groupId>io.springfox</groupId>
15             <artifactId>springfox-swagger-ui</artifactId>
16             <version>2.6.1</version>
17         </dependency>

2.配置 yml **需要修改ip**

 1 spring:
 2   servlet:
 3     multipart:
 4       max-file-size: 100MB # 最大支持文件大小
 5       max-request-size: 100MB # 最大支持請求大小
 6 # 分布式文件系統FDFS配置
 7 fdfs:
 8   # 鏈接超時
 9   connect-timeout: 600
10   # 讀取時間
11   so-timeout: 600
12   # 生成縮略圖參數
13   thumb-image:
14     width: 150
15     height: 150
16   tracker-list: 你自己的ip:22122

3.配置文件(兩個)

SwaggerConfig.java   **一定要改成你自己項目的controller包路徑,這里會掃描你的接口**

 1 @Configuration
 2 public class SwaggerConfig {
 3     @Bean
 4     public Docket createRestApi() {
 5         return new Docket(DocumentationType.SWAGGER_2)
 6                 .apiInfo(apiInfo())
 7                 .select()
 8                 .apis(RequestHandlerSelectors.basePackage("top.mail.email.controller"))
 9                 .paths(PathSelectors.any())
10                 .build();
11     }
12     private ApiInfo apiInfo() {
13         return new ApiInfoBuilder()
14                 .title("SpringBoot利用Swagger構建API文檔")
15                 .description("使用RestFul風格, 創建人:知了一笑")
16                 .termsOfServiceUrl("https://github.com/cicadasmile")
17                 .version("version 1.0")
18                 .build();
19     }
20 }

DfsConfig.java

1 @Configuration
2 @Import(FdfsClientConfig.class)
3 // Jmx重復注冊bean的問題
4 @EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
5 public class DfsConfig {
6 }

4.工具類

FileDfsUtil.java

 1 @Component
 2 public class FileDfsUtil {
 3     private static final Logger LOGGER = LoggerFactory.getLogger(FileDfsUtil.class);
 4     @Resource
 5     private FastFileStorageClient storageClient ;
 6     /**
 7      * 上傳文件
 8      */
 9     public String upload(MultipartFile file) throws Exception{
10         StorePath storePath = storageClient.uploadFile(file.getInputStream(),file.getSize(), FilenameUtils.getExtension(file.getOriginalFilename()),null);
11         return storePath.getFullPath() ;
12     }
13     /**
14      * 刪除文件
15      */
16     public void deleteFile(String fileUrl) {
17         if (StringUtils.isEmpty(fileUrl)) {
18             LOGGER.info("fileUrl == >>文件路徑為空...");
19             return;
20         }
21         try {
22             StorePath storePath = StorePath.parseFromUrl(fileUrl);
23             storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
24         } catch (Exception e) {
25             LOGGER.info(e.getMessage());
26         }
27     }
28 }

5.controller接口

FileController.java

 1 @RestController
 2 public class FileController {
 3     @Resource
 4     private FileDfsUtil fileDfsUtil ;
 5     /**
 6      * 文件上傳
 7      */
 8     @ApiOperation(value="上傳文件", notes="測試FastDFS文件上傳")
 9     @RequestMapping(value = "/uploadFile",headers="content-type=multipart/form-data", method = RequestMethod.POST)
10     public ResponseEntity<String> uploadFile (@RequestParam("file") MultipartFile file){
11         String result ;
12         try{
13             String path = fileDfsUtil.upload(file) ;
14             if (!StringUtils.isEmpty(path)){
15                 result = path ;
16             } else {
17                 result = "上傳失敗" ;
18             }
19         } catch (Exception e){
20             e.printStackTrace() ;
21             result = "服務異常" ;
22         }
23         return ResponseEntity.ok(result);
24     }
25     /**
26      * 文件刪除
27      */
28     @RequestMapping(value = "/deleteByPath", method = RequestMethod.GET)
29     public ResponseEntity<String> deleteByPath (){
30         String filePathName = "group1/M00/00/00/wKhIgl0n4AKABxQEABhlMYw_3Lo825.png" ;
31         fileDfsUtil.deleteFile(filePathName);
32         return ResponseEntity.ok("SUCCESS") ;
33     }
34 }

6.springBoot啟動類

1 @SpringBootApplication
2 @EnableSwagger2
3 public class EmailApplication {
4 
5     public static void main(String[] args) {
6         SpringApplication.run(EmailApplication.class, args);
7     }
8 
9 }

啟動項目

訪問::::

http://localhost:8080/swagger-ui.html

 

 返回地址表示上傳成功。

 

怎么訪問呢?

默認的話,是通過你的  ip:8888/上面返回的地址    如果你是阿里雲服務就要手動開放 8888、22122、23000 這三個端口

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM