jsp 應用kindeditor4.1.7 時的一些配置問題

我的kindeditor版本是4.1.7,是此時的最新版本。

一些參考的內容;http://blog.csdn.net/xnbb9622/article/details/6116037

做自己的別業設計要用到文本編輯器,選擇了kindeditor,感覺這個還是很好的,下面是內容:

首先,要注意目錄的問題:最好保持原先的目錄結構,即比如說新建一個文件夾,直接將需要的文件拷進去。如圖;這是我的目錄

第二:把需要的jar包,都加入到lib目錄下-------3個

第三文本編輯頁面:addblog.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>   
    <title>編輯器測試頁面</title>   
<script charset="utf-8" src="editor/kindeditor.js"></script>
<script charset="utf-8" src="editor/lang/zh_CN.js"></script>
 <script>
KindEditor.ready(function(K) {
var editor1 = K.create('textarea[name="content1"]', {
cssPath : 'editor/plugins/code/prettify.css',
uploadJson :'editor/jsp/upload_json.jsp',
fileManagerJson : 'editor/jsp/file_manager_json.jsp',
allowFileManager : true,
afterCreate : function() {
var self = this;
K.ctrl(document, 13, function() {
self.sync();
K('form[name=example]')[0].submit();
});
K.ctrl(self.edit.doc, 13, function() {
self.sync();
K('form[name=example]')[0].submit();
});
}
});
prettyPrint();
});
</script>
  </head>
  
  <body>
   <form action="" id="upForm" name="upForm" method="post">
<textarea name="content1" style="width:550px;height:350px;visibility:hidden;"></textarea>
    </form>
  
    
  </body>
</html>

第四,要修改plugins下的filemanage下的filemanager.js中的第12行將php改爲jsp.同樣image目錄下的image.js文件16行也進行修改。

第五:就是替換upload_json.jsp文件,是因爲struts2攔截的原因,導致了出錯,這可能是一個bug吧,替換了就可以用了。代碼如下:

upload_json.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.util.*,java.io.*" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="org.apache.commons.fileupload.*" %>
<%@ page import="org.apache.commons.fileupload.disk.*" %>
<%@ page import="org.apache.commons.fileupload.servlet.*" %>
<%@ page import="org.json.simple.*" %>
<%@ page import="org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper" %>


<%
//文件保存目錄路徑    
//D:\Tomcat6.0\webapps\zswz\attached/
String savePath = request.getSession().getServletContext().getRealPath("/") + "attached/";
//文件保存目錄URL /zswz/attached/
String saveUrl = request.getContextPath() + "/attached/";
//定義允許上傳的文件擴展名
//定義允許上傳的文件擴展名
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");


//允許最大上傳文件大小 struts.xml struts.multipart.maxSize=3G
long maxSize = 3000000000l;


response.setContentType("text/html; charset=UTF-8");


if(!ServletFileUpload.isMultipartContent(request)){
        out.println(getError("請選擇文件。"));
        return;
}
//檢查目錄
File uploadDir = new File(savePath);
if(!uploadDir.isDirectory()){
        out.println(getError("上傳目錄不存在。"));
        return;
}
//檢查目錄寫權限
if(!uploadDir.canWrite()){
        out.println(getError("上傳目錄沒有寫權限。"));
        return;
}


String dirName = request.getParameter("dir");//image
if (dirName == null) {
        dirName = "image";
}
if(!extMap.containsKey(dirName)){
        out.println(getError("目錄名不正確。"));
        return;
}
//創建文件夾
savePath += dirName + "/";//D:\Tomcat6.0\webapps\zswz\attached/image/
saveUrl += dirName + "/";///zswz/attached/image/
File saveDirFile = new File(savePath);
if (!saveDirFile.exists()) {
        saveDirFile.mkdirs();
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String ymd = sdf.format(new Date());
savePath += ymd + "/";//D:\Tomcat6.0\webapps\zswz\attached/image/20111129/
saveUrl += ymd + "/";///zswz/attached/image/20111129/
File dirFile = new File(savePath);
if (!dirFile.exists()) {
        dirFile.mkdirs();
}
if (!dirFile.isDirectory()) {
    out.println(getError("上傳目錄不存在 。"));
    return;
}
//檢查目錄寫入權限
if (!dirFile.canWrite()) {
    out.println(getError("上傳目錄沒有寫入權限。"));
    return;
}


//Struts2 請求 包裝過濾器
MultiPartRequestWrapper wrapper = (MultiPartRequestWrapper) request;
//獲得上傳的文件名
String fileName = wrapper.getFileNames("imgFile")[0];//imgFile,imgFile,imgFile
//獲得文件過濾器
File file = wrapper.getFiles("imgFile")[0];


//檢查擴展名
String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
if(!Arrays.<String>asList(extMap.get(dirName).split(",")).contains(fileExt)){
        out.println(getError("上傳文件擴展名是不允許的擴展名。\n只允許" + extMap.get(dirName) + "格式。"));
        return;
}
//檢查文件大小
if (file.length() > maxSize) {
        out.println(getError("上傳文件大小超過限制。"));
        return;





//重構上傳圖片的名稱 
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
String newImgName = df.format(new Date()) + "_"
                + new Random().nextInt(1000) + "." + fileExt;
byte[] buffer = new byte[1024];
//獲取文件輸出流
FileOutputStream fos = new FileOutputStream(savePath +"/" + newImgName);
//獲取內存中當前文件輸入流
InputStream in = new FileInputStream(file);
try {
        int num = 0;
        while ((num = in.read(buffer)) > 0) {
                fos.write(buffer, 0, num);
        }
} catch (Exception e) {
        e.printStackTrace(System.err);
} finally {
        in.close();
        fos.close();
}
//發送給 KE 
JSONObject obj = new JSONObject();
obj.put("error", 0);
obj.put("url", saveUrl +"/" + newImgName);
///zswz/attached/image/20111129/  image 20111129195421_593.jpg
out.println(obj.toJSONString());
%>
<%!
private String getError(String message) {
        JSONObject obj = new JSONObject();
        obj.put("error", 1);
        obj.put("message", message);
        return obj.toJSONString();
}
%>

這樣就配置好了。 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章