标签:
1.下载ckeditor后将文件夹放在webroot的js目录下;
2.springmvc配置静态文件访问
在web.xml中添加:
<servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.css</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.gif</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.jpg</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.png</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.js</url-pattern> </servlet-mapping>3.在springmvc-servlet.xml配置文件中添加文件上传支持
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="utf-8" /> <property name="maxUploadSize" value="10485760000" /> <property name="maxInMemorySize" value="40960" /> </bean>
<textarea rows="4" cols="5" name="content"></textarea> <script type="text/javascript">CKEDITOR.replace('content', { filebrowserImageUploadUrl : 'uploadImg', language : 'zh-cn', } );</script>
5.编写controller
@Controller public class FileUploadController { /* * 图片命名格式 */ private static final String DEFAULT_SUB_FOLDER_FORMAT_AUTO = "yyyyMMddHHmmss"; protected Logger logger = Logger.getLogger(FileUploadController.class); /* * 上传图片文件夹 */ private static final String UPLOAD_PATH = "upload/img/"; /* * 上传图片 */ @RequestMapping(value = "news/uploadImg") public void uplodaImg(@RequestParam("upload") MultipartFile file,// HttpServletRequest request, // HttpServletResponse response)// { try { String proPath = request.getSession().getServletContext().getRealPath("/"); String proName = request.getContextPath(); String path=proName + UPLOAD_PATH; PrintWriter out = response.getWriter(); String CKEditorFuncNum = request.getParameter("CKEditorFuncNum"); String fileName = file.getOriginalFilename(); String uploadContentType = file.getContentType(); String expandedName = ""; if (uploadContentType.equals("image/pjpeg") || uploadContentType.equals("image/jpeg")) { // IE6上传jpg图片的headimageContentType是image/pjpeg,而IE9以及火狐上传的jpg图片是image/jpeg expandedName = ".jpg"; } else if (uploadContentType.equals("image/png") || uploadContentType.equals("image/x-png")) { // IE6上传的png图片的headimageContentType是"image/x-png" expandedName = ".png"; } else if (uploadContentType.equals("image/gif")) { expandedName = ".gif"; } else if (uploadContentType.equals("image/bmp")) { expandedName = ".bmp"; } else { out.println("<script type=\"text/javascript\">"); out.println("window.parent.CKEDITOR.tools.callFunction(" + CKEditorFuncNum + ",''," + "'文件格式不正确(必须为.jpg/.gif/.bmp/.png文件)');"); out.println("</script>"); return; } if (file.getSize() > 1024 * 1024 * 2) { out.println("<script type=\"text/javascript\">"); out.println("window.parent.CKEDITOR.tools.callFunction(" + CKEditorFuncNum + ",''," + "'文件大小不得大于2M');"); out.println("</script>"); return; } DateFormat df = new SimpleDateFormat(DEFAULT_SUB_FOLDER_FORMAT_AUTO); fileName = df.format(new Date()) + expandedName; file.transferTo(new File(path + "/" + fileName)); // 返回"图像"选项卡并显示图片 request.getContextPath()为web项目名 out.println("<script type=\"text/javascript\">"); out.println("window.parent.CKEDITOR.tools.callFunction(" + CKEditorFuncNum + ",'" + "../../upload/img/" + fileName + "','')"); out.println("</script>"); return; } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } }
标签:
原文地址:http://blog.csdn.net/liuchangqing123/article/details/45270977