FastDFSを使用するためのシンプルでJavaの録音
ビルドへの環境
環境の準備
-
ダウンロードGCC環境
yum install -y gcc-c++
-
2つの依存ライブラリをダウンロード
yum -y install libevent
cd /usr/local wget https://github.com/happyfish100/libfastcommon/archive/V1.0.43.tar.gz tar -zxvf V1.0.43.tar.gz cd libfastcommon-1.0.43/ ./make.sh ./make.sh install
-
インストールパッケージをダウンロードします
wget https://github.com/happyfish100/fastdfs/archive/V6.06.tar.gz tar -zxvf V6.06.tar.gz cd fastdfs-6.06/ ./make.sh ./make.sh install cd conf/ cp ./* /etc/fdfs/
スタートトラッカー
-
コンフィギュレーション
cd /etc/fdfs cat tracker.conf # the base path to store data and log files base_path = /home/yuqing/fastdfs cd /home mkdir yuqing cd yuqing mkdir fastdfs chmod 777 /home/yuqing/fastdfs
-
スタート
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf start
スタートストレージ
-
コンフィギュレーション
vi /etc/fdfs/storage.conf ### #tracker_server = 192.168.209.121:22122 #tracker_server = 192.168.209.122:22122 tracker_server = 192.168.204.133:22122
-
スタート
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf start
nginxのインストール
wget http://nginx.org/download/nginx-1.17.0.tar.gz
tar zxvf nginx-1.17.0.tar.gz
cd cd nginx-1.17.0
yum -y install pcre-devel
yum -y install openssl openssl-devel
./configure
make
make install
/usr/local/nginx/sbin/nginx
fastdfs-nginxのモジュールのインストール
cd /usr/local
wget https://github.com/happyfish100/fastdfs-nginx-module/archive/V1.22.tar.gz
tar -zxvf V1.22.tar.gz
cp fastdfs-nginx-module-1.22/src/mod_fastdfs.conf /etc/fdfs/
vi /etc/fdfs/mod_fastdfs.conf
#tracker_server=tracker:22122
tracker_server=127.0.0.1:22122
#url_have_group_name = false
url_have_group_name = true
nginxのインストール再コンパイル
cd /usr/local/nginx-1.17.0
./configure --add-module=/usr/local/fastdfs-nginx-module-1.22/src
make
make install
設定nginxの##
vi /usr/local/nginx/conf/nginx.conf
location ~/group([0-9]) {
ngx_fastdfs_module;
}
再起動nginxの
lsof -i:80
kill -9 PID
/usr/local/nginx/sbin/nginx
ngx_http_fastdfs_set pid=15596[启动成功]
Javaクライアント・コール
-
一般的なMavenプロジェクトを作成するには、以下の依存関係を追加します。
<dependency> <groupId>net.oschina.zcx7878</groupId> <artifactId>fastdfs-client-java</artifactId> <version>1.27.0.0</version> </dependency>
-
ファイルのアップロード
public static void uploadFile() { try { ClientGlobal.initByProperties("client.properties"); TrackerClient trackerClient = new TrackerClient(); TrackerServer trackerServer = trackerClient.getConnection(); StorageServer storageServer = null; StorageClient1 storageClient = new StorageClient1(trackerServer,storageServer); String url = storageClient.upload_file1("C:\\Users\\qzf\\Desktop\\p.jpg", "jpg", null); System.out.println(url); // group1/M00/00/00/wKjMhV5h_QWAWXR0AACJiovU6KI120.jpg } catch (IOException e) { e.printStackTrace(); } catch (MyException e) { e.printStackTrace(); }
http://192.168.204.133/group1/M00/00/00/wKjMhV5h_QWAWXR0AACJiovU6KI120.jpg
-
ファイルのダウンロード
public static void downloadFile() { try { ClientGlobal.initByProperties("client.properties"); TrackerClient trackerClient = new TrackerClient(); TrackerServer trackerServer = trackerClient.getConnection(); StorageServer storageServer = null; StorageClient1 storageClient1 = new StorageClient1(trackerServer,storageServer); byte[] bytes = storageClient1.download_file1("group1/M00/00/00/wKjMhV5h_QWAWXR0AACJiovU6KI120.jpg"); FileOutputStream fos = new FileOutputStream(new File("C:\\Users\\qzf\\Desktop\\pp.jpg")); fos.write(bytes); fos.close(); } catch (IOException e) { e.printStackTrace(); } catch (MyException e) { e.printStackTrace(); } }