1、把FastDFS提供的jar包添加到工程中
由于我使用的maven工程管理,所以我首先需要将fastdfs_client_v1.24.jar添加到本地仓库:
添加方法详见见:
http://blog.youkuaiyun.com/qq_20565303/article/details/66971981
我这里直接在mvn中使用命令:
mvn install:install-file -Dfile=C:\fastdfs_client_v1.24.jar -DgroupId=fastdfs_client -DartifactId=fastdfs_client -Dversion=1.24 -Dpackaging=jar
注意:我这个是在win10中添加,使用win10自带的shell命令无法成功,使用cmd命令就可已成功,原因我还没有看。然后需要在项目中的pom文件中添加:
<dependency>
<groupId>fastdfs_client</groupId>
<artifactId>fastdfs_client</artifactId>
<version>1.24</version>
</dependency>
然后maven—>update下 就好了
2、初始化全局配置。加载一个配置文件。
配置文件的内容为:
tracker_server=192.168.147.130:22122
注意:这个tracker_server的值就是服务器上tracker服务配置文件里面的值。
3、创建一个FastdfsTest文件。
1)创建一个TrackerClient对象。
2)创建一个TrackerServer对象。
3)声明一个StorageServer对象,null。
4)获得StorageClient对象。
5)直接调用StorageClient对象方法上传文件即可。
文件代码如下:
package com.test.fastdfs;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 FastdfsTest {
@Test
public void testUpload() throws Exception {
// 1、把FastDFS提供的jar包添加到工程中
// 2、初始化全局配置。加载一个配置文件。
ClientGlobal.init("D:\\workspace\\yigouwu-manager\\yigouwu-manager-web\\src\\main\\resources\\properties\\client.conf");
// 3、创建一个TrackerClient对象。
TrackerClient trackerClient = new TrackerClient();
// 4、创建一个TrackerServer对象。
TrackerServer trackerServer = trackerClient.getConnection();
// 5、声明一个StorageServer对象,null。
StorageServer storageServer = null;
// 6、获得StorageClient对象。
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
// 7、直接调用StorageClient对象方法上传文件即可。
String[] strings = storageClient.upload_file("C:\\Users\\WANGTIANYUAN\\Desktop\\235113-1403260U22059[1].jpg", "jpg", null);
for (String string : strings) {
System.out.println(string);
}
}
}
其中上面的那个配置文件的路径取法:
右键点击配置文件client.conf,选择perproties,然后如图选择Location全路径:
4、测试
对FastdfsTest文件进行Junit Test执行:
然后将拿到的地址在浏览器上访问:
其中地址为:http://192.168.147.130/group1/M00/00/00/wKiTgllFXD2AArZ5AANrSBHtS0I016.jpg
5、使用fastdfs-client-java封装的工具类
工具类代码:
package com.test.fastdfs;import java.io.FileNotFoundException;
import java.io.IOException;import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;public class FastDFSClient {
private TrackerClient trackerClient = null;
private TrackerServer trackerServer = null;
private StorageServer storageServer = null;
private StorageClient1 storageClient = null;
public FastDFSClient(String conf) throws FileNotFoundException, IOException, MyException {
if(conf.contains("classpath:")){
conf = conf.replace("classpath:", this.getClass().getResource("/").getPath());
}
ClientGlobal.init(conf);
trackerClient = new TrackerClient();
trackerServer = trackerClient.getConnection();
storageServer = null;
storageClient = new StorageClient1(trackerServer, storageServer);
}
/**
* 上传文件方法
* <p>Title:uploadFile</p>
* <p>Description:</p>
* @param fileName 文件全路径
* @param extName 文件扩展名,不包含(.)
* @param metas 文件扩展信息
* @return
* @throws IOException
* @throws MyException
*/
public String uploadFile(String fileName, String extName, NameValuePair[] metas) throws IOException, MyException{
String result = storageClient.upload_file1(fileName, extName, metas);
return result;
}
public String uploadFile(String fileName, String extName) throws IOException, MyException{
return uploadFile(fileName, extName, null);
}
public String uploadFile(String fileName) throws IOException, MyException{
return uploadFile(fileName, null, null);
} /**
* 上传文件方法
* <p>Title:uploadFile</p>
* <p>Description:</p>
* @param fileContent 文件的内容,字节数组
* @param extName 文件扩展名,不包含(.)
* @param metas 文件扩展信息
* @return
* @throws IOException
* @throws MyException
*/
public String uploadFile(byte[] fileContent, String extName, NameValuePair[] metas) throws IOException, MyException{
String result = storageClient.upload_file1(fileContent, extName, metas);
return result;
}
public String uploadFile(byte[] fileContent, String extName) throws IOException, MyException{
return uploadFile(fileContent, extName, null);
}
public String uploadFile(byte[] fileContent) throws IOException, MyException{
return uploadFile(fileContent, null, null);
}
}
测试:
@Test
public void testFastDfsClient() throws Exception {
FastDFSClient client = new FastDFSClient("D:\\workspace\\yigouwu-manager\\yigouwu-manager-web\\src\\main\\resources\\properties\\client.conf");
String uploadFile = client.uploadFile("C:\\Users\\WANGTIANYUAN\\Desktop\\timg[1].jpg", "jpg");
System.out.println(uploadFile);
}
执行结果:
访问这个图片: