FastDFS文件系统(java客户端使用)
一、导包(注意:maven中央仓库并没有这个依赖,需要自行寻找资源并安装到本地maven仓库,才能正常使用下方的依赖)
<!--FastDFS--> <dependency> <groupId>fastdfs_client</groupId> <artifactId>fastdfs_client</artifactId> <version>1.25</version> </dependency>
二、测试方法(代码)
package cn.e3mall.fileUpload; import org.csource.common.MyException; import org.csource.fastdfs.*; import org.junit.Test; import java.io.IOException; /** * 测试文件上传 * FastDFS(java客户端使用) * Author: xushuai * Date: 2018/5/14 * Time: 19:53 * Description: */ public class FileUploadTest { /* * 步骤: * 1、使用ClientGlobal的静态方法加载Tracker服务器配置信息(注意:必须为全路径) * 2、创建TrackerClient对象 * 3、使用TrackerClient获取TrackerServer对象 * 4、创建StorageServer对象,引用为null即可 * 5、使用TrackerServer和StorageServer构造StorageClient对象 * 6、使用StorageClient进行文件上传 * 7、返回的字符串数组为上传文件在服务器中所在位置以及文件名 */ @Test public void fileUpload() throws IOException, MyException { //加载连接信息(即FastDFS中tracker服务器IP) ClientGlobal.init("E:\\workspace\\idea\\e3-parent\\e3-manager-web\\src\\main\\resources\\other\\file-client.conf"); //创建TrackerClient对象\ TrackerClient trackerClient = new TrackerClient(); //获取TrackerServer对象 TrackerServer trackerServer = trackerClient.getConnection(); //创建StorageServer对象,引用为空 StorageServer storageServer = null; //使用TrackerServer和StorageServer构造StorageClient对象 StorageClient storageClient = new StorageClient(trackerServer, storageServer); //使用storageClient上传文件到服务器 String[] strings = storageClient.upload_file("C:\\Users\\Administrator\\Desktop\\59c8c013Nda2b12a6.jpg", "jpg", null); //上传成功会返回一个字符数组,分别为:文件所在组和文件在组中的位置及名称 for(String s:strings){ System.out.println(s); } } }
运行结果:
使用浏览器访问刚才上传到服务器的图片:
FastDFS工具类的封装及其使用
一、工具类(代码)
package cn.e3mall.common.entity; 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 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; } 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); } }
二、工具类的使用(代码)
/** * FastDFS工具类使用 * @auther: xushuai * @date: 2018/5/14 20:21 * @return: * @throws: */ @Test public void fileUploadUtil() throws Exception { //使用Tracker服务器信息配置文件位置构造工具类对象 FastDFSClient util = new FastDFSClient("E:\\workspace\\idea\\e3-parent\\e3-manager-web\\src\\main\\resources\\other\\file-client.conf"); //上传文件到文件服务器 String s = util.uploadFile("C:\\Users\\Administrator\\Desktop\\0049045698345890_b.jpg"); //打印文件在服务器所在位置 System.out.print(s); }
运行结果:
使用浏览器访问刚才上传到服务器的图片:
![](https://i-blog.csdnimg.cn/blog_migrate/1085f1832d8a3647bd13c9aeaac707a6.png)