This article introduces how to upload image files in the java + ckeditor and some solutions.
Html code
| The Code is as follows: |
Copy code |
|
<% @ Page language = "java" contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" %>
<% @ Page import = "com. ckeditor. CKEditorConfig" %>
<% @ Page import = "java. util. *" %>
<% @ Taglib uri = "http://ckeditor.com" prefix = "ckeditor" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Link type = "text/css" rel = "stylesheet" href = "ckeditor/_ samples/sample.css"/>
<Title> CKEditor Demo </title>
</Head>
<Body>
<Form action = "CKEditor" method = "post">
<P>
<Label for = "editor1"> Editor 1: </label>
<Textarea cols = "80" id = "editor1" name = "editor1" rows = "10"> </textarea>
</P>
<P>
<Label for = "editor2"> Editor 2: </label>
<%
// String value = "My first <strong> CKEditor </strong> Java tag ";
String value = request. getParameter ("editor2") = null? "": Request. getAttribute ("editor2"). toString ();
Map <String, String> attr = new HashMap <String, String> ();
Attr. put ("rows", "8 ");
Attr. put ("cols", "50 ");
CKEditorConfig settings2 = new CKEditorConfig ();
/Settings2.addconfigvalue ("width", "500 ");
Settings2.addConfigValue ("toolbar", "Basic ");
%>
<Ckeditor: editor basePath = "ckeditor/" textareaAttributes = "<% = attr %>" config = "<% = settings2 %>" editor = "editor2" value = "<% = value %> "/>
</P>
<P> <input type = "submit" value = "Submit"/> </p>
</Form>
<%
CKEditorConfig settings = new CKEditorConfig ();
Settings. addConfigValue ("filebrowserImageUploadUrl", "imageUpload. action? Type = Image "); // upload an Image
/*
Settings. addConfigValue ("filebrowserUploadUrl", "imageUpload. action? Type = File "); // upload a File
Settings. addConfigValue ("filebrowserFlashUploadUrl", "imageUpload. action? Type = Flash "); // upload flash
*/
%>
<Ckeditor: replace = "editor1" basePath = "ckeditor/" config = "<% = settings %>"/>
</Body>
</Html> |
Main Code of the Image Upload action
| The Code is as follows: |
Copy code |
|
Public String execute () throws Exception {
Log.info ("upload image ...");
If (upload! = Null ){
ActionContext context = ActionContext. getContext ();
HttpServletRequest request = (HttpServletRequest) context. get (ServletActionContext. HTTP_REQUEST );
HttpServletResponse response = (HttpServletResponse) context. get (ServletActionContext. HTTP_RESPONSE );
String fileUrl = null;
String CKEditorFuncNum = request. getParameter ("CKEditorFuncNum ");
String scheme = request. getScheme ();
Int port = request. getServerPort ();
String serverName = request. getServerName ();
String contextPath = request. getContextPath ();
/* Rename the file name */
Calendar cal = Calendar. getInstance ();
SimpleDateFormat sdf = new SimpleDateFormat ("yyyyMMddHHmmssSSS ");
String type = uploadFileName. substring (uploadFileName. lastIndexOf ("."));
String newFileName = sdf. format (cal. getTime () + type;
String path = request. getSession (). getServletContext (). getRealPath ("/");
File destFile = new File (path + UPLOAD_PATH + "/" + newFileName );
FileUtils. copyFile (upload, destFile );
Log.info ("image uploaded successfully. ");
FileUrl = UPLOAD_PATH + newFileName;
Response. setContentType ("text/html; charset = UTF-8 ");
PrintWriter out = response. getWriter ();
Out = response. getWriter ();
FileUrl = scheme + ": //" + serverName + ":" + port + contextPath + fileUrl;
Log.info ("fileUrl:" + fileUrl );
Out. write ("<script type = 'text/javascript '> window. parent. CKEDITOR. tools. callFunction ("
+ CKEditorFuncNum + ", '" + fileUrl + "'); </script> ");
Out. flush ();
Out. close ();
} Else {
Log.info ("no files have been uploaded .");
}
Return SUCCESS;
} |
Problem Summary
The image has been uploaded successfully, but it does not jump to the preview box. However, this js Code is displayed on the original page:
| The Code is as follows: |
Copy code |
|
<Script type = 'text/javascript '> window. parent. CKEDITOR. tools. callFunction (2,'/upload/commodity/20120406/201204061740421341 .jpg ', ''); </script> |
Not executed.
I was puzzled. Later, I found that the Returned Code was escaped through Firefox:
Javascript code
| The Code is as follows: |
Copy code |
| & Lt; script type = 'text/javascript '& gt; window. parent. CKEDITOR. tools. callFunction (2,/upload/commodity/20120406/201204061740421341 .jpg ', ''); & lt;/script & gt; |
Although the problem was found, I still don't know how to solve it. After several queries, I found the problem. It turned out that the contentType format was not specified when I output the script to the foreground in the background, in this case, there is a problem with writing:
Java code
| The Code is as follows: |
Copy code |
|
HttpServletResponse response = ServletActionContext. getResponse ();
// Set headers Parameters
String fullContentType = "text/html; charset = UTF-8 ";
Response. setContentType (fullContentType );
Out = response. getWriter ();
Out. write ("<script type = 'text/javascript '> window. parent. CKEDITOR. tools. callFunction (" + this. CKEditorFuncNum
+ ", '" + GetUploadRootPath () + path + "/"
+ This. newImageFileNames [0] + "'); </script> "); |
I went there, and there was no relevant information on the Internet. I was depressed for a week.