关于之前的操作步骤具体可以查看自己上一篇博文:链接地址为:http://blog.csdn.net/aoxiangzhe/article/details/51824539
接下来讲解怎么样进行图片上传:
①修改image.js,在kindeditor-4.1.10\plugins\image下,将默认的php/upload_php改为jsp/upload_json.jsp
②引入jsp/lib下的 commons-fileupload-1.2.1.jar、commons-io-1.4.jar、json_simple-1.1.jar
③找到upload_json.jsp 文件,将以下代码:
*******************************************************************************************************************************
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setHeaderEncoding("UTF-8");
List items = upload.parseRequest(request);
Iterator itr = items.iterator();
while (itr.hasNext()) {
FileItem item = (FileItem) itr.next();
String fileName = item.getName();
long fileSize = item.getSize();
if (!item.isFormField()) {
//检查文件大小
if(item.getSize() > maxSize){
out.println(getError("上传文件大小超过限制。"));
return;
}
//检查扩展名
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;
}
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
String newFileName = df.format(new Date()) + "_" + new Random().nextInt(1000) + "." + fileExt;
try{
File uploadedFile = new File(savePath, newFileName);
item.write(uploadedFile);
}catch(Exception e){
out.println(getError("上传文件失败。"));
return;
}
JSONObject obj = new JSONObject();
obj.put("error", 0);
obj.put("url", saveUrl + newFileName);
out.println(obj.toJSONString());
}
}
*******************************************************************************************************************************替换为如下代码:
替换为如下代码:(头部引入<%@ page import="org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper" %>)
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);
//System.out.println("########" + saveUrl + "@@"+ 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();
}
JSONObject obj = new JSONObject();
obj.put("error", 0);
obj.put("url", saveUrl + newImgName);
out.println(obj.toJSONString());