1.在服务器安装fastDfs,安装自行百度
2.在pom文件添加fastDfs依赖
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.26.1-RELEASE</version>
</dependency>
3.在application.yml中配置fastDfs
fdfs:
so-timeout: 2500
connect-timeout: 600
thumb-image: #缩略图
width: 60
height: 50
tracker-list: #tracker地址 服务器集群地址
-
ly:
upload:
baseUrl: #上传文件地址
allowTypes: #允许上传文件类型
-image/jpg
-image/jpeg
-image/png
-image/bmp
4.编写fastDfs客户端类 FastClientImporter
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 {
}
5.编写读取fastDfs配置信息类
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import java.util.List;
@Data
@ConfigurationProperties(prefix = "ly.upload")
public class UploadProperties {
private String baseUrl;
private List<String> allowTypes;
}
6.编写上传文件controller
import com.leyou.upload.service.UploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
@RestController
@RequestMapping("upload")
public class UploadController {
@Autowired
private UploadService uploadService;
@PostMapping("image")
public String uploadImage(@RequestParam("file") MultipartFile file){
return uploadService.uploadImage(file);
}
}
7.编写上传文件service实现
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.upload.config.UploadProperties;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.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 javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
@Service
@Slf4j
@EnableConfigurationProperties(UploadProperties.class)
public class UploadService {
@Autowired
private FastFileStorageClient fastFileStorageClient;
@Autowired
private UploadProperties prop;
public String uploadImage(MultipartFile file) {
try {
String contentType =file.getContentType();
if (!prop.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 dest = new File("/Users/apple/Desktop/upload",file.getOriginalFilename());
// //保存文件到本地
// file.transferTo(dest);
//String extension = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);
String extension = StringUtils.substringAfterLast(file.getOriginalFilename(),".");
StorePath storePath = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(), extension, null);
//返回路径
return prop.getBaseUrl() + storePath.getFullPath();
}catch (Exception e){
log.error("上传文件失败",e);
//throw new LyException(ExceptionEnum.UPLOAD_ERROR);
//抛出自己想抛出的异常即可
}
}
}