Amoy Mall Series--test of uploading pictures using Fastdfs-client client

Source: Internet
Author: User

http://blog.csdn.net/yerenyuan_pku/article/details/72804018

Not long ago, we realized the category of product selection this function, but this is only the first step of Long March, we have a lot of things to do, such as how to achieve the image upload function. This article is to teach you how to achieve image upload.

Image upload Analysis

We know that for a traditional project, all modules are developed in one project, including all static resource files, and so on, on this tomcat server, as shown in.  

This is not a problem if there is a small amount of traffic, but for Internet projects, there is a lot of user access, so a tomcat server is far from meeting business needs. This requires the deployment of a tomcat cluster, where a cluster requires load balancing, and we typically use Nginx as the Load Balancer server, as shown in.  

But the disadvantages of this tomcat cluster are also obvious, If we put a picture of A.jpg uploaded to the TOMCAT1 images directory, because Nginx is responsible for balancing processing requests, when the user to request access to this picture, assuming the first time, Nginx put the request to TOMCAT1 to deal with, it to their images directory down to find this picture, found to be Can be found, so we can see this picture, when we go through Nginx to request access to the image, Nginx put the request to TOMCAT2 to deal with, then TOMCAT2 to its own images directory down to find this picture, found that there is no this picture, Therefore, the picture is not visible on the page. As a user, a visit can see, and then refresh can not see, and then refresh to see, the user will not understand, the intuitive feeling is that our system is too rotten.  
for the problems mentioned above, we make improvements to the cluster, we specialize in a picture server, all Tomcat will upload images uploaded to the image server, tomcat itself does not save the picture. We use HTTP to access the image, so that we need an HTTP server, can be used as an HTTP server has many options.

    1. Tomcat can act as an HTTP server, but because the Tomcat server's strength is not to handle static resources (its strength is to handle dynamic pages such as Servlets and JSPs), we do not choose Tomcat.
    2. Using Apache as an HTTP server, Apache is a server written in the C language (note that this is not the Apache organization, but just a server), the server in the previous use of a lot of people, but now use fewer people.
    3. Using Nginx,nginx because of its unique advantage, as the HTTP server is currently the hottest. We use Nginx to unify the management of these images, so that the user to access the picture, Nginx directly to the image on the server to return the image, so as to solve the Tomcat cluster resources can not be shared problems.

But here is a problem, that is, as a server, the capacity is certainly limited, when the server is full, how to do? There is the picture server hangs, how to do? These are the problems that must be solved, and in order to solve these two problems, we use the Fastdfs cluster to solve them. Fastdfs is an open source lightweight Distributed File system, which manages files, including file storage, file synchronization, file access (file upload, file download), and so on, which solves the problem of large capacity storage and load balancing. Especially suitable for file-based online services, such as photo album sites, video sites and so on. Its advantage is the ability to expand horizontally, FASTDFS storage resources devices are differentiated by the group, when the storage space is insufficient, you can increase the level of grouping and add equipment to achieve the purpose of expansion, and there is no limit. It also has an advantage is high availability, that is to say, Fastdfs cluster can do when the service of Nginx failure, automatically switch to another nginx device, to ensure the stability of services.

installation of picture server

In this reminds everyone to build Fastdfs cluster is very very troublesome, at least I now will not, so, as an exercise, we only need to build a single version of the Fastdfs can. One of the simplest FASTDFS architectures is as follows:

This architecture is what I provide you with a picture server architecture, of course, this simplest architecture in the real production environment is not enough. Because the picture server installation process is very complex, it does not need to be mastered, so I will provide you with a installed image server. You can refer to my article Amoy Amoy Mall series--vmware Add a configured virtual machine to learn.

Uploading pictures using the Fastdfs-client client

I'll use fastdfs-client to upload and download pictures, but before we do, we need to do two things first.
The first thing: because the Central warehouse does not have a fastdfs-client package, we need to complete it ourselves. Here I provide a Maven project about Fastdfs-client, as shown in.

All we have to do is import the MAVEN project from Fastdfs-client into Eclipse, as shown, and after the import is done, the MAVEN project will be packaged into the local MAVEN repository.

Now, is everything all right? Obviously not, we directly through the performance layer upload pictures to the image server, can not go through the service layer, so taotao-manager-web engineering needs to rely on Fastdfs-client engineering, Therefore, you need to add a dependency to fastdfs-client in the Pom.xml file of the Taotao-manager-web project, as shown below.

<dependency>    <groupId>fastdfs_client</groupId> <artifactId>fastdfs_client</artifactId> <version>1.25</version></dependency>
    • 1

The second thing: Create a new resource folder under the Src/main/resources directory of the Taotao-manager-web project and build the fast_dfs.conf file underneath it fast_ In the dfs.conf file, enter the IP and port of the device where tracker resides, because my tracker is on a virtual machine with IP 192.168.25.133, so I'm writing "tracker_server= 192.168.156.13:22122 ", as shown in.

Here we create a new test class for the picture upload test, we create a new Com.taotao.fdfs package under the Src/test/java directory, and create a new test class Testfastdfs.java under the package, as shown in.

To facilitate replication, the test code is now pasted as follows:

