由于struts框架对request对象做了封装,原来处理上传图片的upload_json.jsp文件无法使用了,  

于是对kindeditor中处理上传图片的upload_json.jsp文件进行重写,通过多次测试后,成功实现了图片的上传。  
Html代码  

  1. <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>    
  2.     <%@ page import="java.util.*,java.io.*"%>    
  3.     <%@ page import="java.text.SimpleDateFormat"%>    
  4.     <%@ page import="org.apache.commons.fileupload.*"%>    
  5.     <%@ page import="org.apache.commons.fileupload.disk.*"%>    
  6.     <%@ page import="org.apache.commons.fileupload.servlet.*"%>    
  7.     <%@ page import="com.opensymphony.xwork2.ActionContext"%>    
  8.     <%@ page import="org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper"%>    
  9.     <%@ page import="org.json.simple.*"%>    
  10.     
  11.     <%    
  12.         //文件保存目录路径 img_upload是服务器存储上传图片的目录名    
  13.         String savePath = request.getSession().getServletContext().getRealPath("/")+ "img_upload/";    
  14.     
  15.         //文件保存目录URL    
  16.         String saveUrl = request.getContextPath() + "/img_upload/";    
  17.     
  18.         //定义允许上传的文件扩展名    
  19.         String[] fileTypes = new String[] { "gif""jpg""jpeg""png","bmp" };    
  20.     
  21.         //允许最大上传文件大小    
  22.         long maxSize = 1024000;    
  23.     
  24.         //Struts2 请求 包装过滤器    
  25.         MultiPartRequestWrapper wrapper = (MultiPartRequestWrapper) request;    
  26.     
  27.         //获得上传的文件名    
  28.         String fileName = wrapper.getFileNames("imgFile")[0];    
  29.     
  30.         //获得文件过滤器    
  31.         File file = wrapper.getFiles("imgFile")[0];    
  32.     
  33.         //得到上传文件的扩展名    
  34.         String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();    
  35.     
  36.         //检查扩展名    
  37.         if (!Arrays.<String> asList(fileTypes).contains(fileExt)) {    
  38.          out.println(getError("上传文件扩展名是不允许的扩展名。"));    
  39.          return;    
  40.         }    
  41.         //检查文件大小    
  42.         if (file.length() > maxSize) {    
  43.          out.println(getError("上传文件大小超过限制。"));    
  44.          return;    
  45.         }     
  46.     
  47.         //检查目录    
  48.         File uploadDir = new File(savePath);    
  49.         if (!uploadDir.isDirectory()) {    
  50.          out.println(getError("上传目录不存在。"));    
  51.          return;    
  52.         }    
  53.         //检查目录写入权限    
  54.         if (!uploadDir.canWrite()) {    
  55.          out.println(getError("上传目录没有写入权限。"));    
  56.          return;    
  57.         }    
  58.     
  59.         //重构上传图片的名称     
  60.         SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");    
  61.         String newImgName = df.format(new Date()) + "_"new Random().nextInt(1000) + "." + fileExt;    
  62.     
  63.         //设置 KE 中的图片文件地址    
  64.     String newFileName = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()    
  65.         + saveUrl + newImgName;    
  66.     
  67.         byte[] buffer = new byte[1024];    
  68.     
  69.         //获取文件输出流    
  70.         FileOutputStream fos = new FileOutputStream(savePath + newImgName);    
  71.     
  72.         //获取内存中当前文件输入流    
  73.         InputStream in = new FileInputStream(file);    
  74.     
  75.         try {    
  76.                   int num = 0;    
  77.            while ((num = in.read(buffer)) > 0) {    
  78.                  fos.write(buffer, 0, num);    
  79.          }    
  80.         } catch (Exception e) {    
  81.                 e.printStackTrace(System.err);    
  82.         } finally {    
  83.                 in.close();    
  84.                 fos.close();    
  85.         }    
  86.     
  87.         //发送给 KE     
  88.     
  89.         JSONObject obj = new JSONObject();    
  90.         obj.put("error"0);    
  91.         obj.put("url", saveUrl + newImgName);    
  92.         out.println(obj.toJSONString());    
  93.         %>    
  94.         <%!private String getError(String message) {    
  95.          JSONObject obj = new JSONObject();    
  96.          obj.put("error"1);    
  97.          obj.put("message", message);    
  98.          return obj.toJSONString();    
  99.         }    
  100.         %>  

本文转载自 http://www.itjianghu.net/120107/40915815532407445.htm