FastDFS上传文件的简单方法
FastDFS上传文件的简单方法:
1.创建配置文件(.conf),内包含TrackerServer的地址和端口;
2.使用Global工具类的init方法加载该配置文件(填写绝对路径);
3.创建TrackClient对象;
4.通过TrackerClient对象的getConnection方法获取TrackerServer对象;
5.创建一个StorageServer的引用(不new());
6.将TracServer和StorageServer作为构造参数创建StorageClient对象;
7.用StorageClient对象的upload_file方法上传文件,该方法会返回一个字符数组,将每个元素用/拼接起来前面加上Tracker服务器的地址,即可访问该文件。
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;
import org.csource.common.MyException;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.junit.Test;
public class TestFastDFS {
@Test
public void test() throws FileNotFoundException, IOException, MyException
{
ClientGlobal.init("D:\\WorkSpace\\Store-web\\src\\main\\resources\\config\\client.conf");
TrackerClient trackerClient = new TrackerClient();
TrackerServer Trackerserver = trackerClient.getConnection();
StorageServer storageServer = null;
StorageClient storageClient = new StorageClient(Trackerserver,storageServer);
String[] res = storageClient.upload_file("D:\\5.jpg", "jpg", null);
System.out.println(Arrays.toString(res));
}
public void TestFastDFSUtil()
{
}
}
浙公网安备 33010602011771号