KindEditor3.4.2实现上传图片

直接上代码

<%@ page pageEncoding="gbk"%>
<%@page
import="java.util.*,java.io.*,
org.apache.commons.fileupload.FileItem,
org.apache.commons.fileupload.FileUploadException,
org.apache.commons.fileupload.disk.DiskFileItemFactory,
org.apache.commons.fileupload.servlet.ServletFileUpload,
java.util.concurrent.locks.*"%>
<%
String id = "";
String url = "";
String imgTitle = "";
String imgWidth = "";
String imgHeight = "";
String imgBorder = "";
String filePath = "";
String align = "";
String newName = null;
// **************************************
// 初始化上传工厂对象
DiskFileItemFactory factory = new DiskFileItemFactory();
// 设置上传工厂对象限制
factory.setSizeThreshold(1024 * 1024 * 20);
factory.setRepository(new File(request.getSession(true)
.getServletContext().getRealPath("/")));
// 创建上传对象
ServletFileUpload upload = new ServletFileUpload(factory);
//upload.setFileSizeMax(1024 * 1024 * 20);
List<FileItem> items = null;
try {
items = upload.parseRequest(request);
} catch (FileUploadException e) {
e.printStackTrace(System.out);
}
for (Iterator<FileItem> i = items.iterator(); i.hasNext();) {
FileItem item = i.next();
if (item.isFormField()) {
String name = item.getFieldName();
String value = item.getString("gbk");
if (name.equals("id")) {
id = value;
} else if (name.equals("imgTitle")) {
imgTitle = value;
} else if (name.equals("imgWidth")) {
imgWidth = value;
} else if (name.equals("imgHeight")) {
imgHeight = value;
} else if (name.equals("imgBorder")) {
imgBorder = value;
} else if (name.equals("align")) {
align = value;
} else if (name.equals("url")) {
filePath = value;
}
} else {
// 取得表单域名
String fieldName = item.getFieldName();
// 取得文件名
String fileName = item.getName();
// 取得文件类型
String contentType = item.getContentType();

final Lock lock = new ReentrantLock();

lock.lock();
try {
//加锁为防止同一时间文件名冲突
newName = System.currentTimeMillis()
+ fileName.substring(fileName.lastIndexOf("."),
fileName.length());
} catch (Exception e) {
e.printStackTrace(System.err);
} finally {
lock.unlock();
}
filePath = ".//notice/editor/updateFile/" + newName;
FileOutputStream fos = new FileOutputStream(request
.getSession().getServletContext().getRealPath("/")
+ "\\notice\\editor\\updateFile\\" + newName);
if (item.isInMemory()) {
fos.write(item.get());
fos.close();
} else {
InputStream in = item.getInputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = in.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
in.close();
fos.close();
}
}
}

String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";

String path1=basePath+"notice/editor/updateFile/" + newName;
out.println("<html><head><title>Insert Image</title><meta http-equiv='content-type' content='text/html; charset=gbk'/></head><body>");
out.println("<script type='text/javascript'>");
out.println("parent.parent.KE.plugin['image'].insert('" + id
+ "','" + path1 + "','" + imgTitle + "','" + imgWidth
+ "','" + imgHeight + "','" + imgBorder + "','" + align
+ "');</script>");
out.println("</body></html>");
%>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值