之前写了,linux上安装配置Fast DFS整合nginx,图片上传测试。
这里在写一下,Java实现Fast DFS图片上传。
开始:
maven添加fastdfs相应的jar包
jar包下载地址:链接:https://pan.baidu.com/s/1smuDFtV 密码:xfnu
上传测试代码:
/**
* 测试图片上传
* @throws MyException
* @throws IOException
* @throws FileNotFoundException
*/
@Test
public void uploadPic() throws Exception{
//1.指定要上传的图片的绝对路径
String path = "F:\\fc29cd5c10385343ade29c739813b07eca8088b2.jpg";
//2.指定图片服务器路径,使用client.conf文件。指定client.conf的绝对路径
String clienPath = "E:\\Itcast\\Project\\taotao\\space3\\taotao-manager-web\\src\\main\\resources\\client.conf";
//3.加载配置文件,连接服务器
ClientGlobal.init(clienPath);
//4.创建tracker客户端
TrackerClient trackerClient = new TrackerClient();
//5.从客户端获取tracker对象
TrackerServer trackerServer = trackerClient.getConnection();
StorageServer storageServer = null;
//6.创建storage客户端
StorageClient storageClient = new StorageClient(trackerServer,storageServer);
//7.上传
String[] upload_file = storageClient.upload_file(path, "jpg", null);
for (String string : upload_file) {
System.out.println(string);
}
}
工具类:
package com.taotao.web.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;
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;
}
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);
}
}