Record of Java in a simple to use FastDFS

Record of Java in a simple to use FastDFS

Environment to build

Preparing the Environment

  1. Download gcc environment

    yum install -y gcc-c++
    
  2. Download two dependent libraries

    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
    
  3. Download the installation package

    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/
    

Start Tracker

  1. Configuration

    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
    
  2. start up

    /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf start
    

Start Storage

  1. Configuration

    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
    
  2. start up

    /usr/bin/fdfs_storaged /etc/fdfs/storage.conf start
    

Nginx installation

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 installation

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

Recompile install Nginx

cd /usr/local/nginx-1.17.0
./configure --add-module=/usr/local/fastdfs-nginx-module-1.22/src
make
make install

## Configure Nginx

vi /usr/local/nginx/conf/nginx.conf

location ~/group([0-9]) {
      ngx_fastdfs_module;
}

Restart Nginx

lsof -i:80
kill -9 PID
/usr/local/nginx/sbin/nginx
ngx_http_fastdfs_set pid=15596[启动成功]

Java client calls

  1. Creating a common Maven project, add the following dependence:

    <dependency>
        <groupId>net.oschina.zcx7878</groupId>
        <artifactId>fastdfs-client-java</artifactId>
        <version>1.27.0.0</version>
    </dependency>
    
  2. upload files

    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

  3. download file

    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();
            }
        }
    
Published 15 original articles · won praise 1 · views 615

Guess you like

Origin blog.csdn.net/weixin_41341130/article/details/104697665