Linux下FastDFS安裝(4)Java程式碼內使用
一.新增依賴
1.下載依賴
2.安裝依賴
將專案匯入到 IDE 中,或者直接使用 maven 指令將依賴安裝到倉庫中
3.專案中新增依賴
<dependency>
<groupId>org.csource</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>1.27-SNAPSHOT</version>
</dependency>
4.配置檔案
4.1 conf 格式
connect_timeout = 2
network_timeout = 30
charset = UTF-8
http.tracker_http_port = 80
http.anti_steal_token = no
http.secret_key = FastDFS1234567890
tracker_server = 10.0.11.247:22122
tracker_server = 10.0.11.248:22122
tracker_server = 10.0.11.249:22122
#注1:tracker_server指向您自己IP地址和埠,1-n個
#注2:除了tracker_server,其它配置項都是可選的
4.2 properties 格式
fastdfs.connect_timeout_in_seconds = 5 fastdfs.network_timeout_in_seconds = 30 fastdfs.charset = UTF-8 fastdfs.http_anti_steal_token = false fastdfs.http_secret_key = FastDFS1234567890 fastdfs.http_tracker_http_port = 80 fastdfs.tracker_servers = 10.0.11.201:22122,10.0.11.202:22122,10.0.11.203:22122 #注1:properties 配置檔案中屬性名跟 conf 配置檔案不盡相同,並且統一加字首"fastdfs.",便於整合到使用者專案配置檔案 #注2:fastdfs.tracker_servers 配置項不能重複屬性名,多個 tracker_server 用逗號","隔開 #注3:除了fastdfs.tracker_servers,其它配置項都是可選的
5.載入配置示例(後面用)
//載入原 conf 格式檔案配置:
ClientGlobal.init("fdfs_client.conf");
ClientGlobal.init("config/fdfs_client.conf");
ClientGlobal.init("/opt/fdfs_client.conf");
ClientGlobal.init("C:\\Users\\Kam\\config\\fdfs_client.conf");
//載入 properties 格式檔案配置:
ClientGlobal.initByProperties("fastdfs-client.properties" );
ClientGlobal.initByProperties("config/fastdfs-client.properties");
ClientGlobal.initByProperties("/opt/fastdfs-client.properties");
ClientGlobal.initByProperties("C:\\Users\\Kam\\config\\fastdfs-client.properties");
//載入 Properties 物件配置:
Properties props = new Properties();
props.put(ClientGlobal.PROP_KEY_TRACKER_SERVERS, "10.0.11.101:22122,10.0.11.102:22122");
ClientGlobal.initByProperties(props);
//載入 trackerServers 字串配置:
String trackerServers = "10.0.11.101:22122,10.0.11.102:22122";
ClientGlobal.initByTrackers(trackerServers);
6.檢查載入配置結果:
System.out.println("ClientGlobal.configInfo(): " + ClientGlobal.configInfo());
ClientGlobal.configInfo(): {
g_connect_timeout(ms) = 5000
g_network_timeout(ms) = 30000
g_charset = UTF-8
g_anti_steal_token = false
g_secret_key = FastDFS1234567890
g_tracker_http_port = 80
trackerServers = 10.0.11.101:22122,10.0.11.102:22122
}
二. 測試方式演示
1.直接測試
@Test
public void test1() throws Exception {
//載入配置檔案,需要告訴它嚮導伺服器在哪
ClientGlobal.init("/home/Kam/workspace/testfastdfs/src/main/resources/server.conf");
//建立客戶端
TrackerClient trackerClient=new TrackerClient();
TrackerServer trackerServer = trackerClient.getConnection();
StorageServer server=null;
StorageClient storageClient=new StorageClient(trackerServer, server);
String[] strings = storageClient.upload_file("/home/img/12333.jpg", "jpg", null);//引數1 檔案路徑,引數2 檔案的副檔名,引數3檔案的元資料
for (String string : strings) {
System.err.println(string);//返回的結果就是整個地址,不包括域名 ip
}
// group1/M00/00/00/rBFdZ1oU8z2AAKfLABx_3eMqYVk200.jpg
}
2.封裝簡單工具類
/**
工具類中只封裝了部分方法,更多方法請自己封裝
*/
public class FastDFSClient {
private TrackerClient trackerClient;
private TrackerServer trackerServer;
private StorageClient1 storageClient;
private StorageServer storageServer;
public FastDFSClient(String conf) throws Exception {
if (conf.startsWith("classpath")) {//如果配置檔案是 classpath 開頭的,則代表是在類路徑中,去掉 classpath: 然後拼接類路徑
conf=conf.replace("classpath:", getClass().getResource("/").getPath());
}
ClientGlobal.init(conf);//載入路徑
trackerClient=new TrackerClient();
trackerServer=trackerClient.getConnection();
storageClient=new StorageClient1(trackerServer, storageServer);
}
/**
上傳檔案,引數是檔案的路徑,字尾名和元資料
*/
public String upload_file(String fileName,String ext_name,NameValuePair[] pairs) throws Exception {
return storageClient.upload_file1(fileName, ext_name, pairs);
}
public String upload_file(String fileName) throws Exception{
return upload_file(fileName, null, null);
}
public String upload_file(String fileName,String ext_name) throws Exception{
return upload_file(fileName, ext_name, null);
}
public String upload_file(String fileName,NameValuePair[] pairs) throws Exception{
return upload_file(fileName, null, pairs);
}
/**
上傳二進位制資料,需要將檔案先轉換為二進位制
*/
public String upload_file(byte[]source,String ext_name,NameValuePair[] pairs) throws Exception{
return storageClient.upload_file1(source, ext_name, pairs);
}
}
3. 測試工具類
@Test
public void test2() throws Exception{
FastDFSClient fastDFSClient =new FastDFSClient("/home/Kam/workspace/testfastdfs/src/main/resources/server.conf");
String upload_file = fastDFSClient.upload_file("/home/img/12333.jpg");
System.err.println(upload_file);
}
三. SpringMvc 中使用
環境配置和前面四步一致
程式碼
@Controller
public class FileUploadController {
/**
* MultipartFile的形參的名字和form表單中檔案的域name一致
* @param file
* @return
*/
@ResponseBody
@RequestMapping(value="/upload",produces= {"text/plain;charset=utf-8"})
public String upLoad(MultipartFile file) {
String path=null;
Map<String, Object> map=new HashMap<>();//用於返回 json 資料的map 物件
try {
//file是使用者上傳的檔案
//需要將檔案轉存到檔案伺服器
//file.transferTo(dest); 儲存到本地檔案
/*file.getOriginalFilename()
file.getName()*/
FastDFSClient fastDFSClient=new FastDFSClient("classpath:server.conf");//此處應當注入物件過來
path= fastDFSClient.upload_file(file.getBytes(), "jpg", null);
map.put("error", 0);//設定返回內容
map.put("url", "http://met.onlykam.jin/"+path);
} catch (Exception e) {
map.put("error", 1);
map.put("message", "Hello Kugou");
e.printStackTrace();
}
return JsonUtils.objectToJson(map);//將資料轉成 json 資料返回,此處可以自己寫或者直接返回map
}
}
相關推薦
Linux下FastDFS安裝(4)Java程式碼內使用
一.新增依賴 1.下載依賴 2.安裝依賴 將專案匯入到 IDE 中,或者直接使用 maven 指令將依賴安裝到倉庫中 3.專案中新增依賴 <dependency> <group
Linux下FastDFS安裝(3) FastDFS搭建Nginx模板
前言: 第一步:下載依賴 yum install -y pcre pcre-devel yum install -y zlib zlib-devel yum install -y openssl openssl-devel 第二步:上傳 nginx
linux下如何部署(執行)java jar包,並關閉此jar的程序
前段時間剛剛接觸了一下linux系統,本人表示第一次使用無從下手,不像windows作業系統是視覺化的,簡單的建立檔案或資料夾都要通過寫命令才能將其完成,由於工作需要,不得不自學linux ,先是自學常用的一些命令,然後再部署可執行jar,以下是具體的操作步驟。 1、執行
linux下MySQL安裝(rpm)
1,根據伺服器情況下載server rpm和client rpm 下載地址http://dev.mysql.com/downloads/mysql/5.1.html 2,linux下執行rpm -ivh MySQL-server-community-5.1.58-1.rhel
linux信號解釋(4)--C語言下的理解
linux信號 C語言下linux信號理解 上一節中中簡單介紹了信號的處理機制,就是調用函數庫來實現信號的處理,因此,在這節中,介紹在C語言下如何理解信號的處理機制。 創建一個文件signal.c,文件內容如下:(對於學過一下C語言的童鞋來說是不是很熟悉呢) #include<signal.h&
Linux下Mysql安裝(RPM安裝)
.cn num led com erro sql文件 官網 方法 l命令 1. 首先檢查機器裏是否已經存在MySQL $ rpm -qa | grep mysql 2. 去官網下載相應的rpm包:https://dev.mysql.com/downloads/mysql
Linux下Nodejs安裝(完整詳細)
很久之前安裝過windows下以及Mac下的node,感覺還是很方便的,不成想今天安裝linux下的坑了老半天,特此記錄。 首先去官網下載程式碼,這裡一定要注意安裝分兩種,一種是Source Code原始碼,一種是編譯後的檔案。我就是按照網上原始碼的安裝方式去操
vtk在linux下的安裝(12月8日更新)
安裝前準備 執行環境:ubuntu14.04 LTS, 64bit //本人在ubuntu 14.10下安裝失敗 安裝版本:vtk6.1.0.tar.gz vtkdata6.1.0.tar.gz 編譯器:g++ $ sudo apt-cache search gcc*g+
redis原理以及redis在linux系統下的安裝(二)
上一篇文章,我們講的是redis的原理,這一篇文章,我們一起學習redis在linux系統下的安裝: 首先,我們需要知道linux系統下其實簡單來講,就是: 1、redis上的安裝包解壓、編譯。
Greenplum 在Linux下的安裝(centOS,RedHat)
Greenplum on Linux 網傳的Greenplum安裝教程多生搬硬套,很多不必要的東西也會列出來,導致環境的配置極其麻煩;官方的安裝部署又顯得太凌亂,100多頁,讓人難以消化。本文介紹了Greenplum分散式下的安裝(Linux系統下:Ce
Linux下Oracle client(sqlplus)安裝和配置
1、下載rpm包 [[email protected] ~]# ls oracle-instantclient11.2-basic-11.2.0.4.0-1.x86_64.rpm
Linux下基礎命令(一)
linux find 實戰操作 每天積累知識就會有成長,只要付出在不久的將來一定會有收獲,可能回來的晚點,但是一定要貴在堅持,今天第一次發博客,如不出意外情況我每天都會發的,這個是我總結的find命令的一些實戰,希望我總結的知識點在今後會對大家有幫助。find -name "*a" #以a
Java Web 深入分析(4) Java I/O 深入分析
lock 異步 瓶頸 系統 基本結構 java 同步異步 nio -i I/O問題可以說是現在大部分web系統的瓶頸。我們要了解的java I/O(後面簡稱為(io)) io類庫的基本結構 -磁盤io的工作機制 -網絡io的工作機制 -NIO的工作方式 -同步異步、阻
linux系統編程(4)
linux系統編程一 IPC 對象 ---- 消息隊列 IPC 對象命令 (1)查看系統中IPC對象 ipcs -a 顯示所有的IPC對象 ipcs -s/-q/-m (2)刪除系統中的IPC對象 ipcrm -q/-s/-m ID 1.獲得key值 第一種:
linux系統程序安裝(一)rpm工具
erl fields program 軟件 方法 owin lib 直接 源碼安裝 linux系統下程序安裝主要采用三種方式:1、rpm,有點類似.msi 和.exe比較類似,軟件包(相當於windows的某個程序的所有文件)的安裝路徑和文件名稱基本是固定的,但是他不會安裝
linux系統程序安裝(一)yum工具
sync 內容 sim lean provide for 就是 grep 大型 yum安裝工具類似appstore,運用yum工具可以方便的下載所需的程序,同時yum工具會自動檢查程序的依賴關系,並安裝相應的依賴包,有點像windows的某些大型軟件安裝程序會自動檢查幫你安
linux系統程序安裝(二)yum工具2-yum源管理
內容 centos 備份 hang clean 原生 yum 聯網 系統 繼續我們的yum工具應用之旅,yum工具之所以方便就是因為有方便的在線雲庫,實際工作中我們可能沒辦法鏈接互聯網,或者我們想安裝的程序原生源那麽我們能不能用其他方式應用方便的yum源呢? 一、使用光盤
linux系統程序安裝(三)源碼包安裝程序
efi ron 下載 示例 wget 問題 位置 提示 /usr 源碼包安裝是日常使用過程中最經常的安裝方式,比如nagios套件、apche等重要軟件都是源碼包方式安裝,源碼包編譯安裝技術是運維技術中比較重要的部分。 一、源碼包安裝位置 運維時,最好把要跑的業務包放到/u
RabbitMQ Linux(Redhat6.5)安裝(二 )
html -i 運行環境 版權 force openss onf mini 命令行 一、安裝erlang 由於RabbitMq的linux運行環境需要erlang環境,所以需要先安裝erlang; 1、erlang下載: http://erlang.org
Linux下的壓縮(zip)解壓(unzip)縮命令
usr 文件夾 desc ont post text -s 解壓縮 mod 1.zip命令zip -r myfile.zip ./*將當前目錄下的所有文件和文件夾全部壓縮成myfile.zip文件,-r表示遞歸壓縮子目錄下所有文件.2.unzip命令unzip -o -