Springmvc and CKEditor Pictures upload

Source: Internet
Author: User

1. After downloading the CKEditor folder in the Webroot JS directory;

2.SPRINGMVC Configuring static file Access
Add in Web. xml:

<servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.css</ url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.gif</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name >default</servlet-name> <url-pattern>*.jpg</url-pattern> </servlet-mapping> < Servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.png</url-pattern > </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> < Url-pattern>*.js</url-pattern> </servlet-mapping>
3. Add File upload support in the Springmvc-servlet.xml configuration file

<bean id= "Multipartresolver" class= "Org.springframework.web.multipart.commons.CommonsMultipartResolver" > <property name= "defaultencoding" value= "Utf-8"/> <property name= "maxuploadsize" value= "10485760000"/> <property name= "maxinmemorysize" value= "40960"/> </bean>

4. On the page where the upload is made

<textarea rows= "4" cols= "5" name= "content" ></textarea><script type= "Text/javascript" > Ckeditor.replace (' content ', {filebrowserimageuploadurl: ' uploadimg ', language: ' ZH-CN ',});</script>

Where Filebrowserimageuploadurl is the controller's path

5. Writing a Controller

@Controllerpublic class Fileuploadcontroller {/** picture naming format */private static final String Default_sub_folder_format_auto = " Yyyymmddhhmmss ";p rotected Logger Logger = Logger.getlogger (fileuploadcontroller.class);/** upload picture folder */private static Final String Upload_path = "upload/img/";/** upload picture */@RequestMapping (value = "news/uploadimg") public void uplodaimg (@ Requestparam ("Upload") multipartfile file,//httpservletrequest request,//httpservletresponse response)//{try { String Propath = Request.getsession (). Getservletcontext (). Getrealpath ("/"); String proname = Request.getcontextpath (); String Path=proname + Upload_path; PrintWriter out = Response.getwriter (); String ckeditorfuncnum = Request.getparameter ("Ckeditorfuncnum"); String fileName = File.getoriginalfilename (); String Uploadcontenttype = File.getcontenttype (); String expandedname = ""; if (Uploadcontenttype.equals ("Image/pjpeg") | | Uploadcontenttype.equals ("Image/jpeg")) {///IE6 upload jpg image headimagecontenttype is image/pjpeg, and IE9 and Firefox upload jpg image is image/ JpegexpaNdedname = ". jpg";} else if (uploadcontenttype.equals ("Image/png") | | | uploadcontenttype.equals ("image/x-png")) {// The headimagecontenttype of IE6 uploaded png images is "image/x-png" Expandedname = ". png";} else if (uploadcontenttype.equals ("Image/gif")) {expandedname = ". gif";} else if (Uploadcontenttype.equals ("Image/bmp" ) {expandedname = ". bmp";} else {out.println ("<script type=\" text/javascript\ ">") out.println (" Window.parent.CKEDITOR.tools.callFunction ("+ Ckeditorfuncnum +", "'," + "file format is incorrect (must be. jpg/.gif/.bmp/.png file) '); O Ut.println ("</script>"); return;} if (File.getsize () > 1024x768 * 1024x768 * 2) {out.println ("<script type=\" text/javascript\ ">"); Out.println (" Window.parent.CKEDITOR.tools.callFunction ("+ Ckeditorfuncnum +", "'," + "' file size not greater than 2M ');"); O Ut.println ("</script>"); return;} DateFormat df = new SimpleDateFormat (default_sub_folder_format_auto); fileName = Df.format (New Date ()) + expandedname; File.transferto (The new File (path + "/" + FileName));//Returns the Image tab and displays the picture Request.getcontextpath() for the Web project name Out.println ("<script type=\" text/javascript\ ">"); Out.println (" Window.parent.CKEDITOR.tools.callFunction ("+ Ckeditorfuncnum +", "+"). /.. /upload/img/"+ filename+" ', ') "); Out.println (" </script> "); return;} catch (IllegalStateException e) {e.printstacktrace (),} catch (IOException e) {e.printstacktrace ();} catch (Exception e) { E.printstacktrace ();}}}



Springmvc and CKEditor Pictures upload

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.