FastDFS upload file Demo--springboot implementation

Develop a habit, like first and then watch! ! !

1 Introduction

Today I followed the tutorial and finally realized uploading files to FastDFS through springboot. I am still a little excited. I will share the process today and hope to help everyone.
Friends who don’t know how to install FastDFS or how to integrate Nginx, you can read my previous two articles:
Distributed File Storage System fastdfs installation tutorial
Integrate FastDFS and Nginx, so that the generated file URL can be accessed through a browser

2. Steps

2.1clone and import the client to the project

Because FastDFS is not like our other frameworks, you can download its corresponding dependencies directly from the Maven repository, and then follow the tutorial directly. It is necessary for us to clone its client from Git and import it to us The project can be used directly.
So we first need to clone the client.
Find our Git tool, then run it as an administrator, and then enter the following command to clone the client first

git clone https://github.com/happyfish100/fastdfs-client-java

Insert picture description here
We will see such a file under the Git directory:
Insert picture description here
This is the FastDFS client, and then we will copy this client to our project, but at this time it is not recognized as a Maven project, so we still need To import it into a Maven project, follow the steps below:
Insert picture description here
Insert picture description here
Then we need to install this project to our local warehouse. This step is optional, but it is best to install it, otherwise the project may not be recognized sometimes.
Insert picture description here
The address in the red box above is the address where it was installed

2.2 Import FastDFS client dependencies

Insert picture description here
Copy this code into the module you need to upload using file.这里有个小坑,到后面我会告诉大家怎么解决。

2.3 Create and configure Tracker configuration information

Create a configuration file under the resource folder of the module that requires file upload

#tracker_server服务的地址及端口号
tracker_server=tracker服务的IP:22122

#连接超时间,默认是30秒
connect_timeout=30000

#网络通讯超时时间,默认是60秒
network_timeout=60000

Insert picture description here

2.4 Tools for writing uploaded files

Then we can formally write our tool class

public static String uploadImage(MultipartFile multipartFile) throws IOException {
    
    
//这里是我定义的常量类
        ConstantUtil constantUtil=new ConstantUtil();
        String ImageUrl= constantUtil.getImageUrl();
        //获取tracker的配置文件路径
        String tracker=PmsUploadUtil.class.getResource("/tracker.conf").getPath();
        //读取配置文件
        try{
    
    
            ClientGlobal.init(tracker);
        }catch (Exception e){
    
    
            e.printStackTrace();
            System.out.println("配置实例化失败");
        }
        TrackerClient trackerClient=new TrackerClient();
        TrackerServer trackerServer=trackerClient.getTrackerServer();
        //创建一个StorageClient对象,需要两个参数TrackerServer对象、StorageServer的引用
        StorageClient storageClient = new StorageClient(trackerServer, null);
        //获取文件的byte数组
        byte[]bytes=multipartFile.getBytes();
        //获取文件的后缀名
        String multipartFileName=multipartFile.getOriginalFilename();
        int index=multipartFileName.lastIndexOf(".");
        String extNamne=multipartFileName.substring(index+1);
        //使用StorageClient对象上传图片;扩展名不带“.”
        try {
    
    
            String[] strings = storageClient.upload_file(bytes, extNamne, null);
            //返回数组。包含组名和图片的路径。重组成URL链接
            for (String string : strings) {
    
    
                ImageUrl+="/"+string;
            }
        }catch (Exception e){
    
    
            e.printStackTrace();
        }

        return ImageUrl;
    }

There is a note that point FastDFS1.27and the FastDFS1.29two versions are slightly different

在1.27的版本里面:
我们获取TrackerServer是通过trackerClient.getConnection()来获取的,
但是在1.29的版本里面:
我们获取TrackerServer是通过trackerClient.getTrackerServer()来获取的,

Secondly, the upload functions in FastDFS are mainly the following two,

Insert picture description here
Only the first parameter of these two functions is different. The first is to upload the file in the form of binary data, and the second is to upload the file through the file path of the file we passed in. Here I choose to upload the file through the first method. Here, you can choose according to your actual needs. At this point, we have finished writing the upload of the file, and then we can test it, but I am sorry, if nothing else, you may encounter such a bug:Detected both log4j-over-slf4j.jar AND slf4j-log4j12.jar on the class path, preempting StackOverflowError

This bug means that your project is used at the same time log4j-over-slf4j.jar 和 slf4j-log4j12.jar. It happens that these two packages are in conflict, so we must remove one of them, but our previous projects can run normally, so it should be a FastDfS customer. The terminal should be a reference to one of the above dependencies, so here we can re-enter the pom dependencies of the FastDFS client to see what his dependencies are. We can see the following figure: The
Insert picture description here
explanation is because FastDFS is introduced again One of them, so we need to remove the dependency in the dependency of importing fastDFS

<dependency>
    <groupId>org.csource</groupId>
    <artifactId>fastdfs-client-java</artifactId>
    <version>1.29-SNAPSHOT</version>
    <exclusions>
        <exclusion>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
        </exclusion>
    </exclusions>
</dependency>

So our project can basically run

3. Upload the demo

Here we can carry out the actual test

Insert picture description here
You can see that the picture was indeed uploaded, and the URL address of the picture generated by FastDFS was returned to us in the background, and we can indeed access it through the browser.

The code word is not easy, if you think it is helpful to you, you can follow my official account, the newbie UP needs your support! ! !
Insert picture description here

Guess you like

Origin blog.csdn.net/lovely__RR/article/details/109580760