SpringBoot成长笔记(七)集成fastdfs

环境

开发环境:参考SpringBoot成长笔记(一)环境搭建
pom.xml

<!--fastdfs 20181105-->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastdfs-client</artifactId>
			<version>1.27</version>
		</dependency>

配置文件

# 连接tracker服务器超时时长  
connect_timeout = 10  
# socket连接超时时长  
network_timeout = 30  
# 文件内容编码  
charset = UTF-8  
# tracker服务器端口  
http.tracker_http_port = 6666
http.anti_steal_token = no  
http.secret_key = FastDFS1234567890  
# tracker服务器IP和端口(可以写多个)  
tracker_server = 10.45.157.180:22122

代码

配置类

package com.mhm.fastdfs;

import org.csource.fastdfs.*;

/**
 * fastdfs配置类
 * Created by MHm on 2018/8/7.
 */
public class FastDfsConfig {

    public StorageClient1 initStorageClient()
    {
        StorageClient1 storageClient = null;
        try
        {
            ClientGlobal.init("fdfs-client.properties");
            TrackerClient trackerClient = new TrackerClient(ClientGlobal.g_tracker_group);
            TrackerServer trackerServer = trackerClient.getConnection();
        if (trackerServer == null)
        {
            throw new IllegalStateException("getConnection return null");
        }
        StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);
        if (storageServer == null)
        {
            throw new IllegalStateException("getStoreStorage return null");
        }
        storageClient = new StorageClient1(trackerServer, storageServer);
    }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return storageClient;
    }
}

客户端

package com.mhm.fastdfs;

import com.mhm.util.FileUtil;
import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.FileInfo;
import org.csource.fastdfs.StorageClient1;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * 工具类
 * Created by MHm on 2018/8/7.
 */
public class FastDfsClient {

    private StorageClient1 storageClient;

    public FastDfsClient(){
        storageClient = new StorageClient1();
    }


    /**
     * 上传文件
     *
     * @param buff
     *            文件对象
     * @param fileName
     *            文件名
     * @return
     */
    public String uploadFile(byte[] buff, String fileName)
    {
        return uploadFile(buff, fileName, null);
    }


    /**
     * 上传文件
     *
     * @param buff
     *            文件对象
     * @param fileexName
     *            文件名
     * @param metaList
     *            文件元数据
     * @return
     */
    public String uploadFile(byte[] buff, String fileexName, Map<String, String> metaList)
    {
        try
        {
            NameValuePair[] nameValuePairs = null;
            if (metaList != null)
            {
                nameValuePairs = new NameValuePair[metaList.size()];
                int index = 0;
                for (Iterator<Map.Entry<String, String>> iterator = metaList.entrySet().iterator(); iterator.hasNext();)
                {
                    Map.Entry<String, String> entry = iterator.next();
                    String name = entry.getKey();
                    String value = entry.getValue();
                    nameValuePairs[index++ ] = new NameValuePair(name, value);
                }
            }
            return storageClient.upload_file1(buff,fileexName,
            nameValuePairs);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取文件元数据
     *
     * @param fileId
     *            文件ID
     * @return
     */
    public Map<String, String> getFileMetadata(String fileId)
    {

        try
        {
            NameValuePair[] metaList = storageClient.get_metadata1(fileId);
            if (metaList != null)
            {
                HashMap<String, String> map = new HashMap<String, String>();
                for (NameValuePair metaItem : metaList)
                {
                    map.put(metaItem.getName(), metaItem.getValue());
                }
                return map;
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 删除文件
     *
     * @param fileId
     *            文件ID
     * @return 删除失败返回-1,否则返回0
     */
    public int deleteFile(String fileId)
    {
        try
        {
            return storageClient.delete_file1(fileId);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return -1;
    }

    /**
     * 下载文件
     *
     * @param fileId
     *            文件ID(上传文件成功后返回的ID)
     * @return
     */
    public byte[] downloadFile(String fileId)
    {
        try
        {
            byte[] content = storageClient.download_file1(fileId);
            return content;
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        catch (MyException e)
        {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * Description:获取文件信息
     *
     * @param fileId
     * @return
     * @see
     */
    public FileInfo getFileInfo(String fileId)
    {

        try
        {
            FileInfo fileInfo = storageClient.get_file_info1(fileId);
            return fileInfo;
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        catch (MyException e)
        {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        FastDfsConfig fastdfsConfig = new FastDfsConfig();
        fastdfsConfig.initStorageClient();
        FastDfsClient client = new FastDfsClient();
        System.out.println(client.uploadFile(FileUtil.readImgFile2Bytes("C:\\Users\\28956\\Pictures\\1.jpg"),"jpg"));
        System.out.println(client.getFileInfo("group1/M00/00/00/Ci2dtFtr82aAbwhVAAOSRD-r3sY92.pic1"));
    }

}

运行

直接运行客户端的main函数,直接上传fastdfs。
在这里插入图片描述

github

github

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值