导入相应的依赖
<dependencies>
<dependency>
<groupId>org.csource.fastdfs</groupId>
<artifactId>fastdfs</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
测试案例
public class TestDfsDemo {
public static void main(String[] args)throws Exception{
//加载配置文件
ClientGlobal.init("E:\\ideaworkspace\\duboo\\ylx_parent\\fastDemo1\\src\\main\\resources\\fdfs_client.conf");
//创建管理端
TrackerClient trackerClient = new TrackerClient();
//通过管理端获得连接
TrackerServer connection = trackerClient.getConnection();
//创建存储端对象
StorageClient1 storageClient = new StorageClient1(connection,null);
//文件属性的信息
NameValuePair[] meta_list = new NameValuePair[3];
meta_list[0] = new NameValuePair("fileName","mv");
meta_list[1] = new NameValuePair("ExtName","jpg");
meta_list[2] = new NameValuePair("zuozhe","lj");
//上传文件
String path =storageClient.upload_file1("d:\\Desktop\\img\\1.jpg","jpg",meta_list);
System.out.println("::::::::"+path);
}
}
UploadController.java
@RestController
@RequestMapping("/upload")
public class UploadController {
//读取application.properties配置文件内容
@Value("${FILE_SERVER_URL}")
private String FILE_SERVER_URL;
@RequestMapping("/uploadFile")
public Result uploadFile(MultipartFile file)throws Exception{
try {
FastDFSClient fastDFS = new FastDFSClient("classpath:fastDFS/fdfs_client.conf");
//字节数组,文件名字,字节大小
String path = fastDFS.uploadFile(file.getBytes(), file.getOriginalFilename(),file.getSize());
return new Result(true,FILE_SERVER_URL+path);
}catch (Exception e){
e.printStackTrace();
return new Result(false,"上传失败");
}
}
}
Resoruce-->conffig-->application.properties
FILE_SERVER_URL=http://192.168.200.128/
Resource-->fastDFS-->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.200.128: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
View Code
修改FastDFSClient.java
/**
*这个比较好用
*/
public String uploadFile(byte[] file, String fileName, long fileSize) throws Exception {
//文件属性信息
NameValuePair[] metas = new NameValuePair[3];
metas[0] = new NameValuePair("fileName", fileName);
metas[1] = new NameValuePair("fileSize", String.valueOf(fileSize));
//获取名字
metas[2] = new NameValuePair("fileExt", FilenameUtils.getExtension(fileName));
String result = storageClient.upload_file1(file, FilenameUtils.getExtension(fileName), metas);
return result;
}