您的位置:首页 > 其它

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

2017-06-13 11:16 281 查看

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

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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: