FastDFS upload and download

The installation

Install GCC (required at compile time)

yum install -y gcc gcc-c++
Copy the code

Install Libevent (runtime requirements)

yum -y install libevent
Copy the code

Install libfastcommon

Libfastcommon is official with FastDFS. Libfastcommon contains some of the basic libraries needed to run FastDFS.

    1. Upload libfastcommon-master.zip to /opt
Run the yum install -y unzip command to install the decompressed zip package: unzip libfastcommon.zip Go to the CD libfastcommon-master directoryCopy the code
    1. compile
./make.sh
Copy the code
  • If: make.sh does not have enough permissions, you need permissions (executable rights)
chmod 777 make.sh
Copy the code
    1. The installation
./make.sh install
Copy the code
  • After libfastcommon is installed, the libfastcommon.so library file is generated in /usr/lib64

    1. Copy library files
cd /usr/lib64
cp libfastcommon.so /usr/lib
Copy the code

Install the Tracker

Download fastDFs_v5.05.tar. gz and upload it to /opt

Tar -zxvf fastdfs_v5.05.tar. gz CD FastDFS./make. Sh./makeCopy the code

The installation is successful. Copy the conf files in the installation directory to /etc/fdfs/

cp /opt/FastDFS/conf/* /etc/fdfs/
Copy the code

configuration

  • The Tracker configuration
vim /etc/fdfs/tracker.conf
Copy the code
Base_path =/home/fastdfs: /home/fastdfs: /home/fastdfs: /home/fastdfs: /home/fastdfs: /home/fastdfsCopy the code
  • Storage configuration
vim /etc/fdfs/storage.conf
Copy the code
Group_name =group1 # port=23000 # heartbeat interval to tracker (s) heart_beat_interval=30 # store_path =/home/fastdfs # store_path =/home/fastdfs # store_path # fdfS_storage #mkdir /home/fastdfs/fdfs_storage store_path0=/home/fastdfs/fdfs_storage #mkdir /home/fastdfs/fdfs_storage store_path0=/home/fastdfs/fdfs_storage The following # store_path1 =... (M01) #store_path2=..... (M02) #tracker_server= 10.1.220.247:22122 #tracker_server= 10.1.220.247:22122 # tracker_Server =10.1.220.247:22122Copy the code

Start the service

  • Start the tracker
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart
Copy the code
  • Start the storage
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart
Copy the code
  • View all running ports:
netstat -ntlp
Copy the code

Building a Java project

Create a Maven project using IDEA

pom.xml

<! Fastdfs Java client -->
<dependency>
    <groupId>net.oschina.zcx7878</groupId>
    <artifactId>fastdfs-client-java</artifactId>
    <version>1.27.0.0</version>
</dependency>
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-io</artifactId>
    <version>1.3.2</version>
</dependency>
Copy the code

Creating a Configuration File

Fastdfs-client.properties = fastdfs-client.properties = fastdfs-client.properties = fastdfs-client.properties = fastdfs-client.properties = fastdfs-client.properties = fastdfs-client.properties = fastdfs-client.properties

##fastdfs-client.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.1.220.247:22122Copy the code

File upload

package test;

import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;

/ * * *@BelongsProject: lagou-fastdfs
* @Author: GuoAn.Sun
* @CreateTime: 2020-07-28 *@Description: File upload */
public class TestUpload {
    public static void main(String[] args) {
        try {
            // Load the configuration file
            ClientGlobal.initByProperties("config/fastdfs-client.properties");

            // Create a tracker client
            TrackerClient trackerClient = new TrackerClient();
            // Get the connection service of the tracker through the tracker client and return it
            TrackerServer trackerServer = trackerClient.getConnection();
            // Declare the storage service
            StorageServer storageServer = null;
            // Define the storage client
            StorageClient1 client = new StorageClient1(trackerServer,storageServer);
            // Define file meta information
            NameValuePair[] list = new NameValuePair[1];
            list[0] = new NameValuePair("fileName"."1.jpg");

            String fileID = client.upload_file1("F:\\img\\1.jpg"."jpg", list);
            System.out.println("fileID = " + fileID);
            // group1/M00/00/00/CgHc918f8l6AFYp0AAWICfQnHuk889.jpg
            /* group1: a server is a group M00: store_path0 ----> /home/fastdfs/fdfs_storage/data 00/00: two-level data directory */
            trackerServer.close();
        }catch(Exception e){ e.printStackTrace(); }}}Copy the code

File query

package test;

import org.csource.fastdfs.*;

/ * * *@BelongsProject: lagou-fastdfs
* @Author: GuoAn.Sun
* @CreateTime: the 2020-07-28 17:46 *@Description: File query */
public class TestQuery {
    public static void main(String[] args) throws Exception {

    // Load the configuration file
    ClientGlobal.initByProperties("config/fastdfs-client.properties");

    // Create a tracker client
    TrackerClient trackerClient = new TrackerClient();
    // Get the connection service of the tracker through the tracker client and return it
    TrackerServer trackerServer = trackerClient.getConnection();
    // Declare the storage service
    StorageServer storageServer = null;
    // Define the storage client
    StorageClient1 client = new StorageClient1(trackerServer,storageServer);

    FileInfo fileInfo = client.query_file_info1("group1/M00/00/00/CgHc918f8l6AFYp0AAWICfQnHuk889.jpg");
    if(fileInfo! =null)
        System.out.println("fileInfo = " + fileInfo);
    else
        System.out.println("Check no this file!"); trackerServer.close(); }}Copy the code

File download

package test;

import org.csource.fastdfs.*;

import java.io.File;
import java.io.FileOutputStream;

/ * * *@BelongsProject: lagou-fastdfs
* @Author: GuoAn.Sun
* @CreateTime: the 2020-07-28 17:49 *@Description: File download */
public class TestDownload {
    public static void main(String[] args) throws Exception{
        // Load the configuration file
        ClientGlobal.initByProperties("config/fastdfs-client.properties");
        // Create a tracker client
        TrackerClient trackerClient = new TrackerClient();
        // Get the connection service of the tracker through the tracker client and return it
        TrackerServer trackerServer = trackerClient.getConnection();
        // Declare the storage service
        StorageServer storageServer = null;
        // Define the storage client
        StorageClient1 client = new StorageClient1(trackerServer,storageServer);

        byte[] bytes = client.download_file1("group1/M00/00/00/CgHc918f8l6AFYp0AAWICfQnHuk889.jpg");

        // Convert an array of bytes to a file via IO
        FileOutputStream fileOutputStream = new FileOutputStream(new
        File("F:/xxxxxx.jpg"));
        fileOutputStream.write(bytes);
        fileOutputStream.close();
        trackerServer.close();
        System.out.println("Download complete!"); }}Copy the code

The project of actual combat

Setting up picture Server

Nginx module installation (Storage)

  1. Upload fastdfs-nginx-module_v1.16.tar.gz to /opt
  2. Unzip the nginx module
The tar - ZXVF fastdfs - nginx - module_v1. 16. The tar. GzCopy the code
  1. Modify the config file to change the /usr/local/path to /usr/
cd /opt/fastdfs-nginx-module/src
vim config
Copy the code
  1. Copy mod_fastdfs.conf from fastdfs-nginx-module/ SRC to /etc/fdfs
cp mod_fastdfs.conf /etc/fdfs/
Copy the code
  1. Modify/etc/FDFS/mod_fastdfs. Conf
vim /etc/fdfs/mod_fastdfs.conf
Copy the code
Base_path =/home/fastdfs tracker_server=10.1.220.247:22122 #(n rows with tracker configuration) # tracker_Server =10.1.220.x:22122 Url_have_group_name =true # specify file storage path (store path configured above) store_path0=/home/fastdfs/fdfs_storageCopy the code
  1. Copy libfdfsclient.so to /usr/lib
