FastDFS 分布式文件插件(Java)
- 首先是fastDFS 的util 工具类:
public class FastDFSUtils {
private static StorageClient client1;
static {
try {
ClientGlobal.initByProperties("fastdfs-client.properties");
TrackerClient trackerClient = new TrackerClient();
TrackerServer trackerServer = trackerClient.getConnection();
client1 = new StorageClient(trackerServer,null);
}catch (Exception e){
e.printStackTrace();
}
}
public static String upload(MultipartFile file) {
String oldName = file.getOriginalFilename();
try {
String[] strings = client1.upload_file(file.getBytes(), oldName.substring(oldName.lastIndexOf(".") + 1), null);
String fileName="";
for (String str:strings){
//System.out.println("Name:----"+str);
fileName+="/"+str;
}
return fileName;
}catch (Exception e){
e.printStackTrace();
}
return null;
}
}
- java 调用:
@PostMapping("/CommonController/IMG")
public JSON insertCommodityIMG(MultipartFile file){
log.info("serveraddress------>"+serveraddress);
String fileId = FastDFSUtils.upload(file);
String url = serveraddress + fileId;
if(url!=null){
return ResultData.getResponseData(url,ResultCode.IMG_SUCCESS);
}else {
return ResultData.getResponseData(null,ResultCode.IMG_ERROR);
}
}
- 最后是pom引用:
<!--fdfs-->
<dependency>
<groupId>net.oschina.zcx7878</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.27.0.0</version>
</dependency>
最后附上 fastdfs-client.properties:
##fastdfs-clint.properties
fastdfs.connect_timeout_in_seconds = 5
fastdfs.network_timeout_in_seconds = 5
fastdfs.charset = UTF-8
fastdfs.http_anti_steal_token = false
##fastdfs.http_secret_key = FasDFS1234567890
fastdfs.http_tracker_http_port = 80
fastdfs.tracker_servers = IP:22122
## Whether to open the connection pool, if not, create a new connection every time
fastdfs.connection_pool.enabled = true
## max_count_per_entry: max connection count per host:port , 0 is not limit
fastdfs.connection_pool.max_count_per_entry = 500
##connections whose the idle time exceeds this time will be closed, unit: second,default value id 3600
fastdfs.connection_pool.max_idle_time = 3600
##Maximum waiting time when the maximum number of connections is reached,unit:millisecond, default value is 3600
fastdfs.connection_pool.max_wait_time_in_ms = 1000