Allow ckeditor to support JSP upload

Source: Internet
Author: User

A long time ago, I wanted to write down the methods for uploading ckeditor images in JSP and browsing server images. However, because it is necessary for teaching projects, I am worried that hem will be applied directly, I don't want to investigate it myself (but I am very careful). I can't do a good job of training and have never written it out. Today, the project started testing and their functions can basically be completed, I can also post my post.

The reason for writing this is my friend's post on the Internet. He complained that ckeditor does not support JSP. He lamented many times that JSP is not supported when ASP and PHP are supported, so I also looked for JSP information on the Internet, which does not seem to be supported, but people also make sense. After all, the images uploaded by JSP are, you can't simply use a JSP.

There are many ways for ckeditor to upload images under JSP, many of which are written in JavascriptCodeThen I registered an event myself and wrote too much. I didn't understand it. However, a man wrote something that showed me the dawn of simple writing. (unfortunately, it took too long, I do not know the URL of Hero ).

To put it bluntly, we have done a good job of uploading ckeditor. We only need to prepare a function to receive the files submitted by ckeditor. So the implementation idea is as follows:

 

    1. Prepare the jar package for JSP file upload: commons-fileupload.jar and commons-io.jar
    2. Compile a JSP to receive the uploaded file (in addition to the image upload function, you need to call a core JS Statement)
    3. Compile a JSP for viewing files (in addition to the image upload function, you need to call a core JS Statement)
    4. Modify the config. js of ckeditor and add the JSP configuration of the uploaded and browsed files.

Note that JSP is not used because JSP is simple, which can reduce ckeditor's project intrusion. See the code below:

