目錄
- 5 SpringBoot整合
- 5.1 操作步驟
- 5.2 項目依賴
- 5.3 客戶端開發
- 5.3.1 FastDFS配置
- 5.3.2 FastDFS配置類
- 5.3.3 文件工具類
- 5.3.4 文件上傳配置
- 5.3.5 配置Swagger2
- 5.3.6 API接口
- 5.4 接口演示
- 配置FastDFS執行環境
- 文件上傳配置
- 整合Swagger2測試接口
<!-- FastDFS依賴 --><dependency><groupId>com.github.tobato</groupId><artifactId>fastdfs-client</artifactId><version>1.26.5</version></dependency><!-- Swagger2 核心依賴 --><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifact><version>2.9.2</version></dependency><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>2.9.2</version></dependency>5.3 客戶端開發5.3.1 FastDFS配置fdfs:# 鏈接超時connect-timeout: 60# 讀取時間so-timeout: 60# 生成縮略圖參數thumb-image:width: 150height: 150tracker-list: 192.168.86.101:221225.3.2 FastDFS配置類【第2-1-4章 SpringBoot整合FastDFS文件存儲服務】@Configuration@Import(FdfsClientConfig.class)// 避免Jmx重復注冊bean@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)public class DFSConfig {}5.3.3 文件工具類import com.github.tobato.fastdfs.domain.fdfs.StorePath;import com.github.tobato.fastdfs.service.FastFileStorageClient;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.stereotype.Component;import org.springframework.util.StringUtils;import org.springframework.web.multipart.MultipartFile;import javax.annotation.Resource;@Componentpublic class FileDfsUtil {private static final Logger LOGGER = LoggerFactory.getLogger(FileDfsUtil.class);@Resourceprivate FastFileStorageClient storageClient ;/*** 上傳文件*/public String upload(MultipartFile multipartFile) throws Exception{String originalFilename = multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf(".") + 1);StorePath storePath = this.storageClient.uploadImageAndCrtThumbImage(multipartFile.getInputStream(),multipartFile.getSize(),originalFilename , null);return storePath.getFullPath() ;}/*** 刪除文件*/public void deleteFile(String fileUrl) {if (StringUtils.isEmpty(fileUrl)) {LOGGER.info("fileUrl == >>文件路徑為空...");return;}try {StorePath storePath = StorePath.parseFromUrl(fileUrl);storageClient.deleteFile(storePath.getGroup(), storePath.getPath());} catch (Exception e) {LOGGER.info(e.getMessage());}}}5.3.4 文件上傳配置spring:application:name: fdfs-demojackson:time-zone: GMT+8date-format: yyyy-MM-dd HH:mm:ssservlet:multipart:max-file-size: 100MBmax-request-size: 100MBenabled: true5.3.5 配置Swagger2配置類:import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import springfox.documentation.builders.ApiInfoBuilder;import springfox.documentation.builders.PathSelectors;import springfox.documentation.builders.RequestHandlerSelectors;import springfox.documentation.service.ApiInfo;import springfox.documentation.spi.DocumentationType;import springfox.documentation.spring.web.plugins.Docket;/** * Swagger 配置文件 */@Configurationpublic class SwaggerConfig {@Beanpublic Docket createRestApi() {return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.basePackage("com.itheima.fdfs.demo")).paths(PathSelectors.any()).build();}private ApiInfo apiInfo() {return new ApiInfoBuilder().title("SpringBoot利用Swagger構建API文檔").description("Fast DFS接口").version("version 1.0").build();}}啟動類:import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import springfox.documentation.swagger2.annotations.EnableSwagger2;@EnableSwagger2@SpringBootApplicationpublic class FdfsDemoApplication { public static void main(String[] args) {SpringApplication.run(FdfsDemoApplication.class, args); }}
經驗總結擴展閱讀
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 海螺哪里不能吃
- 西瓜切開了可以隔夜第二天吃嗎
- 上古卷軸5怎樣結婚(上古卷軸5第一美女)
- 2022年第22號臺風“尼格”在西北太平洋洋面上生成
- 火車站行李托運流程
- 西蘭花怎么吃
- 2023年出伏時間 2023年初伏第一天是幾月幾號
- 奧比島暗夜襲擊第一輪金塊失蹤之謎通關攻略
- 紙嫁衣4第一章圓珠筆怎么獲取以及有什么用
- 火鍋底料可以吃第二次嗎
