FastDFS文件的使用


maven项目进行测试上传

导入以下依赖:



cn.bestwu
fastdfs-client-java
1.27


org.apache.commons
commons-lang3
3.4



代码如下:
由一个配置文件需要进行修改不然和tomcat冲突
vi /etc/fdfs/tracker.conf
修改下面这个
http.server_port=7000
test.java
package com.yongyuankuaile;

import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;

import java.io.FileInputStream;
import java.util.Properties;

public class test {
    private static String dirpath="D:\\";
    public static void main(String[] args)  throws Exception{
        upload();
    }
    //上传
    public static void upload() throws Exception{
        //加载配置文件,内容是tracker的ip和端口。还有链接的设置,如:超时
        //fdfs的java客户端提供了2中配置文件格式,1:conf 2,properties
        Properties properties=new Properties();
        properties.load(test.class.getClassLoader().getResourceAsStream("fdfs.properties"));
        ClientGlobal.initByProperties(properties);
        //初始化客户端对象,StorageClient,存储客户端
        TrackerClient client = new TrackerClient();
        TrackerServer trackerServer= client.getConnection();
        StorageServer storageServer = client.getStoreStorage(trackerServer);
        StorageClient storageClient = new StorageClient(trackerServer, storageServer);
        //文件上传
        //读取文件字节内容到一个字节数组中,一个文件必须完整的存在一个数组中。
        FileInputStream in = new FileInputStream(dirpath + "1.jpg");
        //根据文件的字节长度,创建一个等长的数组
        byte[] datas = new byte[in.available()];
        //把文件内容完整的读取到数组中
        in.read(datas,0,datas.length);
        //元数据,有使用者自定义,是名值对形式存在的数组。如:文件名=1.jpg,文件长度=11111等。
        NameValuePair[]nvps=new NameValuePair[]{
          new NameValuePair("name","1.jpg")
        };
        //上传文件,返回一个字符串数据,长度为2,0下标代表组名,1下标代表文件路径及文件名
        //如:【0】=group1; [1]=M00/00/00/sadifohadsfhgo.jpg
        String[] result = storageClient.upload_file(datas, "jpg", nvps);
        //处理上传结果,就是返回的结果,如: group1/M00/00/00/XXX,jpg
        System.out.println("返回结果数组长度:"+result.length);
        System.out.println("result[0]="+result[0]);
        System.out.println("result[1]="+result[1]);
    }
}

fdfs.properties

#建立连接时间10秒(连接超时)
fastdfs.connect_timeout_in_seconds=10
#上传文件30秒(工作超时)
fastdfs.network_timeout_in_seconds=30
#字符集
fastdfs.charset=UTF-8
#servers服务器(tracker服务器地址,多个地址使用','分隔)
fastdfs.tracker_servers=192.168.33.201:22122
#和配置文件/etc/fdfs/tracker.conf中的http.server_port配置完全相同
fastdfs.http_tracker_http_port=7000

 封装工具类

创建utils包

import com.yongyuankuaile.test.Test;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;

import java.util.Properties;

public class FastDFSUtils {
    private FastDFSUtils(){}
    private static final Properties properties=new Properties();
    private static  StorageClient storageClient;
    static {
        try {
            properties.load(Test.class.getClassLoader().getResourceAsStream("fdfs.properties"));
            ClientGlobal.initByProperties(properties);
            //初始化客户端对象,StorageClient,存储客户端
            TrackerClient client = new TrackerClient();
            TrackerServer trackerServer= client.getConnection();
            StorageServer storageServer = client.getStoreStorage(trackerServer);
            storageClient = new StorageClient(trackerServer, storageServer);
        }catch (Exception e){
            e.printStackTrace();
            //初始化代码块异常,抛出异常,停止虚拟机
            throw  new ExceptionInInitializerError(e);
        }
    }
    public static String uploadFile(byte[]datas, String extName, NameValuePair[]nvps){
        try {
            String[] result = storageClient.upload_file(datas, extName, nvps);
            String path=result[0]+"/"+result[1];
            //上传成功
            return path;
        } catch (Exception e) {
            e.printStackTrace();
            //上传失败
           return null;
        }
    }
}

  Test.java

package com.yongyuankuaile.test;
import com.yongyuankuaile.utils.FastDFSUtils;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;

import java.io.FileInputStream;
import java.util.Properties;
public class Test {
    private static String dirpath="D:\\";
    public static void main(String[] args)  throws Exception{
        upload1();
    }
    public static void upload1()throws Exception{
        FileInputStream in = new FileInputStream(dirpath + "1.jpg");
        int length=in.available();
        byte[]datas=new byte[length];
        in.read(datas,0,length);
        NameValuePair[]nvps=new NameValuePair[]{
          new NameValuePair("name","1.jpg"),
          new NameValuePair("length",length+"")
        };
        String result = FastDFSUtils.uploadFile(datas, "jpg", nvps);
        System.out.println(result);
    }
   /* //上传
    public static void upload() throws Exception{
        //加载配置文件,内容是tracker的ip和端口。还有链接的设置,如:超时
        //fdfs的java客户端提供了2中配置文件格式,1:conf 2,properties
        Properties properties=new Properties();
        properties.load(Test.class.getClassLoader().getResourceAsStream("fdfs.properties"));
        ClientGlobal.initByProperties(properties);
        //初始化客户端对象,StorageClient,存储客户端
        TrackerClient client = new TrackerClient();
        TrackerServer trackerServer= client.getConnection();
        StorageServer storageServer = client.getStoreStorage(trackerServer);
        StorageClient storageClient = new StorageClient(trackerServer, storageServer);
        //文件上传
        //读取文件字节内容到一个字节数组中,一个文件必须完整的存在一个数组中。
        FileInputStream in = new FileInputStream(dirpath + "1.jpg");
        //根据文件的字节长度,创建一个等长的数组
        byte[] datas = new byte[in.available()];
        //把文件内容完整的读取到数组中
        in.read(datas,0,datas.length);
        //元数据,有使用者自定义,是名值对形式存在的数组。如:文件名=1.jpg,文件长度=11111等。
        NameValuePair[]nvps=new NameValuePair[]{
                new NameValuePair("name","1.jpg")
        };
        //上传文件,返回一个字符串数据,长度为2,0下标代表组名,1下标代表文件路径及文件名
        //如:【0】=group1; [1]=M00/00/00/sadifohadsfhgo.jpg
        String[] result = storageClient.upload_file(datas, "jpg", nvps);
        //处理上传结果,就是返回的结果,如: group1/M00/00/00/XXX,jpg
        System.out.println("返回结果数组长度:"+result.length);
        System.out.println("result[0]="+result[0]);
        System.out.println("result[1]="+result[1]);
    }*/
}