目录
- 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:22122
5.3.2 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: true
5.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.3.6 API接口【第2-1-4章 SpringBoot整合FastDFS文件存储服务】import com.itheima.fdfs.demo.common.FileDfsUtil;import io.swagger.annotations.ApiOperation;import org.springframework.http.ResponseEntity;import org.springframework.util.StringUtils;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.multipart.MultipartFile;import javax.annotation.Resource;@RestControllerpublic class FileController {@Resourceprivate FileDfsUtil fileDfsUtil ;/*** http://localhost:8081/swagger-ui.html*/@ApiOperation(value="https://www.huyubaike.com/biancheng/上传文件", notes="测试FastDFS文件上传")@RequestMapping(value = "https://www.huyubaike.com/uploadFile",headers="content-type=multipart/form-data", method = RequestMethod.POST)public ResponseEntity<String> uploadFile (@RequestParam("file") MultipartFile file){String result ;try{String path = fileDfsUtil.upload(file) ;if (!StringUtils.isEmpty(path)){result = path ;} else {result = "上传失败" ;}} catch (Exception e){e.printStackTrace() ;result = "服务异常" ;}return ResponseEntity.ok(result);}/*** 文件删除*/@RequestMapping(value = "https://www.huyubaike.com/deleteByPath", method = RequestMethod.GET)public ResponseEntity<String> deleteByPath (){String filePathName = "group1/M00/00/00/rBIAAmNmi82AJxLsAABdrZgsqUU214.jpg" ;fileDfsUtil.deleteFile(filePathName);return ResponseEntity.ok("SUCCESS") ;}}
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 上古卷轴5怎样结婚(上古卷轴5第一美女)
- 奥比岛暗夜袭击第一轮金块失踪之谜通关攻略
- 纸嫁衣4第一章圆珠笔怎么获取以及有什么用
- 原神纸间奇旅第二天怎么通关
- 第2-1-3章 docker-compose安装FastDFS,实现文件存储服务
- 光与夜之恋第十五期真心话大冒险怎么做
- 奥比岛手游验证猜想第三章要怎么通关
- 第2-1-2章 传统方式安装FastDFS-附FastDFS常用命令
- SpringBoot 自动装配原理
- 奥比岛:梦想国度暗夜袭击第三章怎么通关