1. 程式人生 > >解決kindeditor上傳圖片、檔案的錯誤

解決kindeditor上傳圖片、檔案的錯誤

支援原創:

kindeditor官網:

把下載下來的檔案加壓縮,根據需要提取相應的檔案。如圖所示

注意引入相應的jar包,commons-fileupload-1.2.2jar、commons-io-2.0.1.jar、json_simple-1.1.jar ,本系統中所使用的jar包比下載下來的jar版本要新,請根據需要選取。

效果如下:

上傳檔案:

之前上傳遇到了如下所示的錯誤:

解決的辦法如下:替換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.apache.struts2.dispatcher.multipart.*" %>
<%@ page import="org.json.simple.*" %>
<%

/**
 * KindEditor JSP
 * 
 * 本JSP程式是演示程式,建議不要直接在實際專案中使用。
 * 如果您確定直接使用本程式,使用之前請仔細確認相關安全設定。
 * 
 */
//檔案儲存目錄路徑
String savePath = pageContext.getServletContext().getRealPath("/") + "upload/";


//檔案儲存目錄URL
String saveUrl  = request.getContextPath() + "/upload/";

//定義允許上傳的副檔名
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;

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");
if (dirName == null) {
	dirName = "image";
}
if(!extMap.containsKey(dirName)){
	out.println(getError("目錄名不正確。"));
	return;
}
//建立資料夾
savePath += dirName + "/";
saveUrl += dirName + "/";
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();
}



MultiPartRequestWrapper wrapper = (MultiPartRequestWrapper) request; 
File file = wrapper.getFiles("imgFile")[0]; 
String fileName = wrapper.getFileNames("imgFile")[0];
//檢查檔案大小
		if(file.length() > maxSize){
			String temStr= "上傳檔案大小超過限制。";
			out.println(getError(temStr));
			return;
		}
//檢查副檔名
		String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
		if(!Arrays.<String>asList(extMap.get(dirName).split(",")).contains(fileExt)){
			String temStr= "上傳副檔名是不允許的副檔名。\n只允許" + extMap.get(dirName) + "格式。";
			out.println(getError(temStr));
			return;
		}
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
		String newFileName = df.format(new Date()) + "_" + new Random().nextInt(1000) + "." + fileExt;
		try {  
            InputStream in = new FileInputStream(file);  
            File uploadFile = new File(savePath, newFileName);  
            OutputStream outFile = new FileOutputStream(uploadFile);  
            byte[] buffer = new byte[1024 * 1024];  
            int length;  
            while ((length = in.read(buffer)) > 0) {  
            	outFile.write(buffer, 0, length);  
            }  
  
            in.close();  
            outFile.close();  
        } catch (FileNotFoundException ex) {  
            ex.printStackTrace();  
        } catch (IOException ex) {  
            ex.printStackTrace();  
        }  
		
		JSONObject obj = new JSONObject();
		obj.put("error", 0);
		obj.put("url", saveUrl + newFileName);
		out.println(obj.toJSONString());
%>
<%!
private String getError(String message) {
	JSONObject obj = new JSONObject();
	obj.put("error", 1);
	obj.put("message", message);
	return obj.toJSONString();
}
%>