使用CKeditor的图片上传功能

需引入FileUpLoad文件上传jar包:
commons-fileupload-1.3.2.jar
commons-io-2.4.jar

开启图片上传功能:
ckeditor --> config.js
在CKEDITOR.editorConfig = function( config )下加入:
    config.filebrowserImageUploadUrl = "imageUpload"; //开启图片上传功能,imageUpload为post请求


以下为Servlet内容(固定用法):

public class ImageUploadServlet extends HttpServlet{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//解决ajax乱码问题
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		PrintWriter out=response.getWriter();
		FileItemFactory factory=new DiskFileItemFactory();
		ServletFileUpload upload=new ServletFileUpload(factory);
		try {
			List<FileItem> list=upload.parseRequest(request);
			for (FileItem fileItem : list) {
				String imageName=DateUtil.getCurrentDateStr();
				//将图片保存至硬盘,imagePath=D:\\EclipseNeonWorkSpace\\News\\WebContent\\userImage\\
				File file=new File(PropertiesUtil.getValue("imagePath")+imageName+"."+fileItem.getName().split("\\.")[1]);
				fileItem.write(file);
				//用于ckEditor回调并显示刚上传的图片,ckEditorCallBackImagePath=userImage
				String newPath=PropertiesUtil.getValue("ckEditorCallBackImagePath")+"/"+imageName+"."+fileItem.getName().split("\\.")[1];
				String callback = request.getParameter("CKEditorFuncNum");
				out.println("<script type=\"text/javascript\">");
				out.println("window.parent.CKEDITOR.tools.callFunction(" + callback + ",'" + newPath + "',''" + ")");
				out.println("</script>");
				out.flush();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值