文件上传工具类及配置FastDFSUtils

上传图片工具类

import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.commons.io.FilenameUtils;
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;
import org.springframework.core.io.ClassPathResource;

/**
 * 上传图片
 */
public class FastDFSUtils {

	//上传图片
	public static String uploadPic(byte[] pic,String name,long size) throws Exception{
		//准备工作  读取配置文件
		ClassPathResource resource = new ClassPathResource("fdfs_client.conf");
		//流  读取文件 绝对 路径  相对路径不行的
		ClientGlobal.init(resource.getClassLoader().getResource("fdfs_client.conf").getPath());
		//1:连接Tracker 上传图片
		TrackerClient trackerClient = new TrackerClient();
		//获取保存图片的Stoage的位置
		TrackerServer trackerServer = trackerClient.getConnection();
		//连接Stoager
		StorageServer storageServer = null;
		
		StorageClient1 storageClient1 = new StorageClient1(trackerServer,storageServer);
		//上传图片
		String ext = FilenameUtils.getExtension(name);
		NameValuePair[] meta_list = new NameValuePair[3];
		meta_list[0] = new NameValuePair("filename",name);
		meta_list[1] = new NameValuePair("fileext",ext);
		meta_list[2] = new NameValuePair("filesize",String.valueOf(size));
//		http://192.168.200.128/
		//    group1/M00/00/01/wKjIgFWOYc6APpjAAAD-qk29i78248.jpg
		String path = storageClient1.upload_file1(pic, ext, meta_list);
		return path;
	}
}

fdfs_client.conf的配置文件

# connect timeout in seconds
# default value is 30s
connect_timeout=30

# network timeout in seconds
# default value is 30s
network_timeout=60

# the base path to store log files
base_path=/home/fastdfs

# tracker_server can ocur more than once, and tracker_server format is
#  "host:port", host can be hostname or ip address
tracker_server=192.168.198.123:22122
#tracker_server=192.168.122.60:22122

#standard log level as syslog, case insensitive, value list:
### emerg for emergency
### alert
### crit for critical
### error
### warn for warning
### notice
### info
### debug
log_level=info

# if use connection pool
# default value is false
# since V4.05
use_connection_pool = false

# connections whose the idle time exceeds this time will be closed
# unit: second
# default value is 3600
# since V4.05
connection_pool_max_idle_time = 3600

# if load FastDFS parameters from tracker server
# since V4.05
# default value is false
load_fdfs_parameters_from_tracker=false

# if use storage ID instead of IP address
# same as tracker.conf
# valid only when load_fdfs_parameters_from_tracker is false
# default value is false
# since V4.05
use_storage_id = false

# specify storage ids filename, can use relative or absolute path
# same as tracker.conf
# valid only when load_fdfs_parameters_from_tracker is false
# since V4.05
storage_ids_filename = storage_ids.conf


#HTTP settings
http.tracker_server_port=80

#use "#include" directive to include HTTP other settiongs
##include http.conf

你可能感兴趣的:(utils)