笔者用的kindeditor的版本为kindeditor-v4.1.7,struts2版本2.3..8。虽然网上已有例子了,但还是想再说两句。
把kindeditor解压放在webroot中(解压后文件夹名有版本号,已被删掉)。如图:
文夹里报错,这是代码不规范导至,可以不理会,程序能正常运行。上面的文件中已把其他的语言文件夹删掉,只剩下jsp这个文件夹。其实这个文件夹也可以删掉,但是为了以后方便查看配置示例,于是把它留了下来。
根据他示例中的写法先把JSP配置完成。
sysNew.content为textarea的name属性。从配置中可以看到有两个地址我们要实现,一个是uploadJson与fileManagerJson
uploadJson是上传用的ACTION地址,fileManagerJson是管理文件用的
配置好后我两建两个action(笔者用的struts2注解),第一个先建uploadJson这个action:
package com.action.upload;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Random;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import com.action.JsonBaseAction;
public class UploadJsonAction extends JsonBaseAction{
private List<File> imgFile;
private List<String> imgFileContextType;
private List<String>imgFileFileName;
private String dir;
public List<File> getImgFile() {
return imgFile;
}
public void setImgFile(List<File> imgFile) {
this.imgFile = imgFile;
}
public List<String> getImgFileContextType() {
return imgFileContextType;
}
public void setImgFileContextType(List<String> imgFileContextType) {
this.imgFileContextType = imgFileContextType;
}
public List<String> getImgFileFileName() {
return imgFileFileName;
}
public void setImgFileFileName(List<String> imgFileFileName) {
this.imgFileFileName = imgFileFileName;
}
public String getDir() {
if(dir==null)
return "image";
return dir;
}
public void setDir(String dir) {
this.dir = dir;
}
@Action("uploadjson")
public String execute() {
return super.execute();
}
@Override
protected String myExecute() throws Exception {
//文件保存目录路径
String savePath = ServletActionContext.getServletContext().getRealPath("/") + "\\tmp\\";
//文件保存目录URL
String saveUrl = request.getContextPath() + "/tmp/";
//定义允许上传的文件扩展名
HashMap<String, String> extMap = new HashMap<String, String>();
extMap.put("image", "gif,jpg,jpeg,png,bmp");
extMap.put("flash", "swf,flv");
extMap.put("media", "swf,flv,mp3,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb");
extMap.put("file", "doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2");
//最大文件大小
long maxSize = 1000000;
if(!ServletFileUpload.isMultipartContent(request)){
this.setJsonObject(this.getError("请选择文件。"));
return JSON_UPFILE;
}
//检查目录
File uploadDir = new File(savePath);
if(!uploadDir.isDirectory()){
this.setJsonObject(this.getError("上传目录不存在。"));
return JSON_UPFILE;
}
//检查目录写权限
if(!uploadDir.canWrite()){
this.setJsonObject(this.getError("上传目录没有写权限。"));
return JSON_UPFILE;
}
if(!extMap.containsKey(this.getDir())){
this.setJsonObject(this.getError("目录名不正确。"));
return JSON_UPFILE;
}
//创建文件夹
savePath += this.getDir() + "/";
saveUrl += this.getDir() + "/";
File saveDirFile = new File(savePath);
if (!saveDirFile.exists()) {
saveDirFile.mkdirs();
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String ymd = sdf.format(new Date());
savePath += ymd + "/";
saveUrl += ymd + "/";
File dirFile = new File(savePath);
if (!dirFile.exists()) {
dirFile.mkdirs();
}
Map json = new Hashtable();
for(int i=0;i<imgFile.size();i++) {
String fileName = imgFileFileName.get(i);
long fileSize = imgFile.get(i).length();
if (imgFile.get(i).isFile()) {
//检查文件大小
if(imgFile.get(i).length() > maxSize){
this.setJsonObject(this.getError(("上传文件大小超过限制。")));
return JSON_UPFILE;
}
//检查扩展名
String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
if(!Arrays.<String>asList(extMap.get(this.getDir()).split(",")).contains(fileExt)){
this.setJsonObject(this.getError("上传文件扩展名是不允许的扩展名。\n只允许" + extMap.get(this.getDir()) + "格式。"));
return JSON_UPFILE;
}
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
String newFileName = df.format(new Date()) + "_" + new Random().nextInt(1000) + "." + fileExt;
try{
File uploadedFile = new File(savePath, newFileName);
FileUtils.copyFile(imgFile.get(i), uploadedFile);
}catch(Exception e){
this.setJsonObject(this.getError("上传文件失败。"));
return JSON_UPFILE;
}
json.put("error", 0);
json.put("url",saveUrl + newFileName);
}
}
this.setJsonObject(json);
return JSON_UPFILE;
}
private Map getError(String message) {
Map obj = new Hashtable();
obj.put("error", 1);
obj.put("message", message);
return obj;
}
}
第二个fileManagerJson:
package com.action.upload;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import com.action.JsonBaseAction;
public class FileManagerJsonAction extends JsonBaseAction{
private String dir;
private String path;
private String order;
public String getDir() {
return dir;
}
public void setDir(String dir) {
this.dir = dir;
}
public String getPath() {
if(path!=null)
return path;
return "";
}
public void setPath(String path) {
this.path = path;
}
public String getOrder() {
if(order!=null)
return order.toLowerCase();
return "name";
}
public void setOrder(String order) {
this.order = order;
}
@Action("filemanagerjson")
public String execute() {
return super.execute();
}
@Override
protected String myExecute() throws Exception {
//根目录路径,可以指定绝对路径,比如 /var/www/attached/
String rootPath = ServletActionContext.getServletContext().getRealPath("/") + "\\tmp\\";
//根目录URL,可以指定绝对路径,比如 http://www.yoursite.com/attached/
String rootUrl = request.getContextPath() + "/tmp/";
//图片扩展名
String[] fileTypes = new String[]{"gif", "jpg", "jpeg", "png", "bmp"};
if (dir != null) {
if(!Arrays.<String>asList(new String[]{"image", "flash", "media", "file"}).contains(dir)){
this.setJsonObject("Invalid Directory name.");
return JSON_UPFILE;
}
rootPath += dir + "/";
rootUrl += dir + "/";
File saveDirFile = new File(rootPath);
if (!saveDirFile.exists()) {
saveDirFile.mkdirs();
}
}
//根据path参数,设置各路径和URL
String currentPath = rootPath + this.getPath();
String currentUrl = rootUrl + this.getPath();
String currentDirPath = this.getPath();
String moveupDirPath = "";
if (!"".equals(this.getPath())) {
String str = currentDirPath.substring(0, currentDirPath.length() - 1);
moveupDirPath = str.lastIndexOf("/") >= 0 ? str.substring(0, str.lastIndexOf("/") + 1) : "";
}
//排序形式,name or size or type
//不允许使用..移动到上一级目录
if (this.getPath().indexOf("..") >= 0) {
this.setJsonObject("Access is not allowed.");
return JSON;
}
//最后一个字符不是/
if (!"".equals(this.getPath()) && !this.getPath().endsWith("/")) {
this.setJsonObject("arameter is not valid.");
return JSON_UPFILE;
}
//目录不存在或不是目录
File currentPathFile = new File(currentPath);
if(!currentPathFile.isDirectory()){
this.setJsonObject("Directory does not exist.");
return JSON_UPFILE;
}
//遍历目录取的文件信息
List<Hashtable> fileList = new ArrayList<Hashtable>();
if(currentPathFile.listFiles() != null) {
for (File file : currentPathFile.listFiles()) {
Hashtable<String, Object> hash = new Hashtable<String, Object>();
String fileName = file.getName();
if(file.isDirectory()) {
hash.put("is_dir", true);
hash.put("has_file", (file.listFiles() != null));
hash.put("filesize", 0L);
hash.put("is_photo", false);
hash.put("filetype", "");
} else if(file.isFile()){
String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
hash.put("is_dir", false);
hash.put("has_file", false);
hash.put("filesize", file.length());
hash.put("is_photo", Arrays.<String>asList(fileTypes).contains(fileExt));
hash.put("filetype", fileExt);
}
hash.put("filename", fileName);
hash.put("datetime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(file.lastModified()));
fileList.add(hash);
}
}
if ("size".equals(this.getOrder())) {
Collections.sort(fileList, new SizeComparator());
} else if ("type".equals(this.getOrder())) {
Collections.sort(fileList, new TypeComparator());
} else {
Collections.sort(fileList, new NameComparator());
}
Map result = new Hashtable();
result.put("moveup_dir_path", moveupDirPath);
result.put("current_dir_path", currentDirPath);
result.put("current_url", currentUrl);
result.put("total_count", fileList.size());
result.put("file_list", fileList);
this.setJsonObject(result);
return JSON_UPFILE;
}
}
class SizeComparator implements Comparator {
public int compare(Object a, Object b) {
Hashtable hashA = (Hashtable)a;
Hashtable hashB = (Hashtable)b;
if (((Boolean)hashA.get("is_dir")) && !((Boolean)hashB.get("is_dir"))) {
return -1;
} else if (!((Boolean)hashA.get("is_dir")) && ((Boolean)hashB.get("is_dir"))) {
return 1;
} else {
if (((Long)hashA.get("filesize")) > ((Long)hashB.get("filesize"))) {
return 1;
} else if (((Long)hashA.get("filesize")) < ((Long)hashB.get("filesize"))) {
return -1;
} else {
return 0;
}
}
}
}
class TypeComparator implements Comparator {
public int compare(Object a, Object b) {
Hashtable hashA = (Hashtable)a;
Hashtable hashB = (Hashtable)b;
if (((Boolean)hashA.get("is_dir")) && !((Boolean)hashB.get("is_dir"))) {
return -1;
} else if (!((Boolean)hashA.get("is_dir")) && ((Boolean)hashB.get("is_dir"))) {
return 1;
} else {
return ((String)hashA.get("filetype")).compareTo((String)hashB.get("filetype"));
}
}
}
class NameComparator implements Comparator {
public int compare(Object a, Object b) {
Hashtable hashA = (Hashtable)a;
Hashtable hashB = (Hashtable)b;
if (((Boolean)hashA.get("is_dir")) && !((Boolean)hashB.get("is_dir"))) {
return -1;
} else if (!((Boolean)hashA.get("is_dir")) && ((Boolean)hashB.get("is_dir"))) {
return 1;
} else {
return ((String)hashA.get("filename")).compareTo((String)hashB.get("filename"));
}
}
}
这两个action都是从他例示jsp稍加变动来的。
因为他上传文件的所名字都用的imgFile,所以我这这里直接用List<File> imgFile用于struts2接收文件。我的this.setJsonObject(result)方法在父类中写的。父类就不贴出来了,配置的返回类型是json。
把上面一切就继之后就可以实现上传文件了。上传后也可以查看所有上传的文件。查是在查看的时候遇到一个问题。后台会报错:
虽然能使用但是还是感觉不爽,从上面报错信息大致可以知道,是kindediter提交了一个是时间参数上来,struts2想把它转成int,失败了。于是笔者用监视工具看到,kindediter会把当前时间做为一个参数名(注意不是参数值而是名)提交上来(为了解决浏览器缓存问题)。于是笔者在kindeditor中找到两个js文件提交当前时间
一个是kindeditor-all.js别一个是filemanager.js
param + '&' + new Date().getTime()
于是笔者把以上两个文件中的该段代码改成
param + '&kindeditor_time=' + new Date().getTime()
再看后台果然没有报错信息了。
(注:以上改动kindeditor的JS仅供参考学习)