cp /usr/lib64/libfdfsclient.so /usr/lib/
Copy the code
  1. Create the nginx/client directory
mkdir -p /var/temp/nginx/client
Copy the code

Nginx Installation (Tracker)

  1. Upload nginx-1.14.0.tar.gz to /opt (nginx installed)
  2. Decompress: tar -zxvf nginx-1.14.0.tar.gz
  3. Install dependency libraries (nginx installed, skip this step)
yum install pcre
yum install pcre-devel
yum install zlib
yum install zlib-devel
yum install openssl
yum install openssl-devel
Copy the code
  1. Go to CD /opt/nginx-1.14.0 where the nginx package is decompressed
  2. The installation
./configure \ --prefix=/usr/local/nginx \ --pid-path=/var/run/nginx/nginx.pid \ --lock-path=/var/lock/nginx.lock \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --with-http_gzip_static_module \  --http-client-body-temp-path=/var/temp/nginx/client \ --http-proxy-temp-path=/var/temp/nginx/proxy \ --http-fastcgi-temp-path=/var/temp/nginx/fastcgi \ --http-uwsgi-temp-path=/var/temp/nginx/uwsgi \ --http-scgi-temp-path=/var/temp/nginx/scgi \ --add-module=/opt/fastdfs-nginx-module/srcCopy the code

Nginx /var temp/nginx /var temp/nginx /var temp/nginx /var temp/nginx /var temp/nginx /var temp/nginx

  1. Compiler: make
  2. Install: make install
  3. Copying configuration Files
CD /opt/FastDFS/conf cp http.conf mime.types /etc/fdfs/ Overwrite: yesCopy the code
  1. Modify the nginx configuration file
