Writing in the front
[FastDFS] CentOS 8 server set up FastDFS environment error. And “FastDFS: How to implement massive distributed file storage?” article, we set up a detailed FastDFS environment. So, now that you have the environment, how do you integrate FastDFS into your project? Today, let’s talk about that question.
Note: The project source code has been submitted to: github.com/sunshinelyz… .
Compile the Java client
On Github, there is a FastDFS Java client project linked to github.com/happyfish10… .
We downloaded the Java client code locally, then went to the project directory and compiled it using Maven, as shown below.
git clone https://github.com/happyfish100/fastdfs-client-java.git
cd fastdfs-client-java
mvn clean install -Dmaven.test.skip=true
Copy the code
Next, we need to install the FastDFS Java client compilation into our local Maven repository.
MVN install: install-file-dgroupid =com. fastdfs-dartifactid = fastDFs-client-java-dversion = 1.29-dpackaging =jar - Dfile = fastdfs - the client - Java - 1.29 - the SNAPSHOT. The jarCopy the code
At this point, we compile and install the FastDFS Java client locally.
Set up the project
Edit the pom.xml file
We created a Maven project in IDEA and introduced springBoot-related dependencies and our own compiled FastDFS Java client in the POP.xml file. Finally, the dependencies of the POM.xml file are shown below.
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<skip_maven_deploy>false</skip_maven_deploy>
<java.version>1.8</java.version>
<logback.version>1.1.7</logback.version>
<slf4j.version>1.7.21</slf4j.version>
<common.logging>1.2</common.logging>
<fastjson.version>1.2.51</fastjson.version>
<fastdfs.client.version>1.29</fastdfs.client.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6. RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>${common.logging}</version>
</dependency>
<! -- log -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<dependency>
<groupId>com.fastdfs</groupId>
<artifactId>fastdfs-client-java</artifactId>
<version>${fastdfs.client.version}</version>
</dependency>
</dependencies>
Copy the code
Creating a Configuration File
(1) Create the SpringBoot configuration file application.yml in the SRC /main/resources directory of the project, as shown below.
server:
port: 9999
servlet:
context-path: /resource
tomcat:
uri-encoding: UTF-8
spring:
servlet:
multipart:
max-file-size: 1024MB
max-request-size: 1024MB
main:
allow-bean-definition-overriding: true
profiles:
include: test
active: test
output:
ansi:
enabled: detect
Copy the code
The file specifies the port to listen on after the project is started, the root path to access, the project code, the file upload size, and the runtime environment.
(2) Create the logback-spring. XML log file in the SRC /main/resources directory of the project. See the source code project for details.
(3) Create the fastdfs_client.conf file under the SRC /main/resources directory of the project, which is mainly used to configure the connection information with FastDFS.
connect_timeout = 200 network_timeout = 3000 charset = UTF-8 http.tracker_http_port = 8080 http.anti_steal_token = no Http. secret_key = FastDFS1234567890 Tracker_server = 192.168.175.100:22122Copy the code
At this point, the project is completed. Next, we will implement the functionality of the project together.
Project development
Creating a utility class
First, we create the FastDFSClientUtils utility class in the project’s io.mykit.fastdfs.utils package. Here, I give the core implementation of the tool class, and the rest of you participate in the source code project.
/ * * *@author binghe
* @descriptionFastDFS Distributed file system operation client */
public class FastDFSClientUtils {
private static Logger logger = LoggerFactory.getLogger(FastDFSClientUtils.class);
private static TrackerClient trackerClient;
public static void setFile(String filePath) {
try {
logger.info("Initialization of the Distributed file system service begins...");
if(filePath == null || filePath.trim().isEmpty()) {
filePath = "fastdfs_client.conf";
}
ClientGlobal.init(filePath);
TrackerGroup trackerGroup = ClientGlobal.g_tracker_group;
trackerClient = new TrackerClient(trackerGroup);
logger.info("Initialization of the Distributed file system service completed...");
} catch (Exception e) {
logger.error("Error loading file: {}",e ); }}/ * * *@paramThe data data *@paramExtName File extension *@returnId is returned if upload succeeds, null */ is returned if upload fails
public static String upload(byte[] data, String extName) {
TrackerServer trackerServer = null;
StorageServer storageServer = null;
StorageClient1 storageClient1 = null;
try {
NameValuePair[] meta_list = null; // new NameValuePair[0];
trackerServer = trackerClient.getTrackerServer();
if (trackerServer == null) {
logger.error("getConnection return null");
}
storageServer = trackerClient.getStoreStorage(trackerServer);
storageClient1 = new StorageClient1(trackerServer, storageServer);
String fileid = storageClient1.upload_file1(data, extName, meta_list);
return fileid;
} catch (Exception ex) {
logger.error("Failed to upload file: {}", ex);
return null;
}finally{
try {
storageClient1.close();
} catch (IOException e) {
e.printStackTrace();
}
storageClient1 = null; }}Copy the code
Create the return entity class
We create the ResourceBean class under the io.mykit.fastdfs.bean package for the SpringBoot interface to return the resulting data, as shown below.
/ * * *@author binghe
* @version 1.0.0
* @descriptionData returned after uploading the image */
public class ResourceBean implements Serializable {
private static final long serialVersionUID = -2788538880352897307L;
/** * File access path */
private String fileUrl;
/** * File name */
private String fileName;
public ResourceBean(a) {
super(a); }public ResourceBean(String fileUrl, String fileName) {
super(a);this.fileUrl = fileUrl;
this.fileName = fileName;
}
public String getFileUrl(a) {
return fileUrl;
}
public void setFileUrl(String fileUrl) {
this.fileUrl = fileUrl;
}
public String getFileName(a) {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName; }}Copy the code
The file access path fileUrl and fileName fileName are defined. That is, after the file is successfully uploaded, we will return the file access path and file name information to the client.
Creating a constant class
In IO. Mykit. Fastdfs. Constants package created under ResourcesConstants constant class, ResourcesConstants class mainly defines the basis of access to the file path and get a complete file access path method, as shown below.
/ * * *@author binghe
* @version 1.0.0
* @descriptionConstant * /
public class ResourcesConstants {
private static final String BASE_RESOURCES_URL = "http://192.168.175.100/";
public static String getResourcesUrl(String fileId) {
returnBASE_RESOURCES_URL.concat(fileId); }}Copy the code
Create a Controller class
In the project of IO. Mykit. Fastdfs. Controller package created under ResourceController class, used to define the file upload interface. The functionality of this class is also relatively simple: define a file upload interface, receive the file, and upload the file to FastDFS by calling the Upload (byte[], String) method of the FastDFSClientUtils utility class, as shown below.
/ * * *@author binghe
* @version 1.0.0
* @descriptionFile upload interface */
@RestController
@RequestMapping(value = "/resources/")
public class ResourceController {
@RequestMapping(value={"/upload"}, method=RequestMethod.POST)
@ResponseBody
public ResourceBean upload(@RequestParam("file") MultipartFile file, HttpServletRequest request, HttpServletResponse response){
String extName = "";
String fileName = "";
String originalFilename = file.getOriginalFilename();
if(originalFilename.contains(".")) {
// Split file path
String[] fileArray = originalFilename.split("\ \.");
// Get the file extension
extName = fileArray[1];
// Get the file name
fileName = fileArray[0];
}else {
fileName = originalFilename;
}
byte[] bytes = null;
try {
bytes = file.getBytes(); // Convert files to byte stream form
} catch (IOException e) {
e.printStackTrace();
}
// Call the specific method to upload the file
String fileId= FastDFSClientUtils.upload(bytes,extName);
return newResourceBean(ResourcesConstants.getResourcesUrl(fileId), fileName); }}Copy the code
Creating a startup class
In the project IO. Mykit. fastdfs package, create the project startup class ResourceStarter, as shown below.
/ * * *@author binghe
* @version 1.0.0
* @descriptionStart the * /
@SpringBootApplication
public class ResourceStarter {
public static void main(String[] args) {
try {
String filePath = "fastdfs_client.conf";
if(args.length > 0) {
filePath = args[0];
}
FastDFSClientUtils.setFile(filePath);
SpringApplication.run(ResourceStarter.class, args);
} catch (Exception e) {
// TODO Auto-generated catch blocke.printStackTrace(); }}}Copy the code
As you can see from the code, the main method of the ResourceStarter startup class defines a default filePath for the filePath variable as fastdfs_client.conf. When the project is started, parameters are passed to the main() method, The default value of filePath is overridden with the first argument, and the setFile() method of the FastDFSClientUtils class is called to pass filePath to the FastDFSClientUtils class for initialization.
Creating an HTML file
Finally, we need to create an index.html file to test the file upload operation. The contents of the index.html file are also simpler, as shown below.
<! DOCTYPEhtml>
<html>
<head>
<meta charset="utf-8">
<title>File upload and download</title>
</head>
<body>
<form action='http://192.168.175.100:9999/resource/resources/upload' method='post' enctype='multipart/form-data'>
<input type='file' name='file'>
<button type='submit'>upload</button>
</form>
</body>
</html>
Copy the code
At this point, our entire project development is complete.
Project test
Mykit-fastdfs. jar = /usr/local/ Java/mykit-fastdfs.jar = /usr/local/ Java Make a copy of the fastdfs_client.conf file in SRC /main/resources to /usr/local/java.
Enter the following command on the server command line to start mykit-fastdfs.jar.
nohup java -jar /usr/local/java/mykit-fastdfs.jar /usr/local/java/fastdfs_client.conf >> /dev/null &
Copy the code
Next, we put the index. HTML file into the HTML /test directory in the Nginx installation directory. At this point, the browser address bar, type http://192.168.175.100/test/index.html will be able to open the page.
After we upload the file to the FastDFS file system via the index.html page, the browser will display the returned result data, one is the file access path fileUrl, and the other is the fileName fileName, as shown below.
{
"fileUrl": "http://192.168.175.100/group1/M00/00/00/Ch8FQl9wfkSASTnYAACSPnJ7giA366.jpg"."fileName": "QQ screenshots 20200609234534"
}
Copy the code
The details are shown in the following figure.
Open the fileUrl our logo file access path, http://192.168.175.100/group1/M00/00/00/Ch8FQl9wfkSASTnYAACSPnJ7giA366.jpg, as shown below.
As you can see, the browser displays the uploaded image correctly, indicating that we have successfully integrated the FastDFS Java client into the project.
Big welfare
WeChat search the ice technology WeChat 】 the public, focus on the depth of programmers, daily reading of hard dry nuclear technology, the public, reply within [PDF] have I prepared a line companies interview data and my original super hardcore PDF technology document, and I prepared for you more than your resume template (update), I hope everyone can find the right job, Learning is a way of unhappy, sometimes laugh, come on. If you’ve worked your way into the company of your choice, don’t slack off. Career growth is like learning new technology. If lucky, we meet again in the river’s lake!
In addition, I open source each PDF, I will continue to update and maintain, thank you for your long-term support to glacier!!
Write in the last
If you think glacier wrote good, please search and pay attention to “glacier Technology” wechat public number, learn with glacier high concurrency, distributed, micro services, big data, Internet and cloud native technology, “glacier technology” wechat public number updated a large number of technical topics, each technical article is full of dry goods! Many readers have read the articles on the wechat public account of “Glacier Technology” and succeeded in job-hopping to big factories. There are also many readers to achieve a technological leap, become the company’s technical backbone! If you also want to like them to improve their ability to achieve a leap in technical ability, into the big factory, promotion and salary, then pay attention to the “Glacier Technology” wechat public account, update the super core technology every day dry goods, so that you no longer confused about how to improve technical ability!