springboot+fastdfs


第一步:

开启FastDFS服务(我设置了开启自启动所以不用再次配置)

第二步:

git拉取代码

https://gitee.com/fastdfs100/fastdfs-client-java.git

进入下载目录

cd fastdfs-client-java

打包到本地仓库

mvn install

1.6

1.8

新建一个springboot项目

 创建一个controller用来进行测试

@RestController
@RequestMapping("/fdfs")
public class FdfsController {
@GetMapping(value = "/hello")
public String hello(){
return "桃李不言下";
}

}
配置文件启动端口修改一下
server.port=8090
pom.xml文件
 
        1.8
        1.2.75
        5.5.2
        1.1.2
        2.8.0
        3.11
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-webflux
        
        
            org.springframework.session
            spring-session-core
        
        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        
        
            org.springframework.boot
            spring-boot-configuration-processor
            true
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-tomcat
            provided
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            io.projectreactor
            reactor-test
            test
        

        
            cn.hutool
            hutool-all
            ${hutool.version}
        

        
            com.github.binarywang
            java-testdata-generator
            ${generator.version}
        

        
            commons-io
            commons-io
            ${commons-io.version}
        

        
            com.alibaba
            fastjson
            ${fastjson.version}
        

        
            org.apache.commons
            commons-lang3
            ${commons-lang3.version}
        

        
            org.csource
            fastdfs-client-java
            1.29-SNAPSHOT
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                2.5.10
            
        
    

 resources下fastdfs-client.properties

fastdfs.connect_timeout_in_seconds = 5
fastdfs.network_timeout_in_seconds = 30
fastdfs.charset = UTF-8
fastdfs.http_anti_steal_token = false
fastdfs.http_secret_key = FastDFS1234567890
fastdfs.http_tracker_http_port = 80

fastdfs.tracker_servers = 192.168.178.130:22122

fastdfs.connection_pool.enabled = true
fastdfs.connection_pool.max_count_per_entry = 500
fastdfs.connection_pool.max_idle_time = 3600
fastdfs.connection_pool.max_wait_time_in_ms = 1000

application.yml

server:
    port: 8090
spring:
  servlet:
    multipart:
      max-file-size: 50MB #文件上传大小限制为
      max-request-size: 200MB #请求大小限制为

fdfs_storage_proxy:
  protocol: http://
  domain: 192.168.178.130
  port: 8888

 测试

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/fdfs")
public class FdfsController {
    @Value("${fdfs_storage_proxy.protocol}")
    private String fdfs_storage_proxy_protocol;
    @Value("${fdfs_storage_proxy.domain}")
    private String fdfs_storage_proxy_domain;
    @Value("${fdfs_storage_proxy.port}")
    private String fdfs_storage_proxy_port;

    @GetMapping("/proxy")
    public String proxy(){
        return fdfs_storage_proxy_protocol+fdfs_storage_proxy_domain+":"+fdfs_storage_proxy_port;
    }

    @GetMapping(value = "/hello")
    public String hello(){
        return "桃李不言下";
    }
}
返回值状态枚举
ResultCode.enum
/*
* 返回值状态枚举
* */
public enum ResultCode {
    /*
     * 操作成功 请求成功
     * */
    SUCCESS("200","操作成功","请求成功"),
    /*
     * 上传失败
     * */
    UPLOAD_FAILED("700","上传失败","上传失败");

/*
* 状态码
* */
private String code;
/*
* 状态码含义
* */
 private String msg;
 /*
 * 状态码含义描述
 * */
 private String desc;

 ResultCode(String code,String msg,String desc){
     this.code=code;
     this.msg=msg;
     this.desc=desc;
 }

    public String getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public String getDesc() {
        return desc;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }
}
R.java
/*
* 统一返回数据格式
* */
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
public class R{
    /*
     * 状态码
     * */
    private String code;
    /*
     * 状态码含义
     * */
    private String msg;
    /*
     * 状态码含义描述
     * */
    private T data;
}
FdfsController.java
@RestController
@RequestMapping("/fdfs")
public class FdfsController {
    @Value("${fdfs_storage_proxy.protocol}")
    private String fdfs_storage_proxy_protocol;
    @Value("${fdfs_storage_proxy.domain}")
    private String fdfs_storage_proxy_domain;
    @Value("${fdfs_storage_proxy.port}")
    private String fdfs_storage_proxy_port;

    @GetMapping("/proxy")
    public String proxy(){
        return fdfs_storage_proxy_protocol+fdfs_storage_proxy_domain+":"+fdfs_storage_proxy_port;
    }

    @GetMapping(value = "/hello")
    public String hello(){
        return "桃李不言下";
    }


    @RequestMapping("/upload1")
    public Rupdate01(@RequestParam("header")MultipartFile multipartFile){
        //封装返回数据 默认为上传失败
        R response = new R<>(ResultCode.UPLOAD_FAILED.getCode(), ResultCode.UPLOAD_FAILED.getMsg(), null);
        //获取原文件的名字
        String originalFilename = multipartFile.getOriginalFilename();
        //获取源文件后缀名
        String suffix = FilenameUtils.getExtension(originalFilename);
        //文件上传
        try {
            //加载fastdfs-client.properties配置文件
            ClientGlobal.initByProperties("fastdfs-client.properties");
            //获取tracker和storage客户端对象
            TrackerClient client = new TrackerClient();
            //获取tracker地址
            TrackerServer trackerServer = client.getTrackerServer();
            StorageServer storageServer=null;
            //获取storage地址
            StorageClient storageClient = new StorageClient(trackerServer, storageServer);
            //文件上传字节数组
            InputStream inputStream = multipartFile.getInputStream();
            int length = inputStream.available();
            byte[] bytes = new byte[length];
            //文件上传成功后重新封装返回数据
            inputStream.read(bytes);
            String[] result = storageClient.upload_file(bytes, suffix, null);
            System.out.println(JSON.toJSONString(result));
            //文件上传后,重新封装返回数据
            response.setCode(ResultCode.SUCCESS.getCode());
            response.setMsg(ResultCode.SUCCESS.getMsg());
            String url =fdfs_storage_proxy_protocol+fdfs_storage_proxy_domain+":"+fdfs_storage_proxy_port+"/" +result[0]+"/"+result[1];
            response.setData(url);
        } finally {
            //向用户响应上传成功的访问地址
            return response;
        }
    }

}

 封装成Utils工具类

