fastdfs檔案伺服器實現檔案上傳

2021-10-25 18:00:16 字數 3526 閱讀 6037

引入依賴

commons-fileupload<

/groupid>

commons-fileupload<

/artifactid>

1.3.1

<

/version>

<

/dependency>

org.csource.fastdfs<

/groupid>

fastdfs<

/artifactid>

1.27

<

/version>

<

/dependency>

springmvc配置檔案中 ,配置檔案上傳解析器

<

!-- 配置檔案上傳多**解析器 --

>

"multipartresolver"

class

="org.springframework.web.multipart.commons.commonsmultipartresolver"

>

"defaultencoding" value=

"utf-8"

/>

<

!-- 設定檔案上傳的最大值5mb,5

*1024

*1024

-->

"maxuploadsize" value=

"5242880"

/>

<

/bean>

建立fastdfs的配置檔案,配置伺服器位址

tracker_server=

192.168

.188

.146

:22122

借助檔案上傳的工具類

package com.bjc.util;

import org.csource.common.namevaluepair;

import org.csource.fastdfs.clientglobal;

import org.csource.fastdfs.storageclient1;

import org.csource.fastdfs.storageserver;

import org.csource.fastdfs.trackerclient;

import org.csource.fastdfs.trackerserver;

public

class

fastdfsclient

clientglobal.

init

(conf)

; trackerclient =

newtrackerclient()

; trackerserver = trackerclient.

getconnection()

; storageserver = null;

storageclient =

newstorageclient1

(trackerserver, storageserver);}

/** * 上傳檔案方法

* title: uploadfile

* description:

* @param filename 檔案全路徑

* @param extname 副檔名,不包含(.)

* @param metas 檔案擴充套件資訊

* @return

* @throws exception

*/public string uploadfile

(string filename, string extname, namevaluepair[

] metas)

throws exception

public string uploadfile

(string filename)

throws exception

public string uploadfile

(string filename, string extname)

throws exception

/** * 上傳檔案方法

* title: uploadfile

* description:

* @param filecontent 檔案的內容,位元組陣列

* @param extname 副檔名

* @param metas 檔案擴充套件資訊

* @return

* @throws exception

*/public string uploadfile

(byte

filecontent, string extname, namevaluepair[

] metas)

throws exception

public string uploadfile

(byte

filecontent)

throws exception

public string uploadfile

(byte

filecontent, string extname)

throws exception

}

controller層實現檔案上傳

package com.bjc.shop.controller;

import com.bjc.util.fastdfsclient;

import com.bjc.vo.resp.commonresult;

import org.springframework.web.bind.annotation.restcontroller;

import org.springframework.web.multipart.multipartfile;

@restcontroller

public

class

fileuploadcontroller

catch

(exception e)

}//獲得檔案的拓展名

private string getextname

(multipartfile file)

}

angularjs service定義請求

this

.uploadfile

=function()

,//表單物件fromdata序列化

transformrequest: angular.identity

});}

angularjs controller實現

//的json物件

$scope.image_entity =

;//新建彈出框,上傳按鈕呼叫該方法

$scope.

uploadfile

=function()

);}

fastdfs 檔案伺服器遷移

在實際的專案應用中,由於伺服器替換或專案變更難免會存在fastdfs檔案伺服器遷移的工作。本文重點介紹fastdfs檔案系統在不同情況下的檔案遷移處理方案。通過檔案伺服器儲存規則定義,發現當ip位址沒有發生變化時,這種檔案伺服器遷移是最簡單的,只需要在新的伺服器上安裝fastdfs,把原來伺服器上的...

分布式檔案伺服器FastDFS

客戶端上傳檔案後儲存伺服器將檔案 id 返回給客戶端,此檔案 id 用於以後訪問該檔案的索引資訊。檔案索引資訊包括 組名,虛擬磁碟路徑,資料兩級目錄,檔名。上傳的時候只是問tracker咋樣了。組名 檔案上傳後所在的 storage 組名稱,在檔案上傳成功後有 storage 伺服器返回,需要客戶端...

分布式檔案伺服器FastDFS

1.1什麼是fastdfs tracker server 作用是負載均衡和排程,通過 tracker server 在檔案上傳時可以根據一些策略找到 storage server 提供檔案上傳服務。可以將 tracker 稱為追蹤伺服器或排程伺服器。storage server 作用是檔案儲存,客戶...