FastDFS图片服务器java客户端的使用

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")); //传入所要上传的文件,输出返回的路径信息
	
	}

控制台打印:
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

收破烂的小熊猫~

你的鼓励将是我创造最大的东西~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值