`
only1
  • 浏览: 21808 次
  • 性别: Icon_minigender_1
  • 来自: 广西
社区版块
存档分类
最新评论

KindEditor3.4.2实现上传图片

    博客分类:
  • java
阅读更多
直接上代码
<%@ page pageEncoding="gbk"%>   
<%@page  
    import="java.util.*,java.io.*,   
    org.apache.commons.fileupload.FileItem,   
    org.apache.commons.fileupload.FileUploadException,   
    org.apache.commons.fileupload.disk.DiskFileItemFactory,   
    org.apache.commons.fileupload.servlet.ServletFileUpload,   
    java.util.concurrent.locks.*"%>   
<%   
    String id = "";   
    String url = "";   
    String imgTitle = "";   
    String imgWidth = "";   
    String imgHeight = "";   
    String imgBorder = "";   
    String filePath = "";   
    String align = "";   
     String newName = null;   
    // **************************************   
    // 初始化上传工厂对象   
    DiskFileItemFactory factory = new DiskFileItemFactory();   
    // 设置上传工厂对象限制   
    factory.setSizeThreshold(1024 * 1024 * 20);   
    factory.setRepository(new File(request.getSession(true)   
            .getServletContext().getRealPath("/")));   
    // 创建上传对象   
    ServletFileUpload upload = new ServletFileUpload(factory);   
    //upload.setFileSizeMax(1024 * 1024 * 20);   
    List<FileItem> items = null;   
    try {   
        items = upload.parseRequest(request);   
    } catch (FileUploadException e) {   
        e.printStackTrace(System.out);   
    }   
    for (Iterator<FileItem> i = items.iterator(); i.hasNext();) {   
        FileItem item = i.next();   
        if (item.isFormField()) {   
            String name = item.getFieldName();   
            String value = item.getString("gbk");   
            if (name.equals("id")) {   
                id = value;   
            } else if (name.equals("imgTitle")) {   
                imgTitle = value;   
            } else if (name.equals("imgWidth")) {   
                imgWidth = value;   
            } else if (name.equals("imgHeight")) {   
                imgHeight = value;   
            } else if (name.equals("imgBorder")) {   
                imgBorder = value;   
            } else if (name.equals("align")) {   
                align = value;   
            } else if (name.equals("url")) {   
                filePath = value;   
            }   
        } else {   
            // 取得表单域名   
            String fieldName = item.getFieldName();   
            // 取得文件名   
            String fileName = item.getName();   
            // 取得文件类型   
            String contentType = item.getContentType();   
               
            final Lock lock = new ReentrantLock();   
  
            lock.lock();   
            try {   
                //加锁为防止同一时间文件名冲突    
                newName = System.currentTimeMillis()   
                        + fileName.substring(fileName.lastIndexOf("."),   
                                fileName.length());   
            } catch (Exception e) {   
                e.printStackTrace(System.err);   
            } finally {   
                lock.unlock();   
            }   
            filePath = ".//notice/editor/updateFile/" + newName;   
            FileOutputStream fos = new FileOutputStream(request   
                    .getSession().getServletContext().getRealPath("/")   
                    + "\\notice\\editor\\updateFile\\" + newName);   
            if (item.isInMemory()) {   
                fos.write(item.get());   
                fos.close();   
            } else {   
                InputStream in = item.getInputStream();   
                byte[] buffer = new byte[1024];   
                int len;   
                while ((len = in.read(buffer)) > 0) {   
                    fos.write(buffer, 0, len);   
                }   
                in.close();   
                fos.close();   
            }   
        }   
    }   
    
    String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
			
    String path1=basePath+"notice/editor/updateFile/" + newName;
    out.println("<html><head><title>Insert Image</title><meta http-equiv='content-type' content='text/html; charset=gbk'/></head><body>");   
    out.println("<script type='text/javascript'>");   
    out.println("parent.parent.KE.plugin['image'].insert('" + id   
            + "','" + path1 + "','" + imgTitle + "','" + imgWidth   
            + "','" + imgHeight + "','" + imgBorder + "','" + align   
            + "');</script>");   
    out.println("</body></html>");   
%>  
分享到:
评论

相关推荐

    九头鸭编辑器控件 KindEditor v3.4.2 .net专用版.rar

    KindEditor v3.4.2 .NET专用版的特点在于其丰富的功能集,如文本格式化、图片上传、链接管理、表格操作、多媒体插入等,这些都是构建内容管理系统(CMS)或论坛等Web应用时常用的功能。同时,它的API接口丰富,可以...

    kindeditor-3.4.2.zip

    5. **图片上传与管理**:内置图片上传功能,支持图片预览和管理,方便用户上传和组织图片资源。 6. **视频和音频嵌入**:允许用户直接在编辑器中插入视频和音频,支持多种媒体格式。 7. **代码高亮**:对于程序员或...

    kindeditor 页面编辑器

    在 `kindeditor-3.4.2` 压缩包中,通常会包含以下内容: 1. **KindEditor.js**:核心编辑器脚本文件。 2. **lang** 文件夹:包含不同语言的本地化文件。 3. **skins** 文件夹:编辑器皮肤样式。 4. **plugins** ...

    kindeditor v3.4.3

    # KindEditor 变更记录# ver 3.4.3 * BUG: 修改了重复编辑超级链接时每次都添加&的问题。 * BUG: 修改了在IE上右键菜单没有复制、剪切项目的问题。 * BUG: 修改了在IE上没有格式化代码的问题。 * BUG: 修改了PHP上传...

    HTML在线编辑器.rar

    其特点包括插件丰富、API接口友好、支持图片上传和多媒体嵌入。版本3.4.2可能包含了一些优化和修复,以提高用户体验和稳定性。 3. **kindeditor-skin-tinymce-1.0.zip** 和 **kindeditor-skin-office-1.0.zip**: ...

    寻仙Jsp+Mysql(WAP2.0)-其他

    即将发布的电脑后台将会加入kindeditor-3.4.2使用此帮助排版,不懂html也能做出绚丽的页面程序中内置了4个div可以使用,在排版时可以使用div直接调用。为a\b\head\content这四个。(class="a")程序使用的mysql数据库,...

    寻仙Jsp+Mysql(WAP2.0) v11.1.28

    即将发布的电脑后台将会加入kindeditor-3.4.2使用此帮助排版,不懂html也能做出绚丽的页面。程序中内置了4个div可以使用,在排版时可以使用div直接调用。为aheadcontent这四个。(class="a") 程序使用的mysql数据库,...

    t淘淘商城项目 商城项目 视频和源码教程 详细

    l JSP、JSTL、jQuery、jQuery plugin、EasyUI、KindEditor(富文本编辑器)、CSS+DIV l Redis(缓存服务器) l Solr(搜索) l httpclient(调用系统服务) l Mysql l Nginx(web服务器) 2.4. 开发...

Global site tag (gtag.js) - Google Analytics