Upload images to Fastdfs Distributed File system and Echo

Source: Internet
Author: User

Event, when we browse through the picture selected one, Trigger onchange event to upload the image to the server and Echo,

1 <imgwidth= "+"Height= "+"ID= "Allurl"src= "${brand.imgurl}"/>2 <inputtype= "hidden"name= "Imgurl"ID= "Imgurl"value= "${brand.imgurl}"/>3 <inputtype= "File"name= "Pic"onchange= "Uploadpic ()"/>
Uploadpic () Method:
1 //Upload Image2 functionUploadpic () {3     //Jquery.form.js4     varOptions = {5URL: "/upload/uploadpic.do",6DataType: "JSON",7Type: "POST",8Success:function(data) {9$ ("#allUrl"). attr ("src"), Data.url);Ten$ ("#imgUrl"). Val (data.url); One             } A     } -$ ("#jvForm"). Ajaxsubmit (options); -}
SPRINGMVC Configuring upload Images
1 <!--  -2     < id = "Multipartresolver"  Class= "Org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
The Fastdfs Java interface uses global configuration files fdfs_client.conf
# Connect Timeout in seconds# default value was 30sconnect_timeout=30# network timeout in seconds# default value is 30SNETW ork_timeout=60# the base path to store log filesbase_path=/home/fastdfs# Tracker_server can ocur more than once, and track Er_server format is# "Host:port", host can be hostname or IP addresstracker_server=119.29.146.187:22122#tracker_server=1 19.29.146.187:22122#standard log level as syslog, case insensitive, value list:### Emerg for emergency### alert### crit fo  R critical### error### warn for warning### notice### info### debuglog_level=info# If use connection pool# default value is false# since v4.05use_connection_pool = false# connections whose The idle time exceeds this time would be closed# Unit:se cond# default value is 3600# since v4.05connection_pool_max_idle_time = 3600# if load Fastdfs parameters from tracker Serv er# since v4.05# default value is falseload_fdfs_parameters_from_tracker=false# if use storage ID instead of IP address# s Ame as tracker.conf# ValiD if Load_fdfs_parameters_from_tracker is false# default value is false# since v4.05use_storage_id = false# Specify Storage IDs filename, can use relative or absolute path# same as tracker.conf# valid if Load_fdfs_parameters_from_ Tracker is false# since v4.05storage_ids_filename = Storage_ids.conf#http Settingshttp.tracker_server_port=80#use "# Include "directive to include HTTP other settiongs# #include http.conf

Tool class: Fastdfsutils

1  Public classFastdfsutil {2 3      Public StaticString Uploadpic (byte[] pic, String name,Longsize) {4String Path =NULL;5         //clientgloble Read configuration file6Classpathresource resource =NewClasspathresource ("fdfs_client.conf");7         Try {8Clientglobal.init (Resource.getclassloader (). GetResource ("fdfs_client.conf"). GetPath ());9             //Boss ClientTenTrackerclient trackerclient =Newtrackerclient (); OneTrackerserver Trackerserver =trackerclient.getconnection (); AStorageserver Storageserver =NULL; -StorageClient1 storageClient1 =NewStorageClient1 (Trackerserver, storageserver); -String ext =filenameutils.getextension (name); the              -namevaluepair[] Meta_list =NewNamevaluepair[3]; -Meta_list[0] =NewNamevaluepair ("FileName", name); -META_LIST[1] =NewNamevaluepair ("Fileext", ext); +META_LIST[2] =NewNamevaluepair ("FileSize", string.valueof (size)); -              +Path =storageclient1.upload_file1 (pic, ext, meta_list); A}Catch(Exception e) { at e.printstacktrace (); -         } -         returnpath; -     } -}
Save picture to Fastdfs Distributed file system
1@Service ("Uploadservice")2  Public classUploadserviceimplImplementsUploadservice {3 4     //Upload Image5      PublicString Uploadpic (byte[] pic, String name,Longsize) {6         7         returnfastdfsutil.uploadpic (pic, name, size);8     }9}

Controller

1 @Controller2  Public classUploadcontroller {3     4 @Autowired uploadservice Uploadservice;5     //Upload Image6@RequestMapping (value= "/upload/uploadpic.do")7      Public voidUploadpic (@RequestParam (required =false) Multipartfile pic,httpservletresponse response)throwsioexception{8         9String Path =Uploadservice.uploadpic (Pic.getbytes (), Pic.getoriginalfilename (), pic.getsize ());TenString URL = constants.img_url+path; OneJsonobject Jo =NewJsonobject (); AJo.put ("url", URL); -Response.setcontenttype ("Application/json;charset=utf-8"); - response.getwriter (). Write (jo.tostring ()); the     } -}

Upload images to Fastdfs Distributed File system and Echo

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.