Springboot整合FastFDS

springboot版本采用1.5.3

thymeleaf模板要设置3.0.0的版本

否则在显示html5时候会出错

thymeleaf配置

 

spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.servlet.content-type=text/html
# 关闭缓存,即时刷新,上线生产环境需改成true
spring.thymeleaf.cache=false

fastDFS配置

 

fdfs.connect-timeout=600
fdfs.so-timeout=1500
fdfs.tracker-list[0]=192.168.25.133:22122
fdfs.thumbImage.height=150
fdfs.thumbImage.width=150
IMAGE_SERVER_URL=http://192.168.25.133/
spring.jmx.enabled=false

fastDFS依赖

 


    com.github.tobato
    fastdfs-client
    1.25.4-RELEASE

github上应该有不同的fastdfs-client版本, 但目前开源的这个这个版本只能适用于springBoot-1.5.3如果换更高版本会报错。

使用fastDFS只需要配置几个地方即可:

 

@SpringBootApplication
@EnableScheduling
@Import(FdfsClientConfig.class)
public class SpringbootFastdfsApplication {

   public static void main(String[] args) {
      SpringApplication.run(SpringbootFastdfsApplication.class, args);
   }
}

核心的类是 

FastFileStorageClient

 

@Service
public class PictureServiceImpl implements  PictureService {
    @Autowired
    private FastFileStorageClient fastFileStorageClient;

    @Value("${IMAGE_SERVER_URL}")
    private String IMAGE_SERVER_URL;
    @Override
    public String uploadPicture(MultipartFile file){
        //截取后缀
        try {
            String originalFilename = file.getOriginalFilename();
            System.out.println(originalFilename);
            String extName = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
            System.out.println(extName);
            StorePath storePath = fastFileStorageClient.uploadFile(file.getInputStream(), file.getSize(), FilenameUtils.getExtension(file.getOriginalFilename()), null);
            System.out.println(storePath);
            String fileUrl = IMAGE_SERVER_URL + storePath.getFullPath();
            return fileUrl;
        } catch (IOException e) {
            e.printStackTrace();
            return  null;
        }
    }
}

springboot如果采用2.0以上会报错如下,

Configuration property name 'fdfs.thumbImage' is not valid:


    Invalid characters: 'I'
    Bean: pictureController

    Reason: Canonical names should be kebab-case ('-' separated), lowercase alpha-numeric characters and must start with a letter

 

 


Action:

 

 

 

 

Modify 'fdfs.thumbImage' so that it conforms to the canonical names requirements.

 

 

 

这个是是FastDFS的版本问题。

springboot的版本为2.0以上时候,使用1.26.1-RELEASE版本

    com.github.tobato
    fastdfs-client
    1.26.1-RELEASE

即可以解决上述问题。

图片服务器搭建参考:

https://blog.csdn.net/u014695188/article/details/51577260。

图片服务器的访问路径:

http://ip/group1/M00/00/01/wKgZhVtgdNmAE0OsAAIkMqZJs14177.jpg

上传图片生成缩略图的方法参考下面这篇文章:

https://blog.csdn.net/qq741437836/article/details/74311802

 

你可能感兴趣的:(springboot)