分布式存储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 );
}
}
}