SpringMVC 整合 kindeditor 我是新手 怎么也整不明白了

 我来答
地瓜说机
2017-10-31 · TA获得超过2.9万个赞
知道大有可为答主
回答量:2.3万
采纳率:91%
帮助的人:1.2亿
展开全部
@RequestMapping(value = "/Kindeditor/uploadFile", method = RequestMethod.POST)

public void uploadFile(HttpServletRequest request, HttpServletResponse response) throws Exception {
PrintWriter writer = response.getWriter();
try {
// 文件保存目录路径
String savePath = request.getSession().getServletContext().getRealPath("/") + "upload" + File.separatorChar
+ "products" + File.separatorChar;
String saveUrl = request.getContextPath()+ File.separatorChar + "upload" + File.separatorChar
+ "products" + File.separatorChar;

// 定义允许上传的文件扩展名
HashMap<String, String> extMap = new HashMap<String, String>();
extMap.put("image", "gif,jpg,jpeg,png,bmp");

// 最大文件大小
long maxSize = 1000000;
response.setContentType("text/html; charset=UTF-8");

if (!ServletFileUpload.isMultipartContent(request)) {
writer.println(getError("请选择文件。"));
return;
}

File uploadDir = new File(savePath);
// 判断文件夹是否存在,如果不存在则创建文件夹
if (!uploadDir.exists()) {
uploadDir.mkdirs();
}

// 检查目录写权限
if (!uploadDir.canWrite()) {
writer.println(getError("上传目录没有写权限。"));
return;
}

String dirName = request.getParameter("dir");
if (dirName == null) {
dirName = "image";
}
if (!extMap.containsKey(dirName)) {
writer.println(getError("目录名不正确。"));
return;
}

MultipartHttpServletRequest mRequest = (MultipartHttpServletRequest) request;
Map<String, MultipartFile> fileMap = mRequest.getFileMap();
String fileName = null;
for (Iterator<Map.Entry<String, MultipartFile>> it = fileMap.entrySet().iterator(); it.hasNext();) {
Map.Entry<String, MultipartFile> entry = it.next();
MultipartFile mFile = entry.getValue();
fileName = mFile.getOriginalFilename();
// 检查文件大小
if (mFile.getSize() > maxSize) {
writer.println(getError("上传文件大小超过限制。"));
return;
}
String fileExt = fileName.substring(fileName.lastIndexOf(".")+1);
if (!Arrays.<String> asList(extMap.get(dirName).split(",")).contains(fileExt)) {
writer.println(getError("上传文件扩展名是不允许的扩展名。\n只允许" + extMap.get(dirName) + "格式。"));
return;
}
UUID uuid = UUID.randomUUID();
String path = savePath + uuid.toString() +"."+ fileExt;
saveUrl = saveUrl + uuid.toString() +"."+ fileExt;
BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(path));
FileCopyUtils.copy(mFile.getInputStream(), outputStream);

JSONObject obj = new JSONObject();
obj.put("error", 0);
obj.put("url", saveUrl);
writer.println(obj.toString());

}

} catch (ServiceException serviceException) {
writer.println(getError(ApplicationContextUtil.getMessage(serviceException)));
return;
}
}

private String getError(String message) {
JSONObject obj = new JSONObject();
obj.put("error", 1);
obj.put("message", message);
return obj.toString();
}

[html] view plain copy print?
<link rel="stylesheet" href="<c:url value='/scripts/kindeditor/themes/default/default.css'/>" type="text/css"/>
<link rel="stylesheet" href="<c:url value='/scripts/kindeditor/plugins/code/prettify.css'/>" type="text/css"/>
<script charset="utf-8" src="<c:url value='/scripts/kindeditor/kindeditor-all.js'/>" type="text/javascript"></script>
<script charset="utf-8" src="<c:url value='/scripts/kindeditor/lang/zh-CN.js'/>" type="text/javascript"></script>
<script charset="utf-8" src="<c:url value='/scripts/kindeditor/plugins/code/prettify.js'/>" type="text/javascript"></script>

[html] view plain copy print?
<form:textarea path="esCfgProduct.introduction" id="introduction" cssStyle="width:700px;height:300px;"/>

[javascript] view plain copy print?
KindEditor.ready(function(K) {
var introduction = K.create('#introduction',{
items:[
'source', '|', 'undo', 'redo', '|', 'preview', 'print', 'template', 'code', 'cut', 'copy', 'paste',
'plainpaste', 'wordpaste', '|', 'justifyleft', 'justifycenter', 'justifyright',
'justifyfull', 'insertorderedlist', 'insertunorderedlist', 'indent', 'outdent', 'subscript',
'superscript', 'clearhtml', 'quickformat', 'selectall', '|', 'fullscreen', '/',
'formatblock', 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold',
'italic', 'underline', 'strikethrough', 'lineheight', 'removeformat', '|', 'image', 'multiimage', 'table', 'hr', 'emoticons', 'pagebreak',
'anchor', 'link', 'unlink', '|', 'about'
],
uploadJson : '${ctx}/backoffice/Kindeditor/uploadFile.do',
afterCreate : function() {
var self = this;
K.ctrl(document, 13, function() {
self.sync();
document.forms['inputForm'].submit();
});
K.ctrl(self.edit.doc, 13, function() {
self.sync();
document.forms['inputForm'].submit();
});
}});

<span style="white-space:pre"> introduction.readonly();</span>//只读
});
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式