在线HTML文档编辑器使用入门之图片上传与图片管理的实现

 在线HTML文档编辑器使用入门之图片上传与图片管理的实现:
官方网址: http://kindeditor.net/demo.php
开发步骤:
1.开发中只需要导入选中的文件(通常在 webapp 下,建立 editor 文件夹 )
导入:lang、plugins、themes、kindeditor.js/kindeditor-min.js-->放在editor文件夹下
2.在页面上引入相关的js&css文件
<!-- 导入Kindeditor相关文件 -->
<script type="text/javascript" src="../../editor/lang/zh_CN.js"></script>
<script type="text/javascript" src="../../editor/kindeditor.js"></script>
<link rel="stylesheet" href="../../editor/themes/default/default.css" />
3.在页面提供标签<textarea id="ta" name="ta"></textarea>
KindEditor.ready(function(K) {
window.editor = K.create("#ta");
4.定制工具栏按钮显示:
KindEditor.ready(function(K) {
window.editor = K.create("#ta", {
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',
'flash', 'media', 'insertfile', 'table', 'hr', 'emoticons', 'baidumap', 'pagebreak',
'anchor', 'link', 'unlink', '|', 'about'
],allowFileManager:true,
uploadJson:"../../image_upload.action",
fileManagerJson:"../../image_manager.action"
});
5.上传图片与图片管理功能实现:
页面提供对应方法:
allowFileManager、uploadJson、fileManagerJson-->发送请求到后台action处理:
//提供属性注入
private File imgFile;
private String imgFileFileName;
private String imgFileContentType;
//图片上传方法
@Action(value = "image_upload", results = { @Result(name = "success", type = "json") })
public String uploadImg() throws IOException {
System.out.println("file:" + imgFile);
System.out.println("文件名称:" + imgFileFileName);
System.out.println("文件类型:" + imgFileContentType);
String savePath = ServletActionContext.getServletContext().getRealPath(
"/upload/");
String saveUrl = ServletActionContext.getRequest().getContextPath()
+ savePath;
// 生成随即图片名称
UUID randomUUID = UUID.randomUUID();
String ext = imgFileFileName
.substring(imgFileFileName.lastIndexOf("."));
String randomUrl = randomUUID + ext;
// 保存图片(绝对的路径和)
FileUtils.copyFile(imgFile, new File(savePath + "/" + randomUrl));
// 返回浏览器数据(文件上传成功了还是失败了)
Map<String, Object> map = new HashMap<String, Object>();
map.put("error", 0);
map.put("url", saveUrl + randomUrl);
ServletActionContext.getContext().getValueStack().push(map);
return SUCCESS;
} //图片管理方法
@Action(value = "image_manager", results = { @Result(name = "success", type = "json") })
public String manager() {
// 根目录路径(绝对路径)
String rootPath = ServletActionContext.getServletContext().getRealPath(
"/")
+ "upload/";
// 根目录URL(绝对路径)
String rootUrl = ServletActionContext.getRequest().getContextPath()
+ "/upload/";
List<Map<String, Object>> fileList = new ArrayList<Map<String, Object>>();
// 当前上传目录
File currentPathFile = new File(rootPath);
// 图片的扩展名
String[] fileTypes = new String[] { "gif", "jpg", "jpeg", "png", "bmp" }; if (currentPathFile.listFiles() != null) {
// 遍历目录取的文件信息
for (File file : currentPathFile.listFiles()) {
Map<String, Object> hash = new HashMap<String, Object>();
String fileName = file.getName();
if (file.isDirectory()) {
hash.put("is_dir", true);
hash.put("has_file", (file.listFiles() != null));
hash.put("filesize", 0L);
hash.put("is_photo", false);
hash.put("filetype", "");
} else if (file.isFile()) {
String fileExt = fileName.substring(
fileName.lastIndexOf(".") + 1).toLowerCase();
hash.put("is_dir", false);
hash.put("has_file", false);
hash.put("filesize", file.length());
hash.put("is_photo", Arrays.<String> asList(fileTypes)
.contains(fileExt));
hash.put("filetype", fileExt);
}
hash.put("filename", fileName);
hash.put("datetime",
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(file
.lastModified()));
fileList.add(hash);
}
}
Map<String, Object> result = new HashMap<String, Object>();
result.put("moveup_dir_path", "");
result.put("current_dir_path", rootPath);
result.put("current_url", rootUrl);
result.put("total_count", fileList.size());
result.put("file_list", fileList);
ActionContext.getContext().getValueStack().push(result);
return SUCCESS;
}
上一篇:MySQL准备


下一篇:[Qt及Qt Quick开发实战精解] 第1章 多文档编辑器