一、获取FastDFS Java的客户端链接工具

由于maven库中并没有编译好的jar工具包,目前只能通过GitHub下载源码自己打包成jar
下载地址:点击去下去

如果通过浏览器直接下载,则下载后是一个zip压缩包。
1.解压zip包
2.eclipse通过已存在的maven项目方式导入
3.执行maven install命令打包

打包完成后再target目录下有打包好的jar文件:
fastdfs-client-java-1.27-SNAPSHOT.jar
 

二、导入spring boot项目

创建一个spring boot项目,在创建好的项目中创建一个lib的文件夹,将上面打包的jar文件复制进去。然后打开spring boot项目的pom.xml文件,添加本地依赖如下:
		<!-- fastfds 客户端 |https://github.com/happyfish100/fastdfs-client-java -->
		<dependency>
			<groupId>org.csource</groupId>
			<artifactId>fastdfs-client-java</artifactId>
			<version>1.27-SNAPSHOT</version>
			<scope>system</scope>
			<systemPath>${project.basedir}/lib/fastdfs-client-java-1.27-SNAPSHOT.jar</systemPath>
		</dependency>

三、配置文件

在spring boot项目的resource目录下创建一个fdfs_client.conf文件,内容如下:
#注1:tracker_server指向您自己IP地址和端口,1-n个
#注2:除了tracker_server,其它配置项都是可选的
#注3:.conf 配置文件文件所在位置可以是项目classpath(或OS文件系统目录比如/opt/):
#注4:.conf 配置文件优先按OS文件系统路径读取,没有找到才查找项目classpath,尤其针对linux环境下的相对路径
#注5:其他相关参考:https://github.com/happyfish100/fastdfs-client-java
connect_timeout = 120
network_timeout = 130
charset = UTF-8
http.tracker_http_port = 80
http.anti_steal_token = no
http.secret_key = FastDFS1234567890
tracker_server = 192.168.8.202:22122
其中里面的tracker_server 以及端口均需要根据自身使用的实际请来修改


四、编写一个公用的Java 的fastdfs客户端

首先需要创建一个FastDFS的文件辅助类:
FastDSFile.java
public class FastDSFile {
	private String name;

	private byte[] content;

	private String ext;

	private String md5;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public byte[] getContent() {
		return content;
	}

	public void setContent(byte[] content) {
		this.content = content;
	}

	public String getExt() {
		return ext;
	}

	public void setExt(String ext) {
		this.ext = ext;
	}

	public String getMd5() {
		return md5;
	}

	public void setMd5(String md5) {
		this.md5 = md5;
	}

}

FastDFSClient.java
import java.io.File;
import java.io.IOException;

import org.csource.common.MyException;
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;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import com.alibaba.fastjson.JSONArray;

/**
 * fastDFS文件服务Java客户端实现,所有执行方法均为静态方法。
 * 
 * @author xq
 *
 */
public class FastDFSClient {

	/**
	 * 客户端
	 */
	private static StorageClient1 storageClient1 = null;

	// 初始化客户端,加载类时候执行片段
	static {
		try {
			Resource resource = new ClassPathResource("fdfs_client.conf");
			File file = resource.getFile();
			String configFile = file.getAbsolutePath();

			ClientGlobal.init(configFile);
			//
			TrackerClient trackerClient = new TrackerClient(ClientGlobal.g_tracker_group);
			//
			TrackerServer trackerServer = trackerClient.getConnection();
			//
			StorageServer storageServer = trackerClient.getStoreStorage(trackerServer);
			//
			storageClient1 = new StorageClient1(trackerServer, storageServer);
			System.out.println("FastDFS Client Init Success!");
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("FastDFS Client Init Fail!");
		}
	}

	/***
	 * 文件上传
	 * 
	 * @param fastDSFile
	 * @return
	 * @throws IOException
	 * @throws MyException
	 */
	public static JSONArray upload(FastDSFile fastDSFile) throws IOException, MyException {
		String[] uploadResult = storageClient1.upload_file(fastDSFile.getContent(), fastDSFile.getExt(), null);
		// String arr = JSONArray.toJSONString(uploadResult);
		JSONArray arr = (JSONArray) JSONArray.toJSON(uploadResult);
		return arr;
	}

	/**
	 * 文件下载
	 * 
	 * @param groupName
	 * @param remoteFileName
	 * @return
	 * @throws IOException
	 * @throws MyException
	 */
	public static byte[] download(String groupName, String remoteFileName) throws IOException, MyException {
		return storageClient1.download_file(groupName, remoteFileName);
	}

	/**
	 * 文件删除
	 * 
	 * @param groupName
	 * @param remoteFileName
	 * @throws Exception
	 * @return 返回0成功;非0失败.
	 */
	public static int delete(String groupName, String remoteFileName) throws Exception {
		return storageClient1.delete_file(groupName, remoteFileName);
	}
}

五、编写测试

DemoSpringbootFastdfsApplicationTests.java
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import com.alibaba.fastjson.JSONArray;

import net.xqlee.project.demo.fastdfs.clients.FastDFSClient;
import net.xqlee.project.demo.fastdfs.clients.FastDSFile;

@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoSpringbootFastdfsApplicationTests {

	@Test
	public void contextLoads() {
		try {
			FileInputStream fis = new FileInputStream(new File("C:/Users/xq/Pictures/tx.jpg"));
			ByteArrayOutputStream bos = new ByteArrayOutputStream();

			byte[] cache = new byte[4096];
			while (fis.read(cache) != -1) {
				bos.write(cache);
			}
			fis.close();
			FastDSFile fastDSFile = new FastDSFile();
			fastDSFile.setContent(bos.toByteArray());
			fastDSFile.setExt("jpg");

			// -------上传----
			JSONArray rs = FastDFSClient.upload(fastDSFile);
			System.out.println("上传结束:" + rs);

			// -------下载----
			byte[] dfile = FastDFSClient.download(rs.getString(0), rs.getString(1));

			FileOutputStream fos = new FileOutputStream(new File("C:/Users/xq/Pictures/tx-fdfs.jpg"));
			fos.write(dfile);
			fos.flush();
			fos.close();
			
			// -------删除-----
			int ds=FastDFSClient.delete(rs.getString(0), rs.getString(1));
			//
			System.out.println("Delete:"+ds);
			System.out.println("---End----");

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}

运行测试,可以看到在上传图片的目录下多了一个名为tx-fdfs.jpg的文件
暂无评论