二、SpringBoot实现上传文件到fastDFS文件服务器

diediexiaomi 2020-04-19

上篇文章介绍了如何使用docker安装fastDFS文件服务器,这一篇就介绍整合springBoot实现文件上传到fastDFS文件服务器

1.pom.xml文件添加依赖

<!-- 连接fastdfs文件系统 -->
<dependency>
        <groupId>net.oschina.zcx7878</groupId>
        <artifactId>fastdfs-client-java</artifactId>
        <version>1.27.0.0</version>
</dependency>

2.在resource包下创建配置文件fdfs_client.conf  

  tracker_server的值ip为你文件服务器的ip
connect_timeout=30
network_timeout=60
charset = UTF-8
http.tracker_http_port = 8888
http.anti_steal_token = no
http.secret_key =
tracker_server=ip:22122

3.创建FastDFSConfig.java加载fdfs_client.conf配置文件

import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.TrackerClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;

/**
 * fastDFS文件上传的配置
 */

@Configuration
public class FastDFSConfig {
    private final Logger log = LoggerFactory.getLogger(this.getClass());

    @Value("classpath:fdfs_client.conf")
    private Resource ccs;

    @Bean
    public TrackerClient initClient(){
        try{
            ClientGlobal.init(ccs.getFilename());
            return new TrackerClient();
        }catch (Exception e){
            log.info("FastDFS创建客户端失败");
            return null;
        }
    }
    
}

4.创建文件上传的Cotroller,返回的访问路径中ip是你文件服务器的ip

import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
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("/file")
public class FileController {

    private Logger log = LoggerFactory.getLogger(FileController.class);
    @Autowired
    private TrackerClient trackerClient;

    @PostMapping("/upload")
    public String uploadFile(@RequestParam("file") MultipartFile file) throws Exception{
        if(file == null){
            throw new RuntimeException("文件不能为空");
        }
        //1.获取文件的完整名称
        String filename = file.getOriginalFilename();
        if(StringUtils.isEmpty(filename)){
            throw new RuntimeException("文件不存在");
        }
        //2.获取文件的扩展名称
        String extName = filename.substring(filename.lastIndexOf(".") + 1);
        log.info("文件的全名:"+filename+"    文件的扩展名:"+extName);
        NameValuePair[] metaList = new NameValuePair[1];
        metaList[0] = new NameValuePair("fileName", filename);
        //3.创建trackerServer
        TrackerServer trackerServer = trackerClient.getConnection();
        // 4、创建一个 StorageServer 的引用,值为 null
        StorageServer storageServer = null;
        // 5、创建一个 StorageClient 对象,需要两个参数 TrackerServer 对象、StorageServer 的引用
        StorageClient storageClient = new StorageClient(trackerServer, storageServer);
        // 6、使用 StorageClient 对象上传图片。
        String[] strings = storageClient.upload_file(file.getBytes(), extName, metaList);
        return "http://ip:8888/"+strings[0]+"/"+strings[1];

    }

5.此时用postman调用你的文件上传接口,根据返回的路径在浏览器上访问,即可成功访问到你上传的文件。

相关推荐