筆者用的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僅供參考學習)