导入相应的依赖
1<dependencies> 2 <dependency> 3 <groupId>org.csource.fastdfs</groupId> 4 <artifactId>fastdfs</artifactId> 5 <version>1.2</version> 6 </dependency> 7 </dependencies>
测试案例
1public class TestDfsDemo { 2 public static void main(String[] args)throws Exception{ 3 //加载配置文件 4 ClientGlobal.init("E:\\ideaworkspace\\duboo\\ylx_parent\\fastDemo1\\src\\main\\resources\\fdfs_client.conf"); 5 //创建管理端 6 TrackerClient trackerClient = new TrackerClient(); 7 //通过管理端获得连接 8 TrackerServer connection = trackerClient.getConnection(); 9 //创建存储端对象 10 StorageClient1 storageClient = new StorageClient1(connection,null); 11 //文件属性的信息 12 NameValuePair[] meta_list = new NameValuePair[3]; 13 meta_list[0] = new NameValuePair("fileName","mv"); 14 meta_list[1] = new NameValuePair("ExtName","jpg"); 15 meta_list[2] = new NameValuePair("zuozhe","lj"); 16 //上传文件 17 String path =storageClient.upload_file1("d:\\Desktop\\img\\1.jpg","jpg",meta_list); 18 System.out.println("::::::::"+path); 19 } 20}
UploadController.java
1@RestController 2@RequestMapping("/upload") 3public class UploadController { 4 //读取application.properties配置文件内容 5 @Value("${FILE_SERVER_URL}") 6 private String FILE_SERVER_URL; 7 @RequestMapping("/uploadFile") 8 public Result uploadFile(MultipartFile file)throws Exception{ 9 try { 10 FastDFSClient fastDFS = new FastDFSClient("classpath:fastDFS/fdfs_client.conf"); 11 //字节数组,文件名字,字节大小 12 String path = fastDFS.uploadFile(file.getBytes(), file.getOriginalFilename(),file.getSize()); 13 return new Result(true,FILE_SERVER_URL+path); 14 }catch (Exception e){ 15 e.printStackTrace(); 16 return new Result(false,"上传失败"); 17 } 18 } 19}
Resoruce-->conffig-->application.properties
1FILE_SERVER_URL=http://192.168.200.128/ 2 3Resource-->fastDFS-->fdfs_client.conf

1# connect timeout in seconds 2# default value is 30s 3connect_timeout=30 4 5# network timeout in seconds 6# default value is 30s 7network_timeout=60 8 9# the base path to store log files 10base_path=/home/fastdfs 11 12# tracker_server can ocur more than once, and tracker_server format is 13# "host:port", host can be hostname or ip address 14tracker_server=192.168.200.128:22122 15 16#standard log level as syslog, case insensitive, value list: 17### emerg for emergency 18### alert 19### crit for critical 20### error 21### warn for warning 22### notice 23### info 24### debug 25log_level=info 26 27# if use connection pool 28# default value is false 29# since V4.05 30use_connection_pool = false 31 32# connections whose the idle time exceeds this time will be closed 33# unit: second 34# default value is 3600 35# since V4.05 36connection_pool_max_idle_time = 3600 37 38# if load FastDFS parameters from tracker server 39# since V4.05 40# default value is false 41load_fdfs_parameters_from_tracker=false 42 43# if use storage ID instead of IP address 44# same as tracker.conf 45# valid only when load_fdfs_parameters_from_tracker is false 46# default value is false 47# since V4.05 48use_storage_id = false 49 50# specify storage ids filename, can use relative or absolute path 51# same as tracker.conf 52# valid only when load_fdfs_parameters_from_tracker is false 53# since V4.05 54storage_ids_filename = storage_ids.conf 55 56 57#HTTP settings 58http.tracker_server_port=80 59 60#use "#include" directive to include HTTP other settiongs 61##include http.conf
View Code
1修改FastDFSClient.java 2 3/** 4 *这个比较好用 5 */ 6 public String uploadFile(byte[] file, String fileName, long fileSize) throws Exception { 7 //文件属性信息 8 NameValuePair[] metas = new NameValuePair[3]; 9 metas[0] = new NameValuePair("fileName", fileName); 10 metas[1] = new NameValuePair("fileSize", String.valueOf(fileSize)); 11 //获取名字 12 metas[2] = new NameValuePair("fileExt", FilenameUtils.getExtension(fileName)); 13 String result = storageClient.upload_file1(file, FilenameUtils.getExtension(fileName), metas); 14 return result; 15 }