Java fastdfs客户端实现上传下载文件

网友投稿 497 2022-11-18


Java fastdfs客户端实现上传下载文件

一、项目结构

二、pom.xml

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

org.example

A01fastdfs

1.0-SNAPSHOT

net.oschina.zcx7878

fastdfs-client-java

1.27.0.0

junit

junit

test

org.apache.commons

commons-io

1.3.2

org.apache.maven.plugins

maven-compiler-plugin

1.9

1.9

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

org.example

A01fastdfs

1.0-SNAPSHOT

net.oschina.zcx7878

fastdfs-client-java

1.27.0.0

junit

junit

test

org.apache.commons

commons-io

1.3.2

org.apache.maven.plugins

maven-compiler-plugin

1.9

1.9

三、fastdfs-client.properties

#http连接超时时间

fastdfs.connect_timeout_in_seconds=5

#tracker和storage网络通信超时时间

fastdfs.network_timeout_in_seconds=30

#字符编码

fastdfs.charset=utf-8

#tracker服务器地址,多个地址中间用英文逗号分隔

fastdfs.tracker_servers=192.168.2.105:22122

四、测试

package com.wuxi.test;

import org.csource.fastdfs.*;

import org.junit.Test;

import java.io.File;

import java.io.FileOutputStream;

public class MyTest {

//上传文件

@Test

public void testUpload() {

try {

//加载fastdfs-client.properties配置文件

ClientGlobal.initByProperties("config/fastdfs-client.properties");

//定义TrackerClient,用于请求TrackerServer

TrackerClient trackerClient = new TrackerClient();

//连接tracker

TrackerServer trackerServer = trackerClient.getConnection();

//获取storage

StorageServer storeStorage = trackerClient.getStoreStorage(trackerServer);

//创建storageClient

StorageClient1 storageClient1 = new StorageClient1(trackerServer, storeStorage);

//向storage服务器上传文件

//本地文件的路径

String path = "F:/java/resource/data.txt";

//上传成功后拿到文件Id

String fileId = storageClient1.upload_file1(path, "txt", null);

System.out.println(fileId);//group1/M00/00/00/wKgCaV9vaSaARBTKAAAAGjJpL2g017.txt

} catch (Exception e) {

e.printStackTrace();

}

}

//下载文件

@Test

public void testDownload() {

try {

//加载fastdfs-client.properties配置文件

ClientGlobal.initByProperties("config/fastdfs-client.properties");

//定义TrackerClient,用于请求TrackerServer

TrackerClient trackerClient = new TrackerClient();

//连接tracker

TrackerServer trackerServer = trackerClient.getConnection();

//获取storage

StorageServer storeStorage = trackerClient.getStoreStorage(trackerServer);

//创建storageClient

StorageClient1 storageClient1 = new StorageClient1(trackerServer, storeStorage);

//下载文件

//文件id

String fileId = "group1/M00/00/00/wKgCaV9vaSaARBTKAAAAGjJpL2g017.txt";

byte[] bytes = storageClient1.download_file1(fileId);

//使用输出流保存文件

FileOutputStream fileOutputStream = new FileOutputStream(new File("F:/data.txt"));

fileOutputStream.write(bytes);

} catch (Exception e) {

e.printStackTrace();

}

}

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Springmvc异常处理器及拦截器实现代码
下一篇:SpringBoot2.x 之整合 thumbnailator 图片处理的示例代码
相关文章

 发表评论

暂时没有评论,来抢沙发吧~