FastDFS学习笔记 -- day05 上传图片

一、图片上传测试准备工作

1、导入jar包或者使用jar生成pom依赖
2、在工程中创建配置文件



二、使用demo上传图片

1、编写上传图片的代码

package test;

import java.net.URLDecoder;

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.junit.Test;

public class TestFdfs {

	private String file_ext_name = ".jpg";
	private String local_filename = "G://temp//1.jpg";
	@Test
	public void test() throws Exception{
		//获取fdfs_client配置文件路径
		String confPath = this.getClass().getResource("/fdfs/fdfs_client.conf").getPath();
		//如果路径中有中文,需要处理中文乱码问题
		confPath = URLDecoder.decode(confPath,"utf-8");
		//加载fdfs_client配置文件
		ClientGlobal.init(confPath);
		//创建TrackerClient
		TrackerClient trackerClient = new TrackerClient();
		//创建TrackerServer
		TrackerServer trackerServer = trackerClient.getConnection();
		//创建storageServer
		StorageServer storageServer = null;
		//创建StorageClient1,客户端文件上传时,可以指定上传的服务期地址,也可以有trackerServer调度
		StorageClient1 client1 = new StorageClient1(trackerServer,storageServer);
		//调用StorageClient1的api方法完成文件的上传操作
		String upload_file1 = client1.upload_file1(local_filename, file_ext_name, null);
		//输出上传文件的file_id
		System.out.println(upload_file1);
	}
}
4、浏览器测试


三、使用自定义工具类进行上传

1、导入工具类

package cn.e3mall.manager.utils;

import java.net.URLDecoder;

import org.apache.log4j.Logger;
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 static Logger logger = Logger.getLogger(FastDFSClient.class);

	private static TrackerClient trackerClient = null;
	private static TrackerServer trackerServer = null;
	private static StorageServer storageServer = null;
	private static StorageClient1 client = null;
	// fdfsclient的配置文件的路径
	private static String CONF_NAME = "/fdfs/fdfs_client.conf";

	static {
		try {
			// 配置文件必须指定全路径
			String confName = FastDFSClient.class.getResource(CONF_NAME).getPath();
			// 配置文件全路径中如果有中文,需要进行utf8转码
			confName = URLDecoder.decode(confName, "utf8");

			ClientGlobal.init(confName);
			trackerClient = new TrackerClient();
			trackerServer = trackerClient.getConnection();
			storageServer = null;
			client = new StorageClient1(trackerServer, storageServer);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 上传文件方法
	 * <p>
	 * Title: uploadFile
	 * </p>
	 * <p>
	 * Description:
	 * </p>
	 * 
	 * @param fileName
	 *            文件全路径
	 * @param extName
	 *            文件扩展名,不包含(.)
	 * @param metas
	 *            文件扩展信息
	 * @return
	 * @throws Exception
	 */
	public static String uploadFile(String fileName, String extName, NameValuePair[] metas) throws Exception {
		String result = client.upload_file1(fileName, extName, metas);
		return result;
	}

	public static String uploadFile(String fileName) throws Exception {
		return uploadFile(fileName, null, null);
	}

	public static 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 static String uploadFile(byte[] fileContent, String extName, NameValuePair[] metas) throws Exception {

		String result = client.upload_file1(fileContent, extName, metas);
		return result;
	}

	public static String uploadFile(byte[] fileContent) throws Exception {
		return uploadFile(fileContent, null, null);
	}

	public static String uploadFile(byte[] fileContent, String extName) throws Exception {
		return uploadFile(fileContent, extName, null);
	}

	public static String uploadFile2(String filePath) throws Exception {
		String fileId = "";
		String fileExtName = "";
		if (filePath.contains(".")) {
			fileExtName = filePath.substring(filePath.lastIndexOf(".") + 1);
		} else {
			logger.warn("Fail to upload file, because the format of filename is illegal.");
			return fileId;
		}
		// 建立连接
		/* ....... */
		// 上传文件
		try {
			fileId = client.upload_file1(filePath, fileExtName, null);
		} catch (Exception e) {
			logger.warn("Upload file \"" + filePath + "\"fails");
		} finally {
			trackerServer.close();
		}
		return fileId;
	}

	public static String uploadSlaveFile(String masterFileId, String prefixName, String slaveFilePath)
			throws Exception {
		String slaveFileId = "";
		String slaveFileExtName = "";
		if (slaveFilePath.contains(".")) {
			slaveFileExtName = slaveFilePath.substring(slaveFilePath.lastIndexOf(".") + 1);
		} else {
			logger.warn("Fail to upload file, because the format of filename is illegal.");
			return slaveFileId;
		}
		// 建立连接
		/* ....... */
		// 上传文件
		try {
			slaveFileId = client.upload_file1(masterFileId, prefixName, slaveFilePath, slaveFileExtName, null);
		} catch (Exception e) {
			logger.warn("Upload file \"" + slaveFilePath + "\"fails");
		} finally {
			trackerServer.close();
		}
		return slaveFileId;
	}

	public static int download(String fileId, String localFile) throws Exception {
		int result = 0;
		// 建立连接
		TrackerClient tracker = new TrackerClient();
		TrackerServer trackerServer = tracker.getConnection();
		StorageServer storageServer = null;
		StorageClient1 client = new StorageClient1(trackerServer, storageServer);
		// 上传文件
		try {
			result = client.download_file1(fileId, localFile);
		} catch (Exception e) {
			logger.warn("Download file \"" + localFile + "\"fails");
		} finally {
			trackerServer.close();
		}
		return result;
	}

	public static void main(String[] args) {
		try {
			String masterFileId = uploadFile("E:/1.jpg");
			System.out.println("主文件:" + masterFileId);
			download(masterFileId, "E:/master.jpg");
			String slaveFileId = uploadSlaveFile(masterFileId, "_120x120", "E:/1.jpg");
			System.out.println("从文件:" + slaveFileId);
			download(slaveFileId, "E:/slave.jpg");
		} catch (Exception e) {
			logger.error("upload file to FastDFS failed.", e);
		}
	}
}
2、编写测试代码

package test;

import org.junit.Test;

import cn.e3mall.manager.utils.FastDFSClient;

public class TestFdfs {

	private String file_ext_name = ".jpg";
	private String local_filename = "G://temp//1.jpg";

	
	@Test
	public void testFdfsClientUtils() throws Exception{
		//使用工具类进行图片上传
		String uploadFile = FastDFSClient.uploadFile(local_filename,file_ext_name);
		System.out.println(uploadFile);
	}
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值