Kindeditor3.4实现图片上传示例(JSP)

下面是用于图片上传的JSP!源码我也上传了!
/**
 * Author:QingLong  
 * Date:2010-05-18 
 * Version:1.0 Beta
 */  
<%@ page pageEncoding="gbk"%>   
<%@ page import="java.util.*,java.io.*" %>
<%@ page import="org.apache.commons.fileupload.FileItem" %>
<%@ page import="org.apache.commons.fileupload.FileUploadException" %>
<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory" %>
<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload" %>
<%@ page import="java.util.concurrent.locks.*" %>
<%   
    String id = "";   
    String url = "";   
    String imgTitle = "";   
    String imgWidth = "";   
    String imgHeight = "";   
    String imgBorder = "";   
    String filePath = "";   
    String align = "";   
    // 初始化上传工厂对象   
    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();   
            String newName = null;   
            lock.lock();   
            try {   
                //防止文件名冲突    
                newName = System.currentTimeMillis() + fileName.substring(fileName.lastIndexOf("."),fileName.length());   
            } catch (Exception e) {   
                e.printStackTrace(System.err);   
            } finally {   
                lock.unlock();   
            }   
            filePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/editor/" + "attached/" + newName;   
            FileOutputStream fos = new FileOutputStream(request.getSession().getServletContext().getRealPath("/")   
                    + "editor/" + "attached/" + 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();   
            }   
        }   
    }   
    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   
            + "','" + filePath + "','" + imgTitle + "','" + imgWidth   
            + "','" + imgHeight + "','" + imgBorder + "','" + align   
            + "');</script>");   
    out.println("</body></html>");   
%>

你可能感兴趣的:(apache,jsp,servlet)