1.首先Fastdfs能正常通过URL访问,如下:
2.写个配置文件fdfs_client.conf
#connect timeout(秒)
#默认值为30s
connect_timeout = 30
#network timeout(秒)
#默认值为30s
network_timeout = 60
#存储日志文件的基本路径
BASE_PATH = /home/ fastdfs
#tracker_server可以多次出现,tracker_server格式为
#“host:port”,主机可以是hostname或者ip地址
tracker_server =
192.168.101.4 :22122 trackerServer的IP地址
#tracker_server = 192.168.101.4:22122
#标准日志级别作为syslog,不区分大小写,值列表:### emerg for emergency ### alert ### crit for critical ### error ### warn for warning ###通知### info ### debuglog_level = info
#如果使用连接池#默认值为false#自V4.05use_connection_pool = false
#空闲时间超过此时间的连接将被关闭#单位:秒#默认值为3600#因为V4.05connection_pool_max_idle_time = 3600#如果从V4.05#加载来自跟踪服务器#的FastDFS参数#默认值为falseload_fdfs_parameters_from_tracker = false#如果使用存储ID而不是与tracker.conf相同的IP地址#
#只有当load_fdfs_parameters_from_tracker为false时,才有效#default值为false#自V4.05use_storage_id = false#指定存储ids filename,可以使用与tracker.conf相同的绝对路径#只有当load_fdfs_parameters_from_tracker为false时才有效#自V4.05storage_ids_filename = storage_ids.conf#HTTP settingshttp.tracker_server_por t = 80#使用“#include”指令包含HTTP其他settiongs ## include http.conf 存放位置
3:Maven的依赖
<! - fastdfs client - >
<dependency>
<groupId> org.csource </ groupId>
<artifactId> fastdfs-client-java </ artifactId>
<version> 1.25 </ version>
</ dependency>
<! - 上传 - >
<dependency>
<groupId> commons-fileupload </ groupId>
<artifactId> commons-fileupload </ artifactId>
<version> 1.3.3 </ version>
</ dependency>
4.写工具类FastDfsUtils.java
import org.apache.commons.io.FilenameUtils ;
import org.csource.common.NameValuePair ;
import org.csource.fastdfs。* ;
/ **
*由admin2017/9/1创建。
* 工具
* /
public static String uploadPic(byte[] pic, String name, long size) throws Exception {
InputStream is = FastDFSUtil.class.getResourceAsStream("/META-INF/fdfs_with_redis_client.conf");
ClientGlobal.init(is);
//创建Tracker的客户端
TrackerClient trackerClient = new TrackerClient();
//Tracker服务器返回来给你的Stoager的地址
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));
//执行上传
String path = storageClient1.upload_file1(pic, ext, meta_list);
// http://192.168.200.128/group1/M00/00/01/wKjIgFWOYc6APpjAAAD-qk29i78248.jpg
// 返回的路径如下 // group1/M00/00/01/wKjIgFWOYc6APpjAAAD-qk29i78248.jpg
return path;
}
}
5.来个控制器测试
@Controller
@RequestMapping(“/ up”)
public class UpController {
@ResponseBody
@RequestMapping(“/ up”)
public JData up(@RequestParam(value = “file” ,required = false)MultipartFile文件, HttpServletRequest请求)throws Exception {
try {
String s = FastDFSUtils。uploadPic(file.getBytes(), file.getOriginalFilename(), file.getSize());
System.out .println(s);
return new JData(ReturnCode.SUCCESS,“成功”);
} catch(e){
return new JData(ReturnCode.FAILED,“失败”);
}
}
}
6:使用postman来测试接口
7:结果返回一个图片URL:组1 / M01 / 00/05 / rBADF1ms_D-AHfumAAAWG4WuqfY292.jpg
8:集成成功