PublicClass Testfastdfs {@TestPublicvoidTestupload () throws Exception {1. First create a profile--fast_dfs.conf, the contents of the configuration file is to specify the Trackerserver address2. Use the global method to load the configuration file Clientglobal.init ("F:/java/my-taotao/taotao-manager-web/src/main/resources/resource/fast_dfs.conf");3. Create a Trackerclient object trackerclient trackerclient =New Trackerclient ();4. Obtain Trackerserver object Trackerserver trackerserver = Trackerclient.getconnection () by Trackerclient object;//5. Create a reference to Storageserver, null to storageserver storageserver = null; ///6. Create a Storageclient object that requires two parameters, one trackerserver, one storageserver storageclient storageclient = New Storageclient (Trackerserver, storageserver); //7. Uploading Files using Storageclient object (image) //Parameter 1: File name, parameter name: extension, cannot contain ".", Parameter 3: file's metadata, save file's original name, size, size, etc., if not nullable string[] strings = Storageclient.upload_file ("f:/fastdfs_test/meinv.jpg", "jpg", null); For (String string:strings) {System.  Out.println (string);             }}

The above code has two points to note:

    1. Taotao-manager-web project has not added a dependency on JUnit, Therefore, it is necessary to add a dependency to JUnit in the Pom.xml file, as shown below, because it is not possible to write a version because it has been uniformly defined in the Taotao-parent project.

      <dependency> <groupId>junit</groupid> < Artifactid>junit</artifactid> </DEPENDENCY>              
    2. When we copy and paste the absolute path to the local file of the WIN10 system (for example, F:\fastdfs_test\meinv.jpg ), Eclipse is not recognized and the operation will report the following error. The workaround is to enter manually, and be careful to delete the path string including the double quotation marks ( "e:/images/2.jpg" ) and enter it manually.  

Note that after two points above, we can execute this test method successfully, as shown in. The first line of ECHO information is which group the picture is saved to, since we are now just using a standalone FASTDFS server, so now it is group1. The second line is the exact location of the storage.

Now that we've uploaded the image, we're trying to access the image using HTTP, and we need to put group1 and m00/00/00/ Wkgzhvksg-6aore0aafkkatucz8885.jpg stitching to a piece to access, so we can see the photos we just uploaded, as shown in.

After the above operation, we set up the picture server is no problem. However, the problem is that the image upload process is cumbersome, so we urgently need to encapsulate it, and now I have packaged the contents of the tool class--fastdfsclient.java paste as follows.

PublicClassfastdfsclient {Private Trackerclient trackerclient =NullPrivate Trackerserver Trackerserver =NullPrivate Storageserver Storageserver =NullPrivate StorageClient1 storageclient =NullPublicFastdfsclient (String conf)Throws Exception {if (Conf.contains ("Classpath:")) {conf = Conf.replace ("Classpath:",This.getclass (). GetResource ("/"). GetPath ()); } clientglobal.init (conf); Trackerclient =New Trackerclient (); Trackerserver = Trackerclient.getconnection (); Storageserver =Null Storageclient =New StorageClient1 (Trackerserver, storageserver); }/** * Upload File Method * <p>Title:uploadFile</p> * <p>description: </p> *@param filename File Full path *@param extname file extension, not including (.) *@param metas File extension information *@return *@throws Exception * *Public StringUploadFile (String fileName, String extname, namevaluepair[] metas)Throws Exception {String result = Storageclient.upload_file1 (FileName, Extname, Metas);return result; }Public StringUploadFile (String fileName)Throws Exception {Return UploadFile (FileName,NullNULL); }Public StringUploadFile (String fileName, String extname)Throws Exception {Return UploadFile (FileName, Extname,NULL); }/** * Upload File Method * <p>Title:uploadFile</p> * <p>description: </p> *@param the contents of the Filecontent file, byte array *@param extname File extension *@param metas File extension information *@return *@throws Exception * *Public Stringuploadfile (byte[] filecontent, String extname, NameValuePair[] Metas) throws Exception {String result = Storageclient.upload_file1 (Filecontent, Extname, Metas); return result; public String uploadfile (byte[] filecontent) throws Exception {return uploadfile (fileContent , null, null);} public String uploadfile (byte[] Filecontent, String extname) throws Exception {return UploadFile (Filecontent, Extname, null);}        

The tool class constructs the method in the

conf = conf.replace("classpath:", this.getClass().getResource("/").getPath());

This means that if the user passes in the file path is a relative path (relative path to the Src/main/resources directory as the root directory), such as the user's incoming file path is "Classpath:applications.properties", Then we need to change to absolute path, so we need to replace "classpath:" to f:/java/my-taotao/taotao-manager-web/src/main/resources. And the storage client used in the encapsulation class is StorageClient1 instead of storageclient. The advantage of this client is that we can automatically stitch together the groups and locations where the files are located.  
We create a new Com.taotao.utils toolkit under the Taotao-manager-web project and put our encapsulation class Fastdfsclient.java below. Let's test whether this tool class works, and we'll create a new test method testfastdfsclient in the TESTFASTDFS unit test class, as shown in.   the

Testfastdfsclient Unit test method code is as follows:

@Testpublic void testFastDfsClient() throws Exception {    FastDFSClient fastDFSClient = new FastDFSClient("F:/Java/my-taotao/taotao-manager-web/src/main/resources/resource/fast_dfs.conf"); String string = fastDFSClient.uploadFile("F:/fastdfs_test/a.jpg"); // 注意,你再上传一遍,刚才你上传的那个图片就已经丢了,它已经存在服务器上了,你再也访问不到了 System.out.println(string);}

We run the Testfastdfsclient method and return the results as shown.

Let's visit this picture as shown in.

(turn) Amoy Mall series--use fastdfs-client client to upload picture test

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.