Kingeditor (jsp) is integrated with three SSH frameworks. Solution to Image Upload Error

Source: Internet
Author: User

Recently, I used kindeditor4.1 to edit an article and found that an error occurred while uploading the flash file and media file. I cannot solve this problem. I found out that the struts framework encapsulates the request object by looking for a large amount of information. The upload_json.jsp file can be modified without any modification. the action path in the xml file can be: <filter-mapping>
<Filter-name> struts2 </filter-name>
<Url-pattern> *. action </url-pattern>
</Filter-mapping>

You can get the content in the request normally.

If you have enough time to understand and are interested, you can rewrite the following upload_json.jsp file and send me a copy by the way. Haha!

Upload_json.jsp

 
001
<% @ Page language = "java" contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" %>
002
<% @ Page import = "java. util. *, java. io. *" %>
003
<% @ Page import = "java. text. SimpleDateFormat" %>
004
<% @ Page import = "org. apache. commons. fileupload. *" %>
005
<% @ Page import = "org. apache. commons. fileupload. disk. *" %>
006
<% @ Page import = "org. apache. commons. fileupload. servlet. *" %>
007
<% @ Page import = "org. json. simple. *" %>
008
<%
009
 
010
/**
011
* KindEditor JSP
012
*
013
* This JSP program is a demo program. We recommend that you do not use it directly in the actual project.
014
* If you are sure to directly use this program, please carefully confirm the relevant security settings before using it.
015
*
016
*/
017
 
018
// Path of the file storage directory
019
String savePath = pageContext. getServletContext (). getRealPath ("/") + "attached /";
020
 
021
// File Save directory URL
022
String saveUrl = request. getContextPath () + "/attached /";
023
 
024
// Define the file extension that can be uploaded
025
HashMap <String, String> extMap = new HashMap <String, String> ();
026
ExtMap. put ("image", "gif, jpg, jpeg, png, bmp ");
027
ExtMap. put ("flash", "swf, flv ");
028
ExtMap. put ("media", "swf, flv, mp3, wav, wma, wmv, mid, avi, mpg, asf, rm, rmvb ");
029
ExtMap. put ("file", "doc, docx, xls, xlsx, ppt, htm, html, txt, zip, rar, gz, bz2 ");
030
 
031
// Maximum File Size
032
Long maxSize = 2000000000;
033
 
034
Response. setContentType ("text/html; charset = UTF-8 ");
035
 
036
If (! ServletFileUpload. isMultipartContent (request )){
037
Out. println (getError ("select a file. "));
038
Return;
039
}
040
// Check the Directory
041
File uploadDir = new File (savePath );
042
If (! UploadDir. isDirectory ()){
043
Out. println (getError ("the upload directory does not exist. "));
044
Return;
045
}
046
// Check the directory write permission
047
If (! UploadDir. canWrite ()){
048
Out. println (getError ("the upload directory has no write permission. "));
049
Return;
050
}
051
 
052
String dirName = request. getParameter ("dir ");
053
If (dirName = null ){
054
DirName = "image ";
055
}
056
If (! ExtMap. containsKey (dirName )){
057
Out. println (getError ("the directory name is incorrect. "));
058
Return;
059
}
060
// Create a folder
061
SavePath + = dirName + "/";
062
SaveUrl + = dirName + "/";
063
File saveDirFile = new File (savePath );
064
If (! SaveDirFile. exists ()){
065
SaveDirFile. mkdirs ();
066
}
067
SimpleDateFormat sdf = new SimpleDateFormat ("yyyyMMdd ");
068
String ymd = sdf. format (new Date ());
069
SavePath + = ymd + "/";
070
SaveUrl + = ymd + "/";
071
File dirFile = new File (savePath );
072
If (! DirFile. exists ()){
073
DirFile. mkdirs ();
074
}
075
 
076
FileItemFactory factory = new DiskFileItemFactory ();
077
ServletFileUpload upload = new ServletFileUpload (factory );
078
Upload. setHeaderEncoding ("UTF-8 ");
079
List items = upload. parseRequest (request );
080
Iterator itr = items. iterator ();
081
While (itr. hasNext ()){
082
FileItem item = (FileItem) itr. next ();
083
String fileName = item. getName ();
084
Long fileSize = item. getSize ();
085
If (! Item. isFormField ()){
086
// Check the file size
087
If (item. getSize ()> maxSize ){
088
Out. println (getError ("the size of the uploaded file exceeds the limit. "));
089
Return;
090
}
091
// Check the extension
092
String fileExt = fileName. substring (fileName. lastIndexOf (".") + 1). toLowerCase ();
093
If (! Arrays. <String> asList (extMap. get (dirName). split (","). contains (fileExt )){
094
Out. println (getError ("the File Upload extension is not allowed. \ N only supports the "+ extMap. get (dirName) +" format. "));
095
Return;
096
}
097
 
098
SimpleDateFormat df = new SimpleDateFormat ("yyyyMMddHHmmss ");
099
String newFileName = df. format (new Date () + "_" + new Random (). nextInt (1000) + "." + fileExt;
100
Try {
101
File uploadedFile = new File (savePath, newFileName );
102
Item. write (uploadedFile );
103
} Catch (Exception e ){
104
Out. println (getError ("failed to upload the file. "));
105
Return;
106
}
107
 
108
JSONObject obj = new JSONObject ();
109
Obj. put ("error", 0 );
110
Obj. put ("url", saveUrl + newFileName );
111
Out. println (obj. toJSONString ());
112
}
113
}
114
%>
115
<%!
116
Private String getError (String message ){
117
JSONObject obj = new JSONObject ();
118
Obj. put ("error", 1 );
119
Obj. put ("message", message );
120
Return obj. toJSONString ();
121
}
122
%>


Author: larch

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.