FastDFS图片服务器的使用
一般使用java客户端进行上传图片
使用图片服务器需要相关的依赖,企业中有Maven私服,我们可以把FastDFS的java客户端依赖手动添加到Maven私服的第三方库。我们这里没有Maven私服,需要手动添加依赖,安装到本地仓库。
下载依赖工程项目:
链接:https://pan.baidu.com/s/1G2DGnrA1Wa1m2vTMFHsnvg
提取码:1ts6
添加依赖
将下载好的工程导入eclipse以后,使用make install将项目安装到本地仓库。
创建itcast-fastdfs工程进行测试
然后就可以在自己的项目POM中引入依赖。fastdfs_client工程的坐标,就是我们需要的依赖坐标
<dependency>
<groupId>fastdfs_client</groupId>
<artifactId>fastdfs_client</artifactId>
<version>1.25</version>
</dependency>
java客户端上传步骤
1、加载配置文件,配置文件中的内容就是tracker服务的地址。
配置文件内容:tracker_server=192.168.37.161:22122 (这是FastDFS服务器部署的ip、端口)
2、创建一个TrackerClient对象。直接new一个。
3、使用TrackerClient对象创建连接,获得一个TrackerServer对象。
4、创建一个StorageServer的引用,值为null
5、创建一个StorageClient对象,需要两个参数TrackerServer对象、StorageServer的引用
6、使用StorageClient对象上传图片。
7、返回数组。包含组名和图片的路径。
创建tracker.conf
内容为:
tracker_server=ip+端口
这是FastDFS服务器部署的ip、端口
编写测试代码
package com.taotao.fastdfs;
import java.io.FileNotFoundException;
import java.io.IOException;
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 TestFastDFD {
@Test
public void voloadFile() throws Exception{
// TODO Auto-generated method stub
//1.添加jar包
//2.创建一个配置文件。配置tracker服务器地址
//3.加载配置文件
//4.创建一个trackerClient对象
ClientGlobal.init("D:/eclipse-workspace/taotao-manager-web/src/main/resources/resource/client.conf");
TrackerClient trackerClient=new TrackerClient();
//5.创建一个TranClient对象获取trackServer对象
TrackerServer trackerServer=trackerClient.getConnection();
//6.创建一个storageServer的引用 null就可以
StorageServer storageServer = null;
//7.创建一个storageClient对象。trackerserver、StorageServer两个参数
StorageClient storageClient = new StorageClient(trackerServer, storageServer);
//8.使用storageClient上传文件
String[] result = storageClient.upload_file("C:/Users/Administrator/Desktop/a4.jpg", "jpg", null);
// 7. 打印返回的结果,String数组
for (String s : result) {
System.out.println(s);
}
}
}
控制台的输出路径为:
这是存入fastDFS后返回的存储路径信息,之后我们将通过这个信息去查看图片。
之前的测试代码写法比较繁琐,这里提供一个FastDFS的客户端工具类:
package com.taotao.utils;
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;
/**
*
* @Title: FastDFSClient
* @Description: TODO
* @param: @param conf fastDFS配置文件路径
* @param: @throws Exception
* @throws
*/
public FastDFSClient(String conf) throws Exception {
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 Exception
*/
public String uploadFile(String fileName, String extName, NameValuePair[] metas) throws Exception {
String result = storageClient.upload_file1(fileName, extName, metas);
return result;
}
/**
* 上传文件名
* @Title: uploadFile
* @Description: TODO
* @param: @param fileName
* @param: @return
* @param: @throws Exception
* @return: String
* @throws
*/
public String uploadFile(String fileName) throws Exception {
return uploadFile(fileName, null, null);
}
public String uploadFile(String fileName, String extName) throws Exception {
return uploadFile(fileName, extName, null);
}
/**
* 上传文件方法
* <p>Title: uploadFile</p>
* <p>Description: </p>
* @param fileContent 文件的内容,字节数组
* @param extName 文件扩展名
* @param metas 文件扩展信息
* @return
* @throws Exception
*/
public String uploadFile(byte[] fileContent, String extName, NameValuePair[] metas) throws Exception {
String result = storageClient.upload_file1(fileContent, extName, metas);
return result;
}
public String uploadFile(byte[] fileContent) throws Exception {
return uploadFile(fileContent, null, null);
}
public String uploadFile(byte[] fileContent, String extName) throws Exception {
return uploadFile(fileContent, extName, null);
}
}
工具类的调用测试:
@Test
public void test() throws Exception {
//传入配置文件名
FastDFSClient client=new FastDFSClient("D:/eclipse-workspace/taotao-manager-web/src/main/resources/resource/client.conf");
System.out.println(client.uploadFile("C:/Users/Administrator/Desktop/a5.jpg")); //传入所要上传的文件,输出返回的路径信息
}
控制台打印: