---
title: "百度编辑器UEditor 在J2EE下使用"
author: "tycomputer"
date: "2012-05-14T15:51:03Z"
tags: []
categories: ["Web前端"]
slug: "ueditor-j2ee"
draft: false
lastmod: "2012-05-14T15:51:03Z"
id: 1528403
url: "https://tycomputer.iteye.com/blog/1528403"
---

一、百度编辑器UEditor 下载，最好自定义下载，只取需要的功能，我下载时的版本为：1.2.0
<http://ueditor.baidu.com/website/download.html>

二、解压到webapp的目录ueditor里
![](http://dl.iteye.com/upload/attachment/0068/1026/ef4bdfc6-42e2-31ab-a745-32a85111ac32.png)

三、修改配置文件 image.html，将默认的上传改为jsp（约97行）

```js
url:'../../server/upload/jsp/imageUp.jsp',   
```


四、上传的imageUp.jsp有点问题，更改如下：

```java


//保存文件路径
String filePath = "ueditor/server/upload/uploadimages";
String realPath = request.getRealPath("/") + filePath;
String newFileName = "";
//判断路径是否存在，不存在则创建
File dir = new File(realPath);
if(!dir.isDirectory())
    dir.mkdir();

if(ServletFileUpload.isMultipartContent(request)){

    DiskFileItemFactory dff = new DiskFileItemFactory();
    dff.setRepository(dir);
    dff.setSizeThreshold(1024000);
    ServletFileUpload sfu = new ServletFileUpload(dff);
    FileItemIterator fii = sfu.getItemIterator(request);
    String title = "";   //图片标题
    String url = "";    //图片地址
    String fileName = "";
	String state="SUCCESS";
    while(fii.hasNext()){
        FileItemStream fis = fii.next();

        try{
            if(!fis.isFormField() && fis.getName().length()>0){
                fileName = fis.getName();
				Pattern reg=Pattern.compile("[.]jpg|png|jpeg|gif$");
				Matcher matcher=reg.matcher(fileName);
				if(!matcher.find()) {
					state = "文件类型不允许！";
					break;
				}
				newFileName = new Date().getTime()+fileName.substring(fileName.lastIndexOf("."),fileName.length());
                url = realPath+"/"+ newFileName;
                BufferedInputStream in = new BufferedInputStream(fis.openStream());//获得文件输入流
                FileOutputStream a = new FileOutputStream(new File(url));
                BufferedOutputStream output = new BufferedOutputStream(a);
                Streams.copy(in, output, true);//开始把文件写到你指定的上传文件夹                
            }else{
                String fname = fis.getFieldName();
                if(fname.indexOf("pictitle")!=-1){
                    BufferedInputStream in = new BufferedInputStream(fis.openStream());
                    byte c [] = new byte[10];
                    int n = 0;
                    while((n=in.read(c))!=-1){
                        title = new String(c,0,n);
                        break;
                    }
                }
            }

        }catch(Exception e){
            e.printStackTrace();
        }
    }
	title = title.replace("&", "&amp;").replace("'", "&qpos;").replace("\"", "&quot;").replace("<", "&lt;").replace(">", "&gt;");
    response.getWriter().print("{'url':'uploadimages/"+newFileName+"','title':'"+title+"','state':'"+state+"'}");

}
```