Spring MVC and Kindeditor integration

Source: Internet
Author: User

When using the Spring MVC Framework and then using Kindeditor, there will be an upload error. The reason is that the upload.parserequest (request) value is always null. Because the request obtained at this time is the defaultmultiparthttpservletrequest type. The original processing upload pictures of the upload_json.jsp file can not be used, so kindeditor in the process of uploading images of the upload_json.jsp file to rewrite. This is the rewritten upload_json.jsp file.

<%@ page language= "java" contenttype= "text/html; Charset=utf-8″
 pageencoding= "UTF-8″%>
<%@ page import=" java.util.*,java.io.* "%>
<%@ Page import= "Java.text.SimpleDateFormat"%>
<%@ page import= "org.apache.commons.fileupload.*"%>
<%@ page import= "org.apache.commons.fileupload.disk.*"%>
<%@ page import= " Org.apache.commons.fileupload.servlet.* "%>
<%@ page import=" org.json.simple.* "%>
<%@ page Import = "org.springframework.web.multipart.support.*"%>
<%@ page import= "org.springframework.web.multipart.*"%
<%@ page import= "org.springframework.web.multipart.commons.*"%>

<%

/**
* Kindeditor JSP
*
* This JSP program is a demo program and is not recommended to be used directly in the actual project.
* If you are sure to use this program directly, please carefully confirm the relevant safety settings before use.
*
*/

File Save directory path
String Savepath = Pagecontext.getservletcontext (). Getrealpath ("/") + "attached/";

File Save Directory URL
String Saveurl = Request.getcontextpath () + "/attached/";

Define the file extensions that are allowed to be uploaded
hashmap<string, string> extmap = new hashmap<string, string> ();
Extmap.put ("image", "gif,jpg,jpeg,png,bmp");
Extmap.put ("Flash", "swf,flv");
Extmap.put ("Media", "SWF,FLV,MP3,WAV,WMA,WMV,MID,AVI,MPG,ASF,RM,RMVB");
Extmap.put ("File", "Pdf,doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2,java,php,exe,gif,jpg,jpeg,png,bmp");

Maximum file size 100M
Long maxSize = 102400000;

Response.setcontenttype ("text/html; Charset=utf-8″);

if (! Servletfileupload.ismultipartcontent (Request)) {
 out.println (geterror ("Select File. ”));
 return;
}
//Check the directory
file Uploaddir = new File (savepath);
if (!uploaddir.isdirectory ()) {
 out.println ( GetError ("The upload directory does not exist. ”));
 return;
}
//Check the directory Write permission
if (!uploaddir.canwrite ()) {
 out.println (geterror ("Upload directory does not have write permission. ”));
 return;
}

String DirName = Request.getparameter ("dir");
if (dirName = = null) {
 dirname = "image";
}
if (!extmap.containskey (DirName)) {
 out.println (the GetError ("directory name is incorrect. ”));
 return;
}
//Create folder
Savepath + = DirName + "/";
Saveurl + = DirName + "/";
File Savedirfile = new file (Savepath);
if (!savedirfile.exists ()) {
 savedirfile.mkdirs ();
}
SimpleDateFormat sdf = new SimpleDateFormat ("YyyyMMdd");
String ymd = Sdf.format (New Date ());
Savepath + = Ymd + "/";
Saveurl + = Ymd + "/";
File Dirfile = new file (Savepath);
if (!dirfile.exists ()) {
 dirfile.mkdirs ();
}

Fileitemfactory factory = new Diskfileitemfactory ();
Servletfileupload upload = new Servletfileupload (factory);
Upload.setheaderencoding ("Utf-8″");
List items = upload.parserequest (request);

Defaultmultiparthttpservletrequest mrequest= (defaultmultiparthttpservletrequest) request;

Map Map=mrequest.getfilemap ();
collection<multipartfile> C = map.values ();
Iterator it = C.iterator ();
for (; It.hasnext ();) {
Commonsmultipartfile file= (Commonsmultipartfile) it.next ();

if (!file.isempty ())
{
Long fileSize = File.getsize ();
String fileName = File.getoriginalfilename ();
String Contenttype=file.getcontenttype ();
String Fileext = filename.substring (Filename.lastindexof (".") + 1). toLowerCase ();

if (FileSize > MaxSize) {
Out.println (GetError ("The upload file size exceeds the limit of 100M. ”));
Return
}
if (! Arrays.<string>aslist (Extmap.get (DirName). Split (",")). Contains (Fileext)) {
Out.println (GetError ("The upload file extension is not allowed. \ n only allow "+ extmap.get (dirName) +" format. ”));
Return
}

Fileitem item = NULL;
SimpleDateFormat df = new SimpleDateFormat ("Yyyymmddhhmmss");
String NewFileName = Df.format (New Date ()) + "_" + New Random (). Nextint (+) + "." + Fileext;

try{
File UploadedFile = new file (Savepath, newfilename);
File.transferto (UploadedFile);
}catch (Exception e) {

Out.println (GetError ("Upload file failed. ”));
Return
}

Jsonobject obj = new Jsonobject ();
Obj.put ("error", 0);
Obj.put ("url", Saveurl + NewFileName);
Obj.put ("title", FileName);
System.out.println (FileName);
Out.println (Obj.tojsonstring ());

}
Else
{
Out.println (GetError ("Can't upload empty files! ”));
Return
}

}
%>
<%!
private string GetError (String message) {
Jsonobject obj = new Jsonobject ();
Obj.put ("Error", 1);
Obj.put ("message", message);
return obj.tojsonstring ();
}
%>

Spring MVC and Kindeditor integration

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.