FastDFS(三)fastdfs-springboot

1、創建SpringBoot項目

項目名fastdfs-springboot
githun地址:

導入 fastdfs-client 依賴,記得導入web依賴

<dependency>
    <groupId>com.github.tobato</groupId>
    <artifactId>fastdfs-client</artifactId>
    <version>1.26.7</version>
</dependency>

修改application.yml
IP地址記得修改爲自己的。

fdfs:
  so-timeout: 2500       # 讀取時間
  connect-timeout: 600   # 連接超時時間
  thumb-image:           # 縮略圖
    width: 100
    height: 100
  tracker-list:          # tracker服務配置地址列表
    - 192.168.202.128:22122
upload:
    base-url: http://192.168.202.128/
    #配置允許的文件的類型
    allow-types:
      - image/jpeg
      - image/png
      - image/bmp

注:

  • 服務器地址,記得修改
  • 查看文件的類型,
    Mini Type參考手冊
    https://www.w3school.com.cn/media/media_mimeref.asp

2、創建前端頁面 static/index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>FastDFS 測試</title>
</head>
<body>
    <form action="/upload/doUpload" method="post" enctype="multipart/form-data">
        <input type="file" name="mf"/>
        <input type="submit" value="上傳"/>
    </form>
</body>
</html>

3、創建配置類

@Component
@ConfigurationProperties(prefix = "upload")
@Data
public class UploadProperties {
    private String baseUrl;
    private List<String> allowTypes;
}

4,創建服務類

注入FastFileStorageClient後,可以直接調用其uploadFile()。
比上一種方法省去了手動設置配置信息,也不需要自己建立tracker連接、獲取storage、再獲得storageClient這些準備工作。

@Service
@EnableConfigurationProperties(UploadProperties.class)
public class UploadService {
        private Log log= LogFactory.getLog(UploadService.class);
        @Autowired
        private FastFileStorageClient storageClient;
        @Autowired
        private UploadProperties prop;
        public String uploadImage(MultipartFile file) {
            // 1、校驗文件類型
            String contentType = file.getContentType();
            if (!prop.getAllowTypes().contains(contentType)) {
                throw new RuntimeException("文件類型不支持");
            }
            // 2、校驗文件內容
            try {
                BufferedImage image = ImageIO.read(file.getInputStream());
                if (image == null || image.getWidth() == 0 || image.getHeight() == 0) {
                    throw new RuntimeException("上傳文件有問題");
                }
            } catch (IOException e) {
                log.error("校驗文件內容失敗....{}", e);
                throw new RuntimeException("校驗文件內容失敗"+e.getMessage());
            }
            try {
                // 3、上傳到FastDFS
                // 3.1、獲取擴展名
                String extension = StringUtils.substringAfterLast(file.getOriginalFilename(), ".");
                // 3.2、上傳
                StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(), extension, null);
                // 返回路徑
                return prop.getBaseUrl() + storePath.getFullPath();
            } catch (IOException e) {
                log.error("【文件上傳】上傳文件失敗!....{}", e);
                throw  new RuntimeException("【文件上傳】上傳文件失敗!"+e.getMessage());
            }
        }
    }

5,創建UploadController

@RestController
@RequestMapping("upload")
public class UploadController {
   @Autowired
   private UploadService uploadService;
    @RequestMapping("doUpload")
    @ResponseBody
    public Map<String,Object> doUpload(MultipartFile mf){
        System.out.println(mf.getOriginalFilename());
        Map<String, Object> upload =new HashMap<>();
        String filePath= this.uploadService.uploadImage(mf);
        upload.put("path",filePath);
        return upload;
    }
}

6、測試

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章