Fastdfs與java整合使用過程
阿新 • • 發佈:2018-11-18
1.首先Fastdfs能正常通過URL訪問,如下:
2.寫個配置檔案fdfs_client.conf
#connect timeout(秒)
#預設值為30s
connect_timeout = 30
#network timeout(秒)
#預設值為30s
network_timeout = 60
#儲存日誌檔案的基本路徑
BASE_PATH = /家庭/ fastdfs
#tracker_server可以多次出現,tracker_server格式為
#“host:port”,主機可以是hostname或者ip地址
tracker_server =
192.168.101.4#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:22122 trackerServer的IP地址
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。* ; / ** *由管理員於2017/9/1建立。 * / public class FastDFSUtils { //上傳 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:使用郵差來測試介面
7:結果返回一個圖片URL:組1 / M01 / 00/05 / rBADF1ms_D-AHfumAAAWG4WuqfY292.jpg
8:整合成功