Springboot integrated fastDFS

Configuration file

Configure the fdfs_client.conf configuration file

# 连接超时时间,针对socket套接字函数connect,默认为30秒
connect_timeout=30000
# 网络通讯超时时间,默认是60秒
network_timeout=60000
tracker_server = 47.98.159.15:22122

Import dependencies

<dependency>
            <groupId>org.csource</groupId>
            <artifactId>fastdfs-client-java</artifactId>
            <version>1.27-SNAPSHOT</version>
        </dependency>

Configure the client

package panda.data.Utils;

import org.csource.common.MyException;
import org.csource.fastdfs.*;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.stream.Stream;

/**
 * @Auther: 罗罗
 * @Description:
 */
public class FastDFSClient {
    private static org.slf4j.Logger logger = LoggerFactory.getLogger(FastDFSClient.class);

    public static String uploadImage(MultipartFile multipartFile) throws IOException, MyException {
        // 1、初始化全局配置。加载一个配置文件。
        String filePath = new ClassPathResource("fdfs_client.conf").getFile().getAbsolutePath();
        ClientGlobal.init(filePath);

        // 2、创建一个TrackerClient对象。
        TrackerClient trackerClient = new TrackerClient();
        // 3、创建一个TrackerServer对象。
        TrackerServer trackerServer = trackerClient.getConnection();
        // 4、声明一个StorageServer对象,null。
        StorageServer storageServer = null;
        // 5、获得StorageClient对象。
        StorageClient storageClient = new StorageClient(trackerServer, storageServer);
        byte[] bytes = multipartFile.getBytes();// 获得上传的二进制对象
        String[] strings = storageClient.upload_file(bytes, "jpg", null);
        Stream.of(strings).forEach(System.out::println);
        String imagurl = "http://47.98.159.35/"+strings[0]+"/"+strings[1];
        //返回值0代表删除成功
        //int result = storageClient.delete_file("group1", "M00/00/00/wKgAaFyM3fSANCgXAABMnNm0e54098.jpg");
        //System.out.println("result="+result);
        return imagurl;
    }
}
package panda.data.controller;

import org.csource.common.MyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import panda.data.ResultData.ImgAttr;
import panda.data.ResultData.ResultData;
import panda.data.Utils.FastDFSClient;

import java.io.IOException;
import java.util.ArrayList;

@Controller
public class ImgController {

    @Autowired
    private ResultData resultData;
    //必须是post方式
    @PostMapping("/fileUpload")
    @ResponseBody
    public ResultData fileUpload(@RequestParam("file") MultipartFile multipartFile) throws IOException, MyException {
        String url = FastDFSClient.uploadImage(multipartFile);
        if (url.contains("group")){
            resultData.setCode(0);
            ImgAttr imgAttr = new ImgAttr(url);
            ArrayList<Object> list = new ArrayList<>();
            list.add(imgAttr);
            resultData.setData(list);
        }else {
            resultData.setCode(0);
            resultData.setData(null);
        }
        return resultData;
    }
}

end

This is the end of this article. Thank you for seeing the last friends. They are all seen at the end. Just like it before leaving. If there is something wrong, please correct me.

Guess you like

Origin blog.51cto.com/14969174/2544453