ckeditor图片和flash实现jsp上传功能
DT的ckeditor实现文件上传功能,可是官网的ckeditor为asp和php提供了功能实现,但是对于jsp却不提供,有点小郁闷,不过,要实 现还是得自己写滴,怎么写呢?网上有很多种方法,大都是自己写代码实现,但是看了那么多还是觉得太杂,太乱,可行性不是很高,于是,终于在看到一位网友的 分享,觉得看到了实现的曙光,于是乎,自己捣鼓捣鼓,终于实现了文件的上传,包括图像和flash。
有下面几个要点,配置好了就可以实现了:
1,commons-fileupload-1.2.1.jar和commons-io-1.4.jar的导入,这两个包很好找滴,就不详述了。
2,写两个文件(可以自己定义文件名,没有规定一定要使用java_bird提供的文件名)
a,browse.jsp(用于浏览服务器的相应文件夹里面的文件)
b,upload.jsp(实现文件的上传)
下面看下具体的实现代码(代码中有些地方是需要修改的,例如:path的路径和clientpath的路径,等等,在各种测试中就会慢慢调整过来了,这个不用担心)
————-browse.jsp————
<%@page import="java.io.File"%> <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <% String path = request.getContextPath() + "/"; String type = ""; if(request.getParameter("type") != null)//获取文件分类 type = request.getParameter("type").toLowerCase() + "s/"; String clientPath = "admin/ckeditor/uploader/upload/" + type; File root = new File(request.getSession().getServletContext().getRealPath(clientPath)); if(!root.exists()){ root.mkdirs(); } String callback = request.getParameter("CKEditorFuncNum"); File[] files = root.listFiles(); out.print(files.length); if(files.length > 0){ for(File file:files ) { String src = path + clientPath + file.getName(); out.println("<img src="%E2%80%99%27" alt="’'" file="" getname="" height="’70px’" width="’110px’">"); } }else{ out.println("<h3>未检测到资源。</h3>"); } %>
———–upload.jsp———
<%@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=utf-8" pageEncoding="utf-8"%> <title>JSP上传文件</title> <% String path = request.getContextPath() + "/"; if(ServletFileUpload.isMultipartContent(request)){ String type = ""; if(request.getParameter("type") != null)//获取文件分类 type = request.getParameter("type").toLowerCase() + "/"; String callback = request.getParameter("CKEditorFuncNum");//获取回调JS的函数Num FileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload servletFileUpload = new ServletFileUpload(factory); servletFileUpload.setHeaderEncoding("UTF-8");//解决文件名乱码的问题 List<fileitem> fileItemsList = servletFileUpload.parseRequest(request); for (FileItem item : fileItemsList) { if (!item.isFormField()) { String fileName = item.getName(); fileName = "file" + System.currentTimeMillis() + fileName.substring(fileName.lastIndexOf(".")); //定义文件路径,根据你的文件夹结构,可能需要做修改 String clientPath = "admin/ckeditor/uploader/upload/" + type + fileName; //保存文件到服务器上 File file = new File(request.getSession().getServletContext().getRealPath(clientPath)); if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } item.write(file); //打印一段JS,调用parent页面的CKEditor的函数,传递函数编号和上传后文件的路径;这句很重要,成败在此一句 out.println(""); break; } } } %> </fileitem>
3,修改ckeditor的配置文件,加上以下的一段代码就可以了,具体的修改可以根据自己的文件目录不同做相应的修改!
/* Copyright (c) 2003-2010, CKSource – Frederico Knabben. All rights reserved. For licensing, see LICENSE.html or http://ckeditor.com/license */ CKEDITOR.editorConfig = function( config ) { // Define changes to default configuration here. For example: // config.language = ‘fr’; // config.uiColor = ‘#AADC6E’; config.language = ‘zh-cn’; config.skin= ‘office2003′; 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.filebrowserWindowWidth = ’640′; config.filebrowserWindowHeight = ’480′; config.toolbar_A = [ ['Source'], ['Cut','Copy','Paste','PasteText','PasteFromWord','-','Print', 'SpellChecker', 'Scayt'], ['Undo','Redo','-','Find','Replace','-','SelectAll','RemoveFormat'], ‘/’, ['Bold','Italic','Underline','Strike','-','Subscript','Superscript'], ['NumberedList','BulletedList','-','Outdent','Indent','Blockquote'], ['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'], ['Link','Unlink','Anchor'], ['Image','Flash','Table','HorizontalRule','Smiley','SpecialChar','PageBreak'], ‘/’, ['Styles','Format','Font','FontSize'], ['TextColor','BGColor'], ['Maximize', 'ShowBlocks'] ]; config.toolbar = ‘A’; };
另外,对于将browse.jsp和upload.jsp这两个文件放在哪里的问题,其实,从代码中也可以看出来,就是放在 ckeditor/uploader/下面,至于如果ckeditor下面没有upload的话,就自己建一个咯,自给自足,丰衣足食,哈哈,然后,在 ckeditor/uploader/下面新建一个upload文件夹,用于存放各种文件,至于文件存放的分类,代码会自动生成的!测试过了,可以实现 滴…..纠结完毕….O(∩_∩)O~
posted on 2012-04-11 12:05 restService 阅读(555) 评论(0) 编辑 收藏 举报