下载源码, 使用maven编译并安装
https://github.com/happyfish100/fastdfs-client-java.git
新建maven工程,引入fastdfs-client-java
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.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">
<modelVersion>4.0.0</modelVersion> <groupId>cn.xiaojf</groupId>
<artifactId>fastdfs-demo</artifactId>
<version>1.0-SNAPSHOT</version> <dependencies>
<dependency>
<groupId>org.csource</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.27-SNAPSHOT</version>
</dependency>
</dependencies> </project>
属性文件 fastdfs-client.properties
## 客户端连接超时时间
fastdfs.connect_timeout_in_seconds = 5
## 客户端网络超时时间
fastdfs.network_timeout_in_seconds = 30
## 编码格式
fastdfs.charset = UTF-8 fastdfs.http_anti_steal_token = false
fastdfs.http_secret_key = FastDFS1234567890
## fastdfs tracker server 的http 端口号
fastdfs.http_tracker_http_port = 80
## fastdfs tracker server的地址
fastdfs.tracker_servers = 192.168.1.19:22122
上传
package cn.xiaojf.fastdfs; import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*; import java.io.File;
import java.io.IOException; /**
* 上传
* @author xiaojf 2017/6/28 15:59
*/
public class UploadDemo {
private static StorageClient1 client = null;
static {
try {
ClientGlobal.initByProperties("fastdfs-client.properties");
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getConnection(); if (trackerServer == null) {
throw new RuntimeException("获取Tracker server 发生异常");
} StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);
client = new StorageClient1(trackerServer,storageServer);
} catch (IOException e) {
e.printStackTrace();
} catch (MyException e) {
e.printStackTrace();
}
} /**
* 上传
* @param file 文件
* @author xiaojf 2017/6/28 16:37
*/
public static String upload(File file) throws IOException, MyException {
//指定额外的存储信息
NameValuePair[] nameValuePairs = new NameValuePair[1];
nameValuePairs[0] = new NameValuePair("filename",file.getName());
//上传文件
if (client == null) {
throw new RuntimeException("fastdfs 客户端未初始化");
}
return client.upload_file1(file.getAbsolutePath(), "exe", nameValuePairs);
} public static void main(String[] args) throws IOException, MyException {
File file = new File("c:/windows/system32/notepad.exe");
System.out.println(upload(file));
}
}
下载
package cn.xiaojf.fastdfs; import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*; import java.io.IOException; /**
* 下载
* @author xiaojf 2017/6/28 15:59
*/
public class DownloadDemo {
private static StorageClient1 client = null;
static {
try {
ClientGlobal.initByProperties("fastdfs-client.properties");
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getConnection(); if (trackerServer == null) {
throw new RuntimeException("获取Tracker server 发生异常");
} StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);
client = new StorageClient1(trackerServer,storageServer);
} catch (IOException e) {
e.printStackTrace();
} catch (MyException e) {
e.printStackTrace();
}
} /**
* 下载
* @param token fastdfs 上传后返回的字符串
* @author xiaojf 2017/6/28 16:37
*/
public static byte[] download(String token) throws IOException, MyException {
//获取上传时候,写入的文件信息
NameValuePair[] metadata1 = client.get_metadata1(token);
for (NameValuePair nameValuePair : metadata1) {
System.out.println(nameValuePair.getName() + " -> " + nameValuePair.getValue());
} if (client == null) {
throw new RuntimeException("fastdfs 客户端未初始化");
} //下载,返回文件字节数组
return client.download_file1(token);
} public static void main(String[] args) throws IOException, MyException {
System.out.println(DownloadDemo.download("group1/M00/00/00/wKgBE1lTagmAXsRVAAL0ADCuSWA576.exe").length);
}
}