分散式儲存FastDFS-Spring boot 程式碼呼叫
阿新 • • 發佈:2021-08-23
分散式儲存FastDFS-Spring boot 程式碼呼叫
引入依賴
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.26.2</version>
</dependency>
編寫配置類
package top.caicai.upload.config; import com.github.tobato.fastdfs.FdfsClientConfig; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.EnableMBeanExport; import org.springframework.context.annotation.Import; import org.springframework.jmx.support.RegistrationPolicy; @Configuration @Import(FdfsClientConfig.class) // 解決jmx重複註冊bean的問題 @EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING) public class FastClientImporter { }
配置fdfs地址
記錄一下引入application.yml檔案自定義變數用法
fdfs:
connect-timeout: 1501
so-timeout: 2500
thumb-image:
height: 60
width: 60
tracker-list:
- 192.168.197.125:22122
cp:
upload:
baseUrl: http://image.caicai.top/
allowType:
- image/jpeg
- image/png
- image/jpg
檔案自定義變數用法 配置類
package top.caicai.upload.config; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import java.util.List; @Data @ConfigurationProperties(prefix = "cp.upload") public class UploadProperties { private String baseUrl; private List <String> allowType; }
程式碼呼叫
package top.caicai.upload.Service;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import top.caicai.common.enums.ExceptionEnum;
import top.caicai.common.exception.CpException;
import top.caicai.upload.config.UploadProperties;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
@EnableConfigurationProperties(UploadProperties.class)
@Service
@Slf4j
public class UploadService {
@Autowired
private FastFileStorageClient StorageClient; //注入FastFileStorageClient
@Autowired
private UploadProperties uploadProperties;
// //asList 給元素,自動封裝成集合
// private static final List<String> allowimageType= Arrays.asList("image/jpeg","image/png");
public String uploadimage(MultipartFile file) {
//儲存檔案到本地
try {
//校驗檔案型別
String contentType = file.getContentType();
//判斷檔案型別
if(!uploadProperties.getAllowType().contains(contentType)){
throw new CpException(ExceptionEnum.INVALID_FILE_TYPE );
}
//校驗檔案內容
BufferedImage image = ImageIO.read(file.getInputStream());
if(image==null){
log.error("上傳檔案型別錯誤!");
throw new CpException(ExceptionEnum.INVALID_FILE_TYPE );
}
//擷取檔案末尾,檔案格式
String extension = StringUtils.substringAfterLast(file.getOriginalFilename(),".");
//獲取路徑
StorePath storePath = StorageClient.uploadFile(file.getInputStream(), file.getSize(), extension, null);
return uploadProperties.getBaseUrl()+storePath.getFullPath();
// //目標路徑
// File dest = new File("F:\\DevelopmentProject\\CaiCaiShop\\cp-upload\\src\\main\\resources\\upload\\"+file.getOriginalFilename());
//
// file.transferTo(dest);
// return "http://image.caicai.top/"+file.getOriginalFilename();
} catch (IOException e) {
//儲存失敗
log.error("檔案上傳,失敗!",e);
throw new CpException(ExceptionEnum.UPLOAD_FILE_falid );
}
}
}
開開心心,上班!
快快樂樂,遊玩!
及時行樂!