记录 Java 中 FastDFS的一次简单使用
环境搭建
环境准备
-
下载gcc环境
yum install -y gcc-c++
-
下载两个依赖库
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/
启动Tracker
-
配置
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
启动Storage
-
配置
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-module安装
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(); } }