FastDFS的工具类

package cn.travelround.common.fdfs;

import org.apache.commons.io.FilenameUtils;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import org.springframework.core.io.ClassPathResource;

/**

  • 上传图片到Fast

  • @author lx
    */
    public class FastDFSUtils {

    // 上传文件, 并返回在Storage中的存储路径
    public static String uploadPic(byte[] pic, String name, long size) {
    String path = null;
    // ClassPath会自动搜寻配置文件位置
    ClassPathResource resource = new ClassPathResource(“fdfs_client.conf”);
    try {
    // 读取配置文件, 但直接写名字找不到, 所以需要创建上一句ClassPathResource的对象帮忙搜寻路径
    ClientGlobal.init(resource.getClassLoader().getResource(“fdfs_client.conf”).getPath());
    // 创建Tracker老大并连接
    TrackerClient trackerClient = new TrackerClient();
    TrackerServer trackerServer = trackerClient.getConnection();

         // 创建Storage小弟
         StorageClient1 storageClient1 = new StorageClient1(trackerServer, null);
    
         // 创建storageClient1的意义就在于调用upload_file1来上传文件, 以下几句是为了upload_file1方法准备参数
         // 从文件的全名中截取扩展名
         String ext = FilenameUtils.getExtension(name);
         NameValuePair[] meta_list = new NameValuePair[3];   // key名自己起, 顺序也随意
         meta_list[0] = new NameValuePair("fileName", name); // 文件名
         meta_list[1] = new NameValuePair("fileExt", ext);   // 扩展名
         meta_list[2] = new NameValuePair("fileSize", String.valueOf(size)); // 文件长度
    
         // 上传文件
         // 参数1: 上传文件的内容  类型byte[]
         // 参数2: 上传文件扩展名  类型String
         // 参数3: 上传文件的信息  类型NameValuePair[]  数组  -  本次添加了 文件名,扩展名,文件长度等信息
         // 返回值: 文件上传后, 在小弟Storage电脑中的存储路径 - 如:  group1/M00/00/01/wKjIgFWOYc6APpjAAAD-qk29i78248.jpg
         path = storageClient1.upload_file1(pic, ext, meta_list);
         System.out.println("bbb" + path);
     } catch (Exception e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
     }
     return path;
    

    }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值