BaseFastDFSUtil.java
/*
* FastDFS工具类
* */
public class BaseFastDFSUtil {
    /*
    * 配置文件
    * */
    private final static String propFilePath="fastdfs-client.properties";

    static {
        //加载fastdfs-client.properties配置文件
        try {
            ClientGlobal.initByProperties(propFilePath);
        } catch (IOException  | MyException e) {
            e.printStackTrace();
        }
    }

    /*
    *文件上传到fastdfs
    *fdfs_storage_proxy_url storage web服务地址
    * inputStream 上传文件的自己输入流
    * file_etx_name 上传文件的后缀名
    *  上传成功后的web地址
    *  */
    public static String upload(String fdfs_storage_proxy_url, InputStream inputStream,String file_ext_name) throws Exception {
        //获取tracker和storage客户端对象
        TrackerClient client = new TrackerClient();
        //获取tracker地址
        TrackerServer trackerServer = client.getTrackerServer();
        StorageServer storageServer=null;
        //获取storage地址
        StorageClient storageClient = new StorageClient(trackerServer, storageServer);
        int length = inputStream.available();
        byte[] bytes = new byte[length];
        //文件上传成功后重新封装返回数据
        inputStream.read(bytes);
        String[] result = storageClient.upload_file(bytes, file_ext_name, null);
        System.out.println(JSON.toJSONString(result));
        //断开连接
        storageClient.close();
        return fdfs_storage_proxy_url+"/" +result[0]+"/"+result[1];
    }
}
FdfsController.java
@RestController
@RequestMapping("/fdfs")
public class FdfsController {
    @Value("${fdfs_storage_proxy.protocol}")
    private String fdfs_storage_proxy_protocol;
    @Value("${fdfs_storage_proxy.domain}")
    private String fdfs_storage_proxy_domain;
    @Value("${fdfs_storage_proxy.port}")
    private String fdfs_storage_proxy_port;

    @GetMapping("/proxy")
    public String proxy(){
        return fdfs_storage_proxy_protocol+fdfs_storage_proxy_domain+":"+fdfs_storage_proxy_port;
    }

    @GetMapping(value = "/hello")
    public String hello(){
        return "桃李不言下";
    }


    @RequestMapping("/upload1")
    public Rupdate01(@RequestParam("header")MultipartFile multipartFile){
        //封装返回数据 默认为上传失败
        R response = new R<>(ResultCode.UPLOAD_FAILED.getCode(), ResultCode.UPLOAD_FAILED.getMsg(), null);
        //获取原文件的名字
        String originalFilename = multipartFile.getOriginalFilename();
        //获取源文件后缀名
        String suffix = FilenameUtils.getExtension(originalFilename);
        //文件上传
        try {
            //加载fastdfs-client.properties配置文件
            ClientGlobal.initByProperties("fastdfs-client.properties");
            //获取tracker和storage客户端对象
            TrackerClient client = new TrackerClient();
            //获取tracker地址
            TrackerServer trackerServer = client.getTrackerServer();
            StorageServer storageServer=null;
            //获取storage地址
            StorageClient storageClient = new StorageClient(trackerServer, storageServer);
            //文件上传字节数组
            InputStream inputStream = multipartFile.getInputStream();
            int length = inputStream.available();
            byte[] bytes = new byte[length];
            //文件上传成功后重新封装返回数据
            inputStream.read(bytes);
            String[] result = storageClient.upload_file(bytes, suffix, null);
            System.out.println(JSON.toJSONString(result));
            //文件上传后,重新封装返回数据
            response.setCode(ResultCode.SUCCESS.getCode());
            response.setMsg(ResultCode.SUCCESS.getMsg());
            //断开连接
            storageClient.close();
            String url =fdfs_storage_proxy_protocol+fdfs_storage_proxy_domain+":"+fdfs_storage_proxy_port+"/" +result[0]+"/"+result[1];
            response.setData(url);
        } finally {
            //向用户响应上传成功的访问地址
            return response;
        }
    }

    @RequestMapping("/upload2")
    public Rupdate02(@RequestParam("header")MultipartFile multipartFile){
        //封装返回数据 默认为上传失败
        R response = new R<>(ResultCode.UPLOAD_FAILED.getCode(), ResultCode.UPLOAD_FAILED.getMsg(), null);
        //获取原文件的名字
        String originalFilename = multipartFile.getOriginalFilename();
        //获取源文件后缀名
        String suffix = FilenameUtils.getExtension(originalFilename);
        //文件上传
        try {
            String proxy=fdfs_storage_proxy_protocol+fdfs_storage_proxy_domain+":"+fdfs_storage_proxy_port;
            String url = BaseFastDFSUtil.upload(proxy, multipartFile.getInputStream(), suffix);
            //文件上传后,重新封装返回数据
            response.setCode(ResultCode.SUCCESS.getCode());
            response.setMsg(ResultCode.SUCCESS.getMsg());
            response.setData(url);
        } finally {
            //向用户响应上传成功的访问地址
            return response;
        }
    }
}