first commit
This commit is contained in:
260
WebRoot/plugins/ueditor/jsp/Uploader.java
Normal file
260
WebRoot/plugins/ueditor/jsp/Uploader.java
Normal file
@ -0,0 +1,260 @@
|
||||
package com.baidu.ueditor.um;
|
||||
|
||||
import java.io.*;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
import org.apache.commons.fileupload.*;
|
||||
import org.apache.commons.fileupload.FileUploadBase.InvalidContentTypeException;
|
||||
import org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException;
|
||||
import org.apache.commons.fileupload.util.*;
|
||||
import org.apache.commons.fileupload.servlet.*;
|
||||
import org.apache.commons.fileupload.FileItemIterator;
|
||||
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
|
||||
|
||||
|
||||
import sun.misc.BASE64Decoder;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
/**
|
||||
* UEditor文件上传辅助类
|
||||
*
|
||||
*/
|
||||
public class Uploader {
|
||||
// 输出文件地址
|
||||
private String url = "";
|
||||
// 上传文件名
|
||||
private String fileName = "";
|
||||
// 状态
|
||||
private String state = "";
|
||||
// 文件类型
|
||||
private String type = "";
|
||||
// 原始文件名
|
||||
private String originalName = "";
|
||||
// 文件大小
|
||||
private long size = 0;
|
||||
|
||||
private HttpServletRequest request = null;
|
||||
private String title = "";
|
||||
|
||||
// 保存路径
|
||||
private String savePath = "upload";
|
||||
// 文件允许格式
|
||||
private String[] allowFiles = { ".rar", ".doc", ".docx", ".zip", ".pdf",".txt", ".swf", ".wmv", ".gif", ".png", ".jpg", ".jpeg", ".bmp" };
|
||||
// 文件大小限制,单位KB
|
||||
private int maxSize = 10000;
|
||||
|
||||
private HashMap<String, String> errorInfo = new HashMap<String, String>();
|
||||
|
||||
public Uploader(HttpServletRequest request) {
|
||||
this.request = request;
|
||||
HashMap<String, String> tmp = this.errorInfo;
|
||||
tmp.put("SUCCESS", "SUCCESS"); //默认成功
|
||||
tmp.put("NOFILE", "未包含文件上传域");
|
||||
tmp.put("TYPE", "不允许的文件格式");
|
||||
tmp.put("SIZE", "文件大小超出限制");
|
||||
tmp.put("ENTYPE", "请求类型ENTYPE错误");
|
||||
tmp.put("REQUEST", "上传请求异常");
|
||||
tmp.put("IO", "IO异常");
|
||||
tmp.put("DIR", "目录创建失败");
|
||||
tmp.put("UNKNOWN", "未知错误");
|
||||
|
||||
}
|
||||
|
||||
public void upload() throws Exception {
|
||||
boolean isMultipart = ServletFileUpload.isMultipartContent(this.request);
|
||||
if (!isMultipart) {
|
||||
this.state = this.errorInfo.get("NOFILE");
|
||||
return;
|
||||
}
|
||||
DiskFileItemFactory dff = new DiskFileItemFactory();
|
||||
String savePath = this.getFolder(this.savePath);
|
||||
dff.setRepository(new File(savePath));
|
||||
try {
|
||||
ServletFileUpload sfu = new ServletFileUpload(dff);
|
||||
sfu.setSizeMax(this.maxSize * 1024);
|
||||
sfu.setHeaderEncoding("utf-8");
|
||||
FileItemIterator fii = sfu.getItemIterator(this.request);
|
||||
while (fii.hasNext()) {
|
||||
FileItemStream fis = fii.next();
|
||||
if (!fis.isFormField()) {
|
||||
this.originalName = fis.getName().substring(fis.getName().lastIndexOf(System.getProperty("file.separator")) + 1);
|
||||
if (!this.checkFileType(this.originalName)) {
|
||||
this.state = this.errorInfo.get("TYPE");
|
||||
continue;
|
||||
}
|
||||
this.fileName = this.getName(this.originalName);
|
||||
this.type = this.getFileExt(this.fileName);
|
||||
this.url = savePath + "/" + this.fileName;
|
||||
BufferedInputStream in = new BufferedInputStream(fis.openStream());
|
||||
File file = new File(this.getPhysicalPath(this.url));
|
||||
FileOutputStream out = new FileOutputStream( file );
|
||||
BufferedOutputStream output = new BufferedOutputStream(out);
|
||||
Streams.copy(in, output, true);
|
||||
this.state=this.errorInfo.get("SUCCESS");
|
||||
this.size = file.length();
|
||||
//UE中只会处理单张上传,完成后即退出
|
||||
break;
|
||||
} else {
|
||||
String fname = fis.getFieldName();
|
||||
//只处理title,其余表单请自行处理
|
||||
if(!fname.equals("pictitle")){
|
||||
continue;
|
||||
}
|
||||
BufferedInputStream in = new BufferedInputStream(fis.openStream());
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
|
||||
StringBuffer result = new StringBuffer();
|
||||
while (reader.ready()) {
|
||||
result.append((char)reader.read());
|
||||
}
|
||||
this.title = new String(result.toString().getBytes(),"utf-8");
|
||||
reader.close();
|
||||
|
||||
}
|
||||
}
|
||||
} catch (SizeLimitExceededException e) {
|
||||
this.state = this.errorInfo.get("SIZE");
|
||||
} catch (InvalidContentTypeException e) {
|
||||
this.state = this.errorInfo.get("ENTYPE");
|
||||
} catch (FileUploadException e) {
|
||||
this.state = this.errorInfo.get("REQUEST");
|
||||
} catch (Exception e) {
|
||||
this.state = this.errorInfo.get("UNKNOWN");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 接受并保存以base64格式上传的文件
|
||||
* @param fieldName
|
||||
*/
|
||||
public void uploadBase64(String fieldName){
|
||||
String savePath = this.getFolder(this.savePath);
|
||||
String base64Data = this.request.getParameter(fieldName);
|
||||
this.fileName = this.getName("test.png");
|
||||
this.url = savePath + "/" + this.fileName;
|
||||
BASE64Decoder decoder = new BASE64Decoder();
|
||||
try {
|
||||
File outFile = new File(this.getPhysicalPath(this.url));
|
||||
OutputStream ro = new FileOutputStream(outFile);
|
||||
byte[] b = decoder.decodeBuffer(base64Data);
|
||||
for (int i = 0; i < b.length; ++i) {
|
||||
if (b[i] < 0) {
|
||||
b[i] += 256;
|
||||
}
|
||||
}
|
||||
ro.write(b);
|
||||
ro.flush();
|
||||
ro.close();
|
||||
this.state=this.errorInfo.get("SUCCESS");
|
||||
} catch (Exception e) {
|
||||
this.state = this.errorInfo.get("IO");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件类型判断
|
||||
*
|
||||
* @param fileName
|
||||
* @return
|
||||
*/
|
||||
private boolean checkFileType(String fileName) {
|
||||
Iterator<String> type = Arrays.asList(this.allowFiles).iterator();
|
||||
while (type.hasNext()) {
|
||||
String ext = type.next();
|
||||
if (fileName.toLowerCase().endsWith(ext)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件扩展名
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private String getFileExt(String fileName) {
|
||||
return fileName.substring(fileName.lastIndexOf("."));
|
||||
}
|
||||
|
||||
/**
|
||||
* 依据原始文件名生成新文件名
|
||||
* @return
|
||||
*/
|
||||
private String getName(String fileName) {
|
||||
Random random = new Random();
|
||||
return this.fileName = "" + random.nextInt(10000)
|
||||
+ System.currentTimeMillis() + this.getFileExt(fileName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据字符串创建本地目录 并按照日期建立子目录返回
|
||||
* @param path
|
||||
* @return
|
||||
*/
|
||||
private String getFolder(String path) {
|
||||
SimpleDateFormat formater = new SimpleDateFormat("yyyyMMdd");
|
||||
path += "/" + formater.format(new Date());
|
||||
File dir = new File(this.getPhysicalPath(path));
|
||||
if (!dir.exists()) {
|
||||
try {
|
||||
dir.mkdirs();
|
||||
} catch (Exception e) {
|
||||
this.state = this.errorInfo.get("DIR");
|
||||
return "";
|
||||
}
|
||||
}
|
||||
return path;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据传入的虚拟路径获取物理路径
|
||||
*
|
||||
* @param path
|
||||
* @return
|
||||
*/
|
||||
private String getPhysicalPath(String path) {
|
||||
String servletPath = this.request.getServletPath();
|
||||
String realPath = this.request.getSession().getServletContext()
|
||||
.getRealPath(servletPath);
|
||||
return new File(realPath).getParent() +"/" +path;
|
||||
}
|
||||
|
||||
public void setSavePath(String savePath) {
|
||||
this.savePath = savePath;
|
||||
}
|
||||
|
||||
public void setAllowFiles(String[] allowFiles) {
|
||||
this.allowFiles = allowFiles;
|
||||
}
|
||||
|
||||
public void setMaxSize(int size) {
|
||||
this.maxSize = size;
|
||||
}
|
||||
|
||||
public long getSize() {
|
||||
return this.size;
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return this.url;
|
||||
}
|
||||
|
||||
public String getFileName() {
|
||||
return this.fileName;
|
||||
}
|
||||
|
||||
public String getState() {
|
||||
return this.state;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return this.title;
|
||||
}
|
||||
|
||||
public String getType() {
|
||||
return this.type;
|
||||
}
|
||||
|
||||
public String getOriginalName() {
|
||||
return this.originalName;
|
||||
}
|
||||
}
|
||||
BIN
WebRoot/plugins/ueditor/jsp/commons-fileupload-1.2.2.jar
Normal file
BIN
WebRoot/plugins/ueditor/jsp/commons-fileupload-1.2.2.jar
Normal file
Binary file not shown.
97
WebRoot/plugins/ueditor/jsp/config.json
Normal file
97
WebRoot/plugins/ueditor/jsp/config.json
Normal file
@ -0,0 +1,97 @@
|
||||
/* 前后端通信相关的配置,注释只允许使用多行方式 */
|
||||
{
|
||||
/* 上传图片配置项 */
|
||||
"imageActionName": "uploadimage", /* 执行上传图片的action名称 */
|
||||
"imageFieldName": "upfile", /* 提交的图片表单名称 */
|
||||
"imageMaxSize": 2048000, /* 上传大小限制,单位B */
|
||||
"imageAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"], /* 上传图片格式显示 */
|
||||
"imageCompressEnable": true, /* 是否压缩图片,默认是true */
|
||||
"imageCompressBorder": 1600, /* 图片压缩最长边限制 */
|
||||
"imageInsertAlign": "none", /* 插入的图片浮动方式 */
|
||||
"imageUrlPrefix": "", /* 图片访问路径前缀 */
|
||||
/*"imagePathFormat": "ueditor/jsp/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}", */
|
||||
"imagePathFormat": "/UploadFile/Info/{time}{rand:6}", /* 上传保存路径,可以自定义保存路径和文件名格式 */
|
||||
/* {filename} 会替换成原文件名,配置这项需要注意中文乱码问题 */
|
||||
/* {rand:6} 会替换成随机数,后面的数字是随机数的位数 */
|
||||
/* {time} 会替换成时间戳 */
|
||||
/* {yyyy} 会替换成四位年份 */
|
||||
/* {yy} 会替换成两位年份 */
|
||||
/* {mm} 会替换成两位月份 */
|
||||
/* {dd} 会替换成两位日期 */
|
||||
/* {hh} 会替换成两位小时 */
|
||||
/* {ii} 会替换成两位分钟 */
|
||||
/* {ss} 会替换成两位秒 */
|
||||
/* 非法字符 \ : * ? " < > | */
|
||||
/* 具请体看线上文档: fex.baidu.com/ueditor/#use-format_upload_filename */
|
||||
|
||||
/* 涂鸦图片上传配置项 */
|
||||
"scrawlActionName": "uploadscrawl", /* 执行上传涂鸦的action名称 */
|
||||
"scrawlFieldName": "upfile", /* 提交的图片表单名称 */
|
||||
"scrawlPathFormat": "/ueditor/jsp/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}",/* 上传保存路径,可以自定义保存路径和文件名格式 */
|
||||
|
||||
"scrawlMaxSize": 2048000, /* 上传大小限制,单位B */
|
||||
"scrawlUrlPrefix": "", /* 图片访问路径前缀 */
|
||||
"scrawlInsertAlign": "none",
|
||||
|
||||
/* 截图工具上传 */
|
||||
"snapscreenActionName": "uploadimage", /* 执行上传截图的action名称 */
|
||||
"snapscreenPathFormat": "/ueditor/jsp/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}", /* 上传保存路径,可以自定义保存路径和文件名格式 */
|
||||
"snapscreenUrlPrefix": "", /* 图片访问路径前缀 */
|
||||
"snapscreenInsertAlign": "none", /* 插入的图片浮动方式 */
|
||||
|
||||
/* 抓取远程图片配置 */
|
||||
"catcherLocalDomain": ["127.0.0.1", "localhost", "img.baidu.com"],
|
||||
"catcherActionName": "catchimage", /* 执行抓取远程图片的action名称 */
|
||||
"catcherFieldName": "source", /* 提交的图片列表表单名称 */
|
||||
"catcherPathFormat": "/ueditor/jsp/upload/image/{yyyy}{mm}{dd}/{time}{rand:6}", /* 上传保存路径,可以自定义保存路径和文件名格式 */
|
||||
"catcherUrlPrefix": "", /* 图片访问路径前缀 */
|
||||
"catcherMaxSize": 2048000, /* 上传大小限制,单位B */
|
||||
"catcherAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"], /* 抓取图片格式显示 */
|
||||
|
||||
/* 上传视频配置 */
|
||||
"videoActionName": "uploadvideo", /* 执行上传视频的action名称 */
|
||||
"videoFieldName": "upfile", /* 提交的视频表单名称 */
|
||||
"videoPathFormat": "/ueditor/jsp/upload/video/{yyyy}{mm}{dd}/{time}{rand:6}", /* 上传保存路径,可以自定义保存路径和文件名格式 */
|
||||
"videoUrlPrefix": "", /* 视频访问路径前缀 */
|
||||
"videoMaxSize": 102400000, /* 上传大小限制,单位B,默认100MB */
|
||||
"videoAllowFiles": [
|
||||
".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg",
|
||||
".ogg", ".ogv", ".mov", ".wmv", ".mp4", ".webm", ".mp3", ".wav", ".mid"], /* 上传视频格式显示 */
|
||||
|
||||
/* 上传文件配置 */
|
||||
"fileActionName": "uploadfile", /* controller里,执行上传视频的action名称 */
|
||||
"fileFieldName": "upfile", /* 提交的文件表单名称 */
|
||||
"filePathFormat": "/ueditor/jsp/upload/file/{yyyy}{mm}{dd}/{time}{rand:6}", /* 上传保存路径,可以自定义保存路径和文件名格式 */
|
||||
"fileUrlPrefix": "", /* 文件访问路径前缀 */
|
||||
"fileMaxSize": 51200000, /* 上传大小限制,单位B,默认50MB */
|
||||
"fileAllowFiles": [
|
||||
".png", ".jpg", ".jpeg", ".gif", ".bmp",
|
||||
".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg",
|
||||
".ogg", ".ogv", ".mov", ".wmv", ".mp4", ".webm", ".mp3", ".wav", ".mid",
|
||||
".rar", ".zip", ".tar", ".gz", ".7z", ".bz2", ".cab", ".iso",
|
||||
".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".pdf", ".txt", ".md", ".xml"
|
||||
], /* 上传文件格式显示 */
|
||||
|
||||
/* 列出指定目录下的图片 */
|
||||
"imageManagerActionName": "listimage", /* 执行图片管理的action名称 */
|
||||
/*"imageManagerListPath": "/ueditor/jsp/upload/image/", */
|
||||
"imageManagerListPath": "/../UserFiles/Image/", /* 指定要列出图片的目录 */
|
||||
"imageManagerListSize": 20, /* 每次列出文件数量 */
|
||||
"imageManagerUrlPrefix": "", /* 图片访问路径前缀 */
|
||||
"imageManagerInsertAlign": "none", /* 插入的图片浮动方式 */
|
||||
"imageManagerAllowFiles": [".png", ".jpg", ".jpeg", ".gif", ".bmp"], /* 列出的文件类型 */
|
||||
|
||||
/* 列出指定目录下的文件 */
|
||||
"fileManagerActionName": "listfile", /* 执行文件管理的action名称 */
|
||||
"fileManagerListPath": "/ueditor/jsp/upload/file/", /* 指定要列出文件的目录 */
|
||||
"fileManagerUrlPrefix": "", /* 文件访问路径前缀 */
|
||||
"fileManagerListSize": 20, /* 每次列出文件数量 */
|
||||
"fileManagerAllowFiles": [
|
||||
".png", ".jpg", ".jpeg", ".gif", ".bmp",
|
||||
".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg",
|
||||
".ogg", ".ogv", ".mov", ".wmv", ".mp4", ".webm", ".mp3", ".wav", ".mid",
|
||||
".rar", ".zip", ".tar", ".gz", ".7z", ".bz2", ".cab", ".iso",
|
||||
".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".pdf", ".txt", ".md", ".xml"
|
||||
] /* 列出的文件类型 */
|
||||
|
||||
}
|
||||
14
WebRoot/plugins/ueditor/jsp/controller.jsp
Normal file
14
WebRoot/plugins/ueditor/jsp/controller.jsp
Normal file
@ -0,0 +1,14 @@
|
||||
<%@ page language="java" contentType="text/html; charset=UTF-8"
|
||||
import="com.baidu.ueditor.ActionEnter"
|
||||
pageEncoding="UTF-8"%>
|
||||
<%@ page trimDirectiveWhitespaces="true" %>
|
||||
<%
|
||||
|
||||
request.setCharacterEncoding( "utf-8" );
|
||||
response.setHeader("Content-Type" , "text/html");
|
||||
|
||||
String rootPath = application.getRealPath( "/" );
|
||||
|
||||
out.write( new ActionEnter( request, rootPath ).exec() );
|
||||
|
||||
%>
|
||||
15
WebRoot/plugins/ueditor/jsp/getContent.jsp
Normal file
15
WebRoot/plugins/ueditor/jsp/getContent.jsp
Normal file
@ -0,0 +1,15 @@
|
||||
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
|
||||
<script src="../third-party/jquery.min.js"></script>
|
||||
<script src="../third-party/mathquill/mathquill.min.js"></script>
|
||||
<link rel="stylesheet" href="../third-party/mathquill/mathquill.css"/>
|
||||
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
|
||||
<%
|
||||
request.setCharacterEncoding("utf-8");
|
||||
response.setCharacterEncoding("utf-8");
|
||||
String content = request.getParameter("myEditor");
|
||||
|
||||
|
||||
|
||||
response.getWriter().print("<div class='content'>"+content+"</div>");
|
||||
|
||||
%>
|
||||
26
WebRoot/plugins/ueditor/jsp/imageUp.jsp
Normal file
26
WebRoot/plugins/ueditor/jsp/imageUp.jsp
Normal file
@ -0,0 +1,26 @@
|
||||
<%@ page language="java" contentType="text/html; charset=utf-8"
|
||||
pageEncoding="utf-8"%>
|
||||
<%@ page import="com.baidu.ueditor.um.Uploader" %>
|
||||
|
||||
<%
|
||||
request.setCharacterEncoding("utf-8");
|
||||
response.setCharacterEncoding("utf-8");
|
||||
Uploader up = new Uploader(request);
|
||||
up.setSavePath("upload");
|
||||
String[] fileType = {".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp"};
|
||||
up.setAllowFiles(fileType);
|
||||
up.setMaxSize(10000); //单位KB
|
||||
up.upload();
|
||||
|
||||
String callback = request.getParameter("callback");
|
||||
|
||||
String result = "{\"name\":\""+ up.getFileName() +"\", \"originalName\": \""+ up.getOriginalName() +"\", \"size\": "+ up.getSize() +", \"state\": \""+ up.getState() +"\", \"type\": \""+ up.getType() +"\", \"url\": \""+ up.getUrl() +"\"}";
|
||||
|
||||
result = result.replaceAll( "\\\\", "\\\\" );
|
||||
|
||||
if( callback == null ){
|
||||
response.getWriter().print( result );
|
||||
}else{
|
||||
response.getWriter().print("<script>"+ callback +"(" + result + ")</script>");
|
||||
}
|
||||
%>
|
||||
BIN
WebRoot/plugins/ueditor/jsp/lib/commons-codec-1.9.jar
Normal file
BIN
WebRoot/plugins/ueditor/jsp/lib/commons-codec-1.9.jar
Normal file
Binary file not shown.
BIN
WebRoot/plugins/ueditor/jsp/lib/commons-fileupload-1.3.1.jar
Normal file
BIN
WebRoot/plugins/ueditor/jsp/lib/commons-fileupload-1.3.1.jar
Normal file
Binary file not shown.
BIN
WebRoot/plugins/ueditor/jsp/lib/commons-io-2.4.jar
Normal file
BIN
WebRoot/plugins/ueditor/jsp/lib/commons-io-2.4.jar
Normal file
Binary file not shown.
BIN
WebRoot/plugins/ueditor/jsp/lib/json.jar
Normal file
BIN
WebRoot/plugins/ueditor/jsp/lib/json.jar
Normal file
Binary file not shown.
BIN
WebRoot/plugins/ueditor/jsp/lib/ueditor-1.1.2.jar
Normal file
BIN
WebRoot/plugins/ueditor/jsp/lib/ueditor-1.1.2.jar
Normal file
Binary file not shown.
BIN
WebRoot/plugins/ueditor/jsp/ueditor-mini.jar
Normal file
BIN
WebRoot/plugins/ueditor/jsp/ueditor-mini.jar
Normal file
Binary file not shown.
Reference in New Issue
Block a user