Fastdfs-client-java Tool Class Package

Source: Internet
Author: User

Fastdfs is performed via storageclient to perform the upload operation.

By looking at the source code we know that Fastdfs has two storageclient tool classes.

Storageclient Upload Method Upload_file (...) Returns a string array of string[],

such as [group1,m00/00/00/wkgab1dbk2ianrayaa1riurd3es112.jpg]

StorageClient1 Upload Method Upload_file (...) Returns a string array string,

Like Group1/m00/00/00/wkgab1dbk2ianrayaa1riurd3es112.jpg, it's already been stitched up for us.

So the use of StorageClient1 upload method is more convenient, do not have our own splicing.

Fastdfsclient

public class Fastdfsclient {
Private trackerclient trackerclient = null;
Private Trackerserver trackerserver = null;
Private Storageserver storageserver = null;
Uploading using the StorageClient1
Private StorageClient1 storageClient1 = null;

Public fastdfsclient (String conf) throws Exception {
Get path to Profile "fdfs_client.conf" under Classpath path
Conf write directly relative to the position of classpath, do not need to write classpath:
String Configpath = This.getclass (). getClassLoader (). getresource (conf). GetFile ();
System.out.println (Configpath);
Clientglobal.init (Configpath);

Trackerclient = new Trackerclient ();
Trackerserver = Trackerclient.getconnection ();
Storageserver = Trackerclient.getstorestorage (trackerserver);
StorageClient1 = new StorageClient1 (Trackerserver, storageserver);
}

public string UploadFile (byte[] file_buff, String file_ext_name) throws Exception {

String result = Storageclient1.upload_file1 (File_buff, file_ext_name, NULL);

return result;
}

public string UploadFile (string local_filename, String file_ext_name) throws Exception {

String result = Storageclient1.upload_file1 (local_filename, file_ext_name, NULL);

return result;
}
}

Test

public class Fastdfstest {public    static void Main (string[] args) throws Exception {        //Do not take classpath        Fastdfsclient client = new Fastdfsclient ("properties/fdfs_client.conf");        String result = Client.uploadfile ("C:\\users\\public\\pictures\\sample pictures\\chrysanthemum.jpg", "jpg");        SYSTEM.OUT.PRINTLN (result);}    }


                                                                                        

In the project, the implementation of the picture upload

Service Layer

@Servicepublic class Pictureserviceimpl implements Pictureservice {//The IP address that needs stitching is written in the configuration file and given to the spring management @Value ("${iamge_s    Erver_base_uel} ") Private String Iamge_server_base_uel;        @Override public Pictureresult uploadpic (Multipartfile picfile) {pictureresult result = new Pictureresult ();            Determine if the picture is empty if (Picfile.isempty ()) {result.seterror (1);            Result.setmessage ("Picture is empty");        return result;            }//upload to Image server try {//Get picture extension String originalfilename = Picfile.getoriginalfilename ();            Take the extension, do not "."            String extname = originalfilename.substring (Originalfilename.lastindexof (".") + 1);            Fastdfsclient client = new Fastdfsclient ("properties/fdfs_client.conf");            String url = client.uploadfile (Picfile.getbytes (), extname);            Stitching Image Server IP address (in write config file) url = iamge_server_base_uel + URL;            Respond the URL to the client result.seterror (0);Result.seturl (URL);            } catch (Exception e) {e.printstacktrace ();            Result.seterror (1);        Result.setmessage ("image upload failed");    } return result; }}

Presentation Layer

@Controllerpublic class Picturecontroller {    @Autowired    pictureservice pictureservice;    @RequestMapping ("/pic/upload")    @ResponseBody public    pictureresult uploadfile (multipartfile uploadfile) {        Pictureresult result = Pictureservice.uploadpic (uploadfile);        return result;}    }

The test found that chrome can upload normally, but Firefox does not, the compatibility is a problem

This requires that we convert the returned data into a string literal, and that the browser has the best compatibility with strings,

Encapsulating a Tool class

Jsonutils
Import Java.util.list;import Com.fasterxml.jackson.core.jsonprocessingexception;import Com.fasterxml.jackson.databind.javatype;import Com.fasterxml.jackson.databind.objectmapper;public class JsonUtils    {//Define Jackson object private static final Objectmapper MAPPER = new Objectmapper ();     /** * Converts an object to a JSON string. */public static string Objecttojson (Object data) {try {string string = Mapper.writevalueasstring (d            ATA);        return string;        } catch (Jsonprocessingexception e) {e.printstacktrace ();    } return null;        /** * Convert JSON result set to Object */public static <T> T Jsontopojo (String jsondata, class<t> beantype) {            try {T t = mapper.readvalue (Jsondata, Beantype);        return t;        } catch (Exception e) {e.printstacktrace ();    } return null; /** * Convert JSON data to Pojo object List */public static <T>List<T> jsontolist (String jsondata, ClaSs<t> beantype) {Javatype javatype = Mapper.gettypefactory (). Constructparametrictype (List.class, BeanType);            try {list<t> List = Mapper.readvalue (Jsondata, Javatype);        return list;        } catch (Exception e) {e.printstacktrace ();    } return null; }}

Modified Controller

@Controllerpublic class Picturecontroller {    @Autowired    pictureservice pictureservice;    @RequestMapping ("/pic/upload")    @ResponseBody public    String uploadfile (multipartfile uploadfile) {        Pictureresult result = Pictureservice.uploadpic (uploadfile);        The Java object needs to be converted to JSON data        String JSON = Jsonutils.objecttojson (result);        return JSON;    }}

  

Fastdfs-client-java Tool Class Package

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.