cd /usr/local/nginx/conf/
vim nginx.conf
Copy the code
server {
    listen 80;
    server_name 10.1.220.247;

    #charset koi8-r;

    #access_log logs/host.access.log main;

    location /group1/M00 {
        root /home/fastdfs/fdfs_storage/data;
        ngx_fastdfs_module;
    }
Copy the code
  1. Close nginx and start nginx
pkill -9 nginx
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
Copy the code
  1. Visit nginx and view images

http://10.1.220.247 http://10.1.220.247/group1/M00/00/00/CgHc918f8l6AFYp0AAWICfQnHuk889.jpg

Create front-end pages

<%-- uploads a file --%> <%-- uploads a file --%> <%-- uploads a file --%> <%-- uploads a file --%> <%-- uploads a file --%> <form action="upload" method="post" enctype="multipart/form-data">

    <input type="file" name="fname"> <br> <button> Submit </button> </form>Copy the code

Setting up web Services

pom.xml

<packaging>war</packaging>

<dependencies>
    <! Servlet dependencies are referenced because there are JSP pages.
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <scope>provided</scope>
        <version>2.5</version>
    </dependency>
    <! -- Page submitted requests, using SpringMVC -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>5.2.7. RELEASE</version>
    </dependency>
    <! Java fastDFS client tool -->
    <dependency>
        <groupId>net.oschina.zcx7878</groupId>
        <artifactId>fastdfs-client-java</artifactId>
        <version>1.27.0.0</version>
    </dependency>
    <! Upload image to FastDFS using IO tool -->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-io</artifactId>
        <version>1.3.2</version>
    </dependency>
    <! I/O tool to save images to web server -->
    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.3.1</version>
    </dependency>
    <! -- to convert Java objects to json strings, note that version 2.7 must be used with spring5.0 or later -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.9.8</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <configuration>
                <port>8001</port>
                <path>/</path>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
Copy the code

web.xml


      
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         id="WebApp_ID" version="3.1">
    <servlet>
        <servlet-name>springMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/spring-mvc.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>springMVC</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>
Copy the code

spring-mvc.xml

<! -- Scan annotated package -->
<context:component-scan base-package="controller"/>
<! -- Scan comments in controller: @response -->
<mvc:annotation-driven/>
<! -- Upload file parser (specify upload file size limit) -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <! -- Upload file Max: 2GB-->
    <property name="maxUploadSize" value="2048000000"/>
</bean>
Copy the code

File entity class

public class FileSystem implements Serializable {
    private String fileId;
    private String filePath;
    private String fileName;
}
Copy the code

Control layer

package controller;

import entity.FileSystem;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.io.File;
import java.util.UUID;

/ * * *@BelongsProject: lagou-imageServer
* @Author: GuoAn.Sun
* @CreateTime: now * 2020-07-29@Description: Controller that processes uploaded files */
@Controller
public class FileAction {

    / * * *@paramRequest Request object * for a multi-part form@returnUpload the JSON object of the file object *@throwsException * * Upload process: * 1, save the file to the Web server * 2, upload the file from the Web server to FastDFS */

    @RequestMapping("upload")
    / / MultipartHttpServletRequest: is it improved version, not only can be filled with text information, also can install image file information
    public @ResponseBody FileSystem upload(MultipartHttpServletRequest request) throws Exception {
        FileSystem fileSystem = new FileSystem();

        /* save the file to the web server */

        // From the page request, get the uploaded file object
        MultipartFile file = request.getFile("fname");
        // Get the original file name from the file object
        String oldFileName = file.getOriginalFilename();
        // Obtain the suffix 1.jpg from the original file name by string interception
        String hou = oldFileName.substring(oldFileName.lastIndexOf(".") + 1);
        // To avoid overwriting the file with the same name, generate a new file name
        String newFileName = UUID.randomUUID().toString() + "." + hou;
        // Create a directory for the web server to save files. (create a D:/upload directory in advance, otherwise the system cannot find the path, and exceptions will be thrown.)
        File toSaveFile = new File("D:/upload/" + newFileName);
        // Convert the path to a file
        file.transferTo(toSaveFile);
        // Get the absolute path of the server
        String newFilePath = toSaveFile.getAbsolutePath();

        /* upload file from web server to FastDFS*/

        ClientGlobal.initByProperties("config/fastdfs-client.properties");
        TrackerClient trackerClient = new TrackerClient();
        TrackerServer trackerServer = trackerClient.getConnection();
        StorageServer storageServer = null;
        StorageClient1 client = new StorageClient1(trackerServer,storageServer);

        NameValuePair[] list = new NameValuePair[1];
        list[0] = new NameValuePair("fileName",oldFileName);
        String fileId = client.upload_file1(newFilePath, hou, list);
        trackerServer.close();

        // Encapsulates the fileSystem data object
        fileSystem.setFileId(fileId);
        fileSystem.setFileName(oldFileName);
        fileSystem.setFilePath(fileId); // The image has been uploaded to FastDFS and is accessed by fileId, so fileId is the file path

        returnfileSystem; }}Copy the code

Add the fastDFS configuration file

Create the config directory under Resources and fastdfs-client.properties under config

Reference: Creating a configuration file

Start the fastDFS service and the test begins

[root@localhost /]# /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart
[root@localhost /]# /usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart
[root@localhost /]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
[root@localhost /]# netstat -ntlp
[root@localhost /]# systemctl stop firewalld.service
[root@localhost /]# cd /home/fastdfs/fdfs_storage/data/
[root@localhost /]# ls
Copy the code

Typical mistakes

  • Nginx may fail to restart the Linux server:
[root@localhost logs]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
[root@localhost /]# nginx: [emerg] open() "/var/run/nginx/nginx.pid" failed (2:No such file or directory)
Copy the code
  • Nginx configuration file: nginx configuration file:
vim /usr/local/nginx/conf/nginx.conf
Copy the code
pid /usr/local/nginx/logs/nginx.pid;
Copy the code
  • Start nginx again, done!