spring boot FastDFS Java client使用-Java编程

编程教程 > Java > Spring (13839) 2024-11-26 14:39:04

一、获取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的文件


评论
User Image
提示:请评论与当前内容相关的回复,广告、推广或无关内容将被删除。

相关文章
Java编程之spring boot FastDFS Java client使用,Java编程,FastDFS Java客户端
获取客户端来源真实ip在很多业务场景都需要,比如微信支付的v2版,要求必须有下单的用户来源IP。下面详解。 从Java代码端获取真实客户端IP源码参考: pub
Java编程中Spring Boot整合RabbitMQ实现消息中间件RabbitMQ的使用 1 主要用spring-boot-starter-amqp来整合R
Java编程中spring boot项目如何获取spring容器applicationContext
Java编程之Spring Boot通过JMSTemplate 整合ActiveMQ
一.去activemq官网下载mq软件去Apache官网下载activemq软件,并安装。二.编写Java代码java编程中spring boot整合activ
Java编程中spring boot项目动态添加拦截器Interceptor
spring boot 入门之security oauth2 jwt完美整合例子,Java编程中spring boot框架+spring security框架+spring security o...
Java编程之spring boot shiro redis整合基于角色和权限的安全管理,Java编程,spring boot,shiro,权限控制
java编程为啥会出现spring框架,为什么要有Spring?
spring security常用注解@Secured、@PreAuthorize 、@PostAuthorize说明,Java编程,spring security
本文主要翻译spring官方的基于spring security框架的oauth2开发指南,spring,oauth2,spring框架,Java编程
Java编程中spring mvc 获取请求ip,springmvc 获取访问ip
spring boot整合cxf发布webservice服务和cxf客户端调用,说起web service最近几年restful大行其道,大有取代传统soap web service的趋势,但是...
Java编程中spring security4是一个spring框架项目的一个安全方面的项目。主要用于用户认证,授权,角色认证