Spring Boot(四十四):Springboot整合FastDFS完成文件上传(2)

1.创建项目

Spring Boot(四十四):Springboot整合FastDFS完成文件上传(2)_第1张图片

 

2.添加引用

Spring Boot(四十四):Springboot整合FastDFS完成文件上传(2)_第2张图片

 

3.修改pom.xml

        
        
            com.github.tobato
            fastdfs-client
            1.26.7
        

 

4.创建yml

fdfs:
  so-timeout: 2500       # 读取时间
  connect-timeout: 600   # 连接超时时间
  thumb-image:           # 缩略图
    width: 100
    height: 100
  tracker-list:          # tracker服务配置地址列表
    - 192.168.0.110:22122
upload:
  base-url: http://192.168.0.110/
  allow-types:
    - image/jpeg
    - image/png
    - image/bmp
    - image/gif

 

5.创建配置类UploadProperties

package com.example.demo.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.util.List;

@ConfigurationProperties(prefix = "upload")
@Data
public class UploadProperties {

    private String baseUrl;

    private List allowTypes;
}

 

6.创建UploadService

package com.example.demo.utils;

import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.example.demo.config.UploadProperties;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;

@Component
@EnableConfigurationProperties(UploadProperties.class)
public class UploadService {
    private Log log= LogFactory.getLog(UploadService.class);

    @Autowired
    private FastFileStorageClient storageClient;

    @Autowired
    private UploadProperties prop;

    public String uploadImage(MultipartFile file) {
        // 1、校验文件类型
        String contentType = file.getContentType();
        if (!prop.getAllowTypes().contains(contentType)) {
            throw new RuntimeException("文件类型不支持");
        }
        // 2、校验文件内容
        try {
            BufferedImage image = ImageIO.read(file.getInputStream());
            if (image == null || image.getWidth() == 0 || image.getHeight() == 0) {
                throw new RuntimeException("上传文件有问题");
            }
        } catch (IOException e) {
            log.error("校验文件内容失败....{}", e);
            throw new RuntimeException("校验文件内容失败"+e.getMessage());
        }

        try {
            // 3、上传到FastDFS
            // 3.1、获取扩展名
            String extension = StringUtils.substringAfterLast(file.getOriginalFilename(), ".");
            // 3.2、上传
            StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(), extension, null);
            // 返回路径
            return prop.getBaseUrl() + storePath.getFullPath();
        } catch (IOException e) {
            log.error("【文件上传】上传文件失败!....{}", e);
            throw  new RuntimeException("【文件上传】上传文件失败!"+e.getMessage());
        }
    }
}

 

7.创建UploadController

package com.example.demo.controller;

import com.example.demo.utils.UploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("upload")
public class UploadController {

    @Autowired
    private UploadService uploadService;


    /**
     * 作上传
     */
    @RequestMapping("doUpload")
    public Map doUpload(MultipartFile mf){
        System.out.println(mf.getOriginalFilename());
        Map map = new HashMap<>();
        String filepath= uploadService.uploadImage(mf);
        map.put("path",filepath);
        return map;
    }

}

 

8.创建index.html测试




    
    Title




文件上传


测试结果如下:

Spring Boot(四十四):Springboot整合FastDFS完成文件上传(2)_第3张图片

你可能感兴趣的:(FastDFS,SpringBoot)