1.推荐一个开源的FastDFS客户端,支持最新的SpringBoot2.0。配置使用极为简单,支持连接池,支持自动生成缩略图
1.1 在文件上传的微服务中
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.26.2</version>
</dependency>
1.2引入配置启动类
@Configuration
@Import(FdfsClientConfig.class)
// 解决jmx重复注册bean的问题
@EnableMBeanExport(registration = RegistrationPolicy.IGNORE_EXISTING)
public class FastClientImporter {
}
1.3配置Fast的属性
fdfs:
so-timeout: 1501
connect-timeout: 601
thumb-image: # 缩略图
60
height: 60
tracker-list: # tracker地址
- 192.168.56.101:22122
1.4测试(测试的包名要和java中的包名一致哦)
@RunWith(SpringRunner.class)
@SpringBootTest
public class FdfsTest {
@Autowired
private FastFileStorageClient storageClient;
@Autowired
private ThumbImageConfig thumbImageConfig;
@Test
public void testUpload() throws FileNotFoundException {
File file = new File("D:\test\baby.png");
// 上传并且生成缩略图
StorePath storePath = this.storageClient.uploadFile(
new FileInputStream(file), file.length(), "png", null);
// 带分组的路径
System.out.println(storePath.getFullPath());
// 不带分组的路径
System.out.println(storePath.getPath());
}
@Test
public void testUploadAndCreateThumb() throws FileNotFoundException {
File file = new File("D:\test\baby.png");
// 上传并且生成缩略图
StorePath storePath = this.storageClient.uploadImageAndCrtThumbImage(
new FileInputStream(file), file.length(), "png", null);
// 带分组的路径
System.out.println(storePath.getFullPath());
// 不带分组的路径
System.out.println(storePath.getPath());
// 获取缩略图路径
String path = thumbImageConfig.getThumbImagePath(storePath.getPath());
System.out.println(path);
}
}
配置上传文件大小的限制
#配置最大上传图片大小
client_max_body_size 10m;
1.5测试成功
group1/M00/00/00/wKg4ZVro5eCAZEMVABfYcN8vzII630.png
M00/00/00/wKg4ZVro5eCAZEMVABfYcN8vzII630.png
M00/00/00/wKg4ZVro5eCAZEMVABfYcN8vzII630_60x60.png
2.修改文件上传Service
@Service
@Slf4j
//获得配置文件类
@EnableConfigurationProperties(UploadProperties.class)
public class UploadService {
@Autowired
private UploadProperties properties;
@Autowired
private FastFileStorageClient storageClient;
//private static final List<String> ALLOW_TYPES= Arrays.asList("image/jpeg","image/png");
public String uploadImg(MultipartFile file) {
try {
//检验文件的类型 防止恶意文件
String contentType = file.getContentType();
if (!properties.getAllowTypes().contains(contentType)){
throw new LyException(ExceptionEnum.INVALID_FILE_TYPE);
};
//校验文件的内容
BufferedImage image = ImageIO.read(file.getInputStream());
if (image==null){
throw new LyException(ExceptionEnum.INVALID_FILE_TYPE);
}
//保存文件到本地
//File local = new File("F:\javaee\IdeaResource\uploadImg\",file.getOriginalFilename());
// file.transferTo(local);
String suffix= StringUtils.substringAfterLast(file.getOriginalFilename(),".");
StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(), suffix, null);
//返回文件地址
return properties.getBaseUrl()+storePath.getFullPath();
} catch (IOException e) {
log.error("[文件上传] 上传文件失败",e);
throw new LyException(ExceptionEnum.UPLOAD_FILE_ERROR);
}
}
}
2.1配置文件类从 application.yaml中读取参数
@Data
//加载带有ly.upload前缀的配置文件属性
@ConfigurationProperties(prefix = "ly.upload")
public class UploadProperties {
private String baseUrl;
private List<String> allowTypes;
}
2.2配置文件application.yaml
server:
port: 8082
servlet:
session:
cookie:
http-only:
spring:
application:
name: upload-service
servlet:
multipart:
#最大上传文件
max-file-size: 5MB
#每次请求上传文件总和最大限制
max-request-size: 10MB
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10086/eureka
instance:
ip-address: 127.0.0.1
prefer-ip-address: true
fdfs:
so-timeout: 2500
connect-timeout: 600
thumb-image: # 缩略图
60
height: 60
tracker-list: # tracker地址
- 192.168.98.128:22122
ly:
upload:
baseUrl: http://image.leyou.com/
allowTypes:
- image/jpeg
- image/png
- image/bmp
2.3会被读取到配置文件类中去
ly:
upload:
baseUrl: http://image.leyou.com/
allowTypes:
- image/jpeg
- image/png
- image/bmp
测试成功