第2-1-4章 SpringBoot整合FastDFS文件存储服务( 二 )

5.3.6 API接口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.4 接口演示

  1. 访问页面:http://localhost:8081/fdfs-demo/swagger-ui.html
    第2-1-4章 SpringBoot整合FastDFS文件存储服务

    文章插图
  2. 测试接口
    • 上传接口
    • 删除接口

经验总结扩展阅读