JSP used to receive uploaded files: Java code
  1. <%@ Page Import="Java. Io. File"%>
  2. <%@ Page Import="Java. util. UUID"%>
  3. <%@ Page Import="Org. Apache. commons. fileupload. fileitem"%>
  4. <%@ Page Import="Java. util. List"%>
  5. <%@ Page Import="Org. Apache. commons. fileupload. disk. diskfileitemfactory"%>
  6. <%@ Page Import="Org. Apache. commons. fileupload. fileitemfactory"%>
  7. <%@ Page Import="Org. Apache. commons. fileupload. servlet. servletfileupload"%>
  8. <% @ Page Language ="Java"Contenttype ="Text/html; charset = gb18030"Pageencoding ="Gb18030"%>
  9. <! Doctype HTML public"-// W3C // dtd html 4.01 transitional // en" Http://www.w3.org/TR/html4/loose.dtd">
  10. <HTML>
  11. <Head>
  12. <Meta http-equiv ="Content-Type"Content ="Text/html; charset = gb18030">
  13. <Meta http-equiv ="Pragma"Content ="No-Cache">
  14. <Meta http-equiv ="Cache-control"Content ="No-Cache">
  15. <Meta http-equiv ="Expires"Content ="0">
  16. <Title> JSP file upload </title>
  17. </Head>
  18. <Body>
  19. <%
  20. String Path = request. getcontextpath () +"/";
  21. If(Servletfileupload. ismultipartcontent (request )){
  22. String type ="";
  23. If(Request. getparameter ("Type")! =Null)// Obtain the file category
  24. Type = request. getparameter ("Type"). Tolowercase () +"/";
  25. String callback = request. getparameter ("Ckeditorfuncnum");// Obtain the callback JS function num
  26. Fileitemfactory factory =NewDiskfileitemfactory ();
  27. Servletfileupload =NewServletfileupload (factory );
  28. Servletfileupload. setheaderencoding (UTF-8");// Solve the problem of garbled file names
  29. List <fileitem> fileitemslist = servletfileupload. parserequest (request );
  30. For(Fileitem item: fileitemslist ){
  31. If(! Item. isformfield ()){
  32. String filename = item. getname ();
  33. Filename ="File"+ System. currenttimemillis () + filename. substring (filename. lastindexof ("."));
  34. // Define the file path, which may need to be modified based on your folder structure
  35. String clientpath ="Ckeditor/Uploader/upload /"+ Type + filename;
  36. // Save the file to the server
  37. File file =NewFile (request. getsession (). getservletcontext (). getrealpath (clientpath ));
  38. If(! File. getparentfile (). exists ()){
  39. File. getparentfile (). mkdirs ();
  40. }
  41. Item. Write (File );
  42. // Print a piece of JS Code, call the ckeditor function on the parent page, and pass the function number and the path of the uploaded file. This sentence is very important.
  43. Out. println ("<SCRIPT type = 'text/JavaScript '> window. Parent. ckeditor. Tools. callfunction ("+ Callback +",'"+ Path + clientpath +"') </SCRIPT>");
  44. Break;
  45. }
  46. }
  47. }
  48. %>
  49. </Body>
  50. </Html>
<% @ Page import = "Java. io. file "%> <% @ page import =" Java. util. UUID "%> <% @ page import =" org. apache. commons. fileupload. fileitem "%> <% @ page import =" Java. util. list "%> <% @ page import =" org. apache. commons. fileupload. disk. diskfileitemfactory "%> <% @ page import =" org. apache. commons. fileupload. fileitemfactory "%> <% @ page import =" org. apache. commons. fileupload. servlet. servletfileupload "%> <% @ page Language =" Java "contenttype = "Text/html; charset = gb18030" pageencoding = "gb18030" %> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" "http://www.w3.org/TR/html4/loose.dtd"> <HTML> 
JSP used to browse files: Java code

 
  1. <%@ Page Import="Java. Io. File"%>
  2. <% @ Page Language ="Java"Contenttype ="Text/html; charset = gb18030"
  3. Pageencoding ="Gb18030"%>
  4. <! Doctype HTML public"-// W3C // dtd html 4.01 transitional // en" Http://www.w3.org/TR/html4/loose.dtd">
  5. <HTML>
  6. <Head>
  7. <Meta http-equiv ="Content-Type"Content ="Text/html; charset = gb18030">
  8. <Meta http-equiv ="Pragma"Content ="No-Cache">
  9. <Meta http-equiv ="Cache-control"Content ="No-Cache">
  10. <Meta http-equiv ="Expires"Content ="0">
  11. <Title> Image Browsing </title>
  12. <SCRIPT type ="Text/JavaScript">
  13. // This function is important. Otherwise, you cannot interact with ckeditor.
  14. Function funcallback (funcnum, fileurl ){
  15. VaR parentwindow = (window. Parent = Window )? Window. opener: window. parent;
  16. Parentwindow. ckeditor. Tools. callfunction (funcnum, fileurl );
  17. Window. Close ();
  18. }
  19. </SCRIPT>
  20. </Head>
  21. <Body>
  22. <%
  23. String Path = request. getcontextpath () +"/";
  24. String type ="";
  25. If(Request. getparameter ("Type")! =Null)// Obtain the file category
  26. Type = request. getparameter ("Type"). Tolowercase () +"/";
  27. String clientpath ="Ckeditor/Uploader/upload /"+ Type;
  28. File root =NewFile (request. getsession (). getservletcontext (). getrealpath (clientpath ));
  29. If(! Root. exists ()){
  30. Root. mkdirs ();
  31. }
  32. String callback = request. getparameter ("Ckeditorfuncnum");
  33. File [] files = root. listfiles ();
  34. If(Files. length>0){
  35. For(File file: Files ){
  36. String src = path + clientpath + file. getname ();
  37. Out. println ("+ SRC +"'Alt = '"+ File. getname () +"'Onclick = \" funcallback ("+ Callback +",'"+ SRC +"') \">");
  38. }
  39. }Else{
  40. Out. println ("<H3> no resource is detected. </H3>");
  41. }
  42. %>
  43. </Body>
  44. </Html>
<% @ Page import = "Java. io. file "%> <% @ page Language =" Java "contenttype =" text/html; charset = gb18030 "pageencoding =" gb18030 "%> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" "http://www.w3.org/TR/html4/loose.dtd"> <HTML> 
The modified ckeditor config. JS: JS Code

  
  1. Ckeditor. editorconfig =Function(Config)
  2. {
  3. Config. Language ='Zh-cn';
  4. Config. filebrowserbrowseurl ='Ckeditor/Uploader/browse. jput';
  5. Config. filebrowserimagebrowseurl ='Ckeditor/Uploader/browse. jsp? Type = images';
  6. Config. filebrowserflashbrowseurl ='Ckeditor/Uploader/browse. jsp? Type = flashs';
  7. Config. filebrowseruploadurl ='Ckeditor/Uploader/upload. jput';
  8. Config. filebrowserimageuploadurl ='Ckeditor/Uploader/upload. jsp? Type = images';
  9. Config. filebrowserflashuploadurl ='Ckeditor/Uploader/upload. jsp? Type = flashs';
  10. Config. filebrowser1_wwidth ='123';
  11. Config. filebrowserwindowheight ='123';
  12. }
 
Ckeditor. editorconfig = function (config) {config. language = 'zh-cn'; config. filebrowserbrowseurl = 'ckeditor/Uploader/browse. JSP '; config. filebrowserimagebrowseurl = 'ckeditor/Uploader/browse. JSP? Type = images '; config. filebrowserflashbrowseurl = 'ckeditor/Uploader/browse. jsp? Type = flashs '; config. filebrowseruploadurl = 'ckeditor/Uploader/upload. jsp'; config. filebrowserimageuploadurl = 'ckeditor/Uploader/upload. jsp? Type = images '; config. filebrowserflashuploadurl = 'ckeditor/Uploader/upload. jsp? Type = flashs '; config. filebrowser1_wwidth = '000000'; config. filebrowser1_wheight = '000000 ';}

OK. The modification is complete. It's easy to upload and browse files (the above is just a demonstration and the code is for reference only). The main point is to callCkeditor. Tools. callfunctionMethod.

The attachment is packaged with a runable Eclipse project for your reference.

Bytes ---------------------------------------------------------------------------------------

Image preview:

Folder structure

Upload effect:


 
 

    • Size: 17 KB
    • Ckediter.rar (832.7 KB)

 

Related Article

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.