1.安装好FastDFS后,打开leyou-upload模块的pom.xml依赖文件添加FDFS客户端依赖:
<!--FastDFS客户端-->
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
</dependency>
2.修改leyou-upload模块的application.yml文件:
server:
port: 8082
spring:
application:
name: upload-service
servlet:
multipart:
max-file-size: 5MB #配置上传最大文件大小
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10086/eureka #注册到eureka注册中心
instance:
lease-expiration-duration-in-seconds: 5 #心跳时间
lease-renewal-interval-in-seconds: 15 #过期时间
fdfs:
so-timeout: 1501 #超时时间
connect-timeout: 601 #连接超时时间
thumb-image: #缩略图
width: 60
height: 60
tracker-list: #tracker地址:自己的虚拟机服务器地址+端口(默认是22122)
- 192.168.77.88:22122
3.创建fdfs客户端实体类:
- 文件目录:
- 代码:
package com.leyou.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 {
}
4.添加测试类测试fdfs是否可以成功上传:
- 文件目录
- FastDFSTest.java代码:
package com.leyou.upload.test;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.domain.ThumbImageConfig;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
@SpringBootTest
@RunWith(SpringRunner.class)
public class FastDFSTest {
@Autowired
private FastFileStorageClient storageClient;
@Autowired
private ThumbImageConfig thumbImageConfig;
@Test
public void testUpload() throws FileNotFoundException {
//要上传的文件
File file = new File("C:\\Users\\HP\\Desktop\\a.png");
// 上传并保存图片,参数:1-上传的文件流 2-文件的大小 3-文件的后缀 4-可以不管他
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("C:\\Users\\HP\\Desktop\\a.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);
}
}
- 启动Test,成功上传图片文件:
- 成功访问:
5.改造文件上传: - 更改hosts文件:
- 更改leyou-upload模块下的service层的代码:
- 代码:
package com.leyou.upload.service;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.leyou.upload.config.FastClientImporter;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
@Service
public class UploadService {
private static final List<String> CONTENT_TYPES = Arrays.asList("image/gif","image/jpeg");
private static final Logger LOGGER = LoggerFactory.getLogger(UploadService.class);
@Autowired
private FastFileStorageClient storageClient;
public String uploadImage(MultipartFile file){
//获取文件名称
String originalFilename = file.getOriginalFilename();
//校验文件类型
String contentType = file.getContentType();
if(!CONTENT_TYPES.contains(contentType)){
LOGGER.info("文件类型不合法: {}",originalFilename);
return null;
}
try {
//校验文件内容
BufferedImage bufferedImage = ImageIO.read(file.getInputStream());
if(bufferedImage == null){
LOGGER.info("文件内容不合法:{}",originalFilename);
}
//保存到服务器
//file.transferTo(new File("G:\\SpringBoot_leyou\\image\\" + originalFilename));
String ext = StringUtils.substringAfterLast(originalFilename,".");
StorePath storePath = this.storageClient.uploadFile(file.getInputStream(),file.getSize(),ext,null);
//返回url,进行回显
//return "http://image.leyou.com/" + originalFilename;
return "http://image.leyou.com/" + storePath.getFullPath();
} catch (IOException e) {
LOGGER.info("服务器内部错误:" + originalFilename);
e.printStackTrace();
}
return null;
}
}
- 重新启动leyou-upload的启动引导类:
- 成功上传并且回显图片。