自己之前写了一篇关于POI 相关的博客, 想了想在公司中一般常用的不就是上传下载,poi,分页,定时等。好像还有个在线编辑器, 于是自己就花了两个多小时把编辑器相关的代码撸了遍,当然了是先百度找了找资料,看了看实现的逻辑,然后自己撸的。 编辑器自己使用的是WangEditor,网上也有很多关于Editor,kindEitor 的文章, 不过貌似好像没用。业务方面:在编辑器中编辑, 然后保存为word,或者将word中的内容加载进在线编辑器中再次编辑。效果图:

    http://www.wangeditor.com/   这是WangEditor的相关网址,其中api,文档,实例都有。 WangEditor使用,配置还是相对来说比较简单的,引入相关js,创建editor对象,初始化对象。

    

    

  editor.txt.html() 会将在编辑器中编辑的内容获取,然后你直接将其传入后台便可以获取到编辑器中编辑的内容。

  当你使用编辑器编辑并保存后,会在指定的保存位置生成一个word,txt文件夹和一天个htm文件。txt文件夹中是txt文件。txt文件和htm文件都是自动生成的。其中txt文件里是HTML中的标签语言,当你要将word中的内容加载进编辑器再次编辑时,获取的内容是相对应的txt文件中的内容。htm文件只有一个,是刚使用用WangEditor创建word成功后生成的,其就是个HTML文件,其中的标签,属性对应的都是编辑器中展示的模样。当你保存生成word时,是先读取htm中的内容,将${content}替换成你编辑的内容,样式什么的htm文件中模板原先就有。然后利用流将HTML中的内容写入到word中并生成word。

  

  

package com.cn.platform.utils;

import java.io.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class EditorUtils { // 获取项目文件路径
public static String getUploadPath(HttpServletRequest request,String name){
StringBuilder sb = new StringBuilder();
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path;
String uploadPath = sb.append(basePath).append("/ui/CAndTFiles/").append(name).append(".doc").toString();
return uploadPath;
} //获取服务器,本地文件路径
public static String getWindowsPath(HttpServletRequest request,String name){
StringBuilder sb = new StringBuilder();
String windowPath = sb.append("I:/yishangxincheng/ysdFiles/").append(name).append(".doc").toString();
return windowPath;
} //获取服务器,本地文件路径
public static String getWindowsTxtPath(HttpServletRequest request,String name){
StringBuilder sb = new StringBuilder();
String windowPath = sb.append("I:/yishangxincheng/ysdFiles/txt/").append(name).append(".txt").toString();
return windowPath;
} /*public static void saveWord(String editTemplate,String windowPath,HttpServletRequest request,HttpServletResponse response) throws IOException{
EditorUtils.setCode(request, response);
if (editTemplate != null) {
List<String> array = new ArrayList<>();
array.add(editTemplate);
XWPFDocument doc = new XWPFDocument();
XWPFParagraph para = doc.createParagraph();
XWPFRun run = para.createRun();
OutputStream os = new FileOutputStream(windowPath);
for (String s : array) {
//把doc输出到输出流
run.setText(s);
doc.write(os);
}
os.close();
doc.close();
}
}*/ //设置编码
public static void setCode(HttpServletRequest request,HttpServletResponse response) throws IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
} //导出
public static void export(HttpServletRequest request,HttpServletResponse response,String url) throws IOException {
EditorUtils.setCode(request, response);
//获取文件下载路径
String filename = url.substring(url.length()-4, url.length());
if (filename.equals("docx")) {
filename = url.substring(url.length()-6, url.length());
}else{
filename = url.substring(url.length()-5, url.length());
}
File file = new File(url);
if(file.exists()){
//设置相应类型让浏览器知道用什么打开 用application/octet-stream也可以,看是什么浏览器
response.setContentType("application/x-msdownload");
//设置头信息
StringBuilder sb = new StringBuilder();
response.setHeader("Content-Disposition", sb.append("attachment;filename=\"").append(filename).append("\"").toString());
InputStream inputStream = new FileInputStream(file);
ServletOutputStream ouputStream = response.getOutputStream();
byte b[] = new byte[1024];
int n ;
while((n = inputStream.read(b)) != -1){
ouputStream.write(b,0,n);
}
//关闭流
ouputStream.close();
inputStream.close();
}
} // 读取.mht网页中的信息
private static String readFile(String filePath) throws Exception{
StringBuilder sb = new StringBuilder();
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(filePath),"utf-8"));
while (br.ready()) {
sb.append((char) br.read());
}
}catch(Exception e){
e.printStackTrace();
}finally {
if (br!=null) {
br.close();
}
}
return sb.toString();
} //将HTML转word
private static boolean writeWordFile(String content ,String path,String fileName) throws Exception{
boolean flag = false;
FileOutputStream fos = null;
StringBuilder sb = new StringBuilder();
try {
if(!"".equals(path)){
byte[]b = content.getBytes("utf-8");
fos = new FileOutputStream(sb.append(path).append(fileName).append(".doc").toString());
fos.write(b);
fos.close();
flag = true;
}
}catch (IOException e)
{
e.printStackTrace();
}finally {
if (fos !=null) {
fos.close();
}
}
return flag;
} public static void htmlToWord(String editorContent,String htmlPath,HttpServletRequest request,String wordPath,String wordName) throws Exception{
//读取网页中的内容
String htmlFile = EditorUtils.readFile(htmlPath);
// 替换后的内容
String endContent = htmlFile.replace("${content}", editorContent);
//转word
EditorUtils.writeWordFile(endContent, wordPath, wordName);
} // 将editorContent存入txt中用于载入时直接使用
public static void saveEditorContent(String editorContent,String targetPath,String fileName) throws IOException{
FileOutputStream fos = null;
StringBuilder sb = new StringBuilder();
try {
if(!"".equals(targetPath)){
byte[]b = editorContent.getBytes("utf-8");
fos = new FileOutputStream(targetPath);
fos.write(b);
fos.close();
}
}catch (IOException e)
{
e.printStackTrace();
}finally {
if (fos !=null) {
fos.close();
}
} } //载入
public static String load(String name,HttpServletRequest request,HttpServletResponse response) throws IOException{
EditorUtils.setCode(request, response);
String path = EditorUtils.getWindowsTxtPath(request, name);
StringBuilder sb= new StringBuilder();
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(path),"utf-8"));
while (br.ready()) {
sb.append((char) br.read());
}
}catch(Exception e){
e.printStackTrace();
}finally {
if (br!=null) {
br.close();
}
} return sb.toString();
} }

  其中主要的代码就是工具类,代码都是能直接使用的。当然了,代码我还有10%没弄上来,不过我相信有了这些代码,看到此篇博客的人应该没问题。

  在此,希望此篇博客能帮助到一些人。有不足之处,有问题的话可以博客上Q我,看到就会回复

在线编辑器(WangEditor)的更多相关文章

  1. 在ASP.NET Core中使用百度在线编辑器UEditor

    在ASP.NET Core中使用百度在线编辑器UEditor 0x00 起因 最近需要一个在线编辑器,之前听人说过百度的UEditor不错,去官网下了一个.不过服务端只有ASP.NET版的,如果是为了 ...

  2. 在线编辑器的使用-KindEditor

    第一种:KindEditor编辑器 步骤一:加载相应的核心的文件 下载地址:http://kindeditor.net/demo.php <link rel="stylesheet&q ...

  3. js组件在线编辑器插件、图表库插件、文件树插件

    在线编辑器插件: 一.kindeditor 二.UEditor 图表库插件: 一.echart 二.highchart 文件树插件: 一.zTree -- jQuery 树插件 http://www. ...

  4. Method Draw – 很好用的 SVG 在线编辑器

    Method Draw 是一款在线 SVG 编辑器,是 SVG Edit 的一个分支.Method Draw 的目的是改进 SVG Edit 的可用性和用户体验.它移除了 line-caps/corn ...

  5. 百度在线编辑器UEditor(v1.3.6) .net环境下详细配置教程之更改图片和附件上传路径

    本文是接上一篇博客,如果有疑问请先阅读上一篇:百度在线编辑器UEditor(v1.3.6) .net环境下详细配置教程 默认UEditor上传图片的路径是,编辑器包目录里面的net目录下 下面就演示如 ...

  6. 百度在线编辑器UEditor(v1.3.6) .net环境下详细配置教程

    UEditor是百度开发团队奉献的一款很不错的在线编辑器.在百度自己很多产品上都有应用,本文主要是该编辑器的配置教程. 1.下载UEditor,当前最新版本是1.3.6.这里下载的.net版本,选择U ...

  7. 05传智_jbpm与OA项目_部门模块中增加部门的jsp页面增加一个在线编辑器功能

    这篇文章讲的是在线编辑器功能,之前的部门模块中,增加部门的功能jsp页面起先是这么做的.

  8. 将kindeditor在线编辑器制作成smarty插件

    在web开发中,在线编辑器是经常要用到的功能模块,虽说配置在线编辑器没有多大难度,但是每一次编写重复的代码,总是让人感觉不爽. 本篇,就来讲解一下,如何将kindeditor制作成smarty的一个自 ...

  9. 在线编辑器 (UBB, FCK)

    这里主要说明一下:UBB UBB 使用类型HTML的语法.  UBB相对FCK的HTML方式, 安全性高. 用户不可以直接嵌入HTML代码.   UBB 在线编辑器(JS版): http://www. ...

随机推荐

  1. 八使用Shell函数

    在Shell脚本中,将一些需要重复使用的操作,定义为公共的语句块,即可称为函数 使用函数的好处? 使脚本代码更简洁,增强易读性 提高Shell脚本的执行效率 函数定义方法 基本格式1 function ...

  2. nginx命令行及演示:重载、热部署、日志切割

    重载配置文件 nginx -s reload 热部署(升级nginx) 首先备份二进制文件 cp nginx nginx.old  拷贝新版本的nginx替换以前的nginx二进制文件 cp  ngi ...

  3. PySpark Rdd Cheat Sheet Python

  4. 关于Android的hellowrd中出现的r文件错误

    当你的androidAPI 由2.1版本更换成2.2版本时:res/vavlues/styles.xml中使用的android:WindowTitle会报以下异常,error: Error retri ...

  5. [RAC] 1. 安装Oracle RAC时,不能验证ASMSNMP密码问题的解决(ORA-01031或ORA-01017)

      1."ORA-01031: insufficient privileges" [grid@node1 bin]$ orapwd file=/u01/app/11.2.0/gri ...

  6. Go语言实现:【剑指offer】从尾到头打印链表

    该题目来源于牛客网<剑指offer>专题.​ 输入一个链表,按链表从尾到头的顺序返回一个ArrayList. Go语言实现: type ListNode struct { Val int ...

  7. centos7安装bind(DNS服务)

    环境介绍 公网IP:149.129.92.239 内网IP:172.17.56.249 系统:CentOS 7.4 一.安装 yum install bind bind-utils -y 二.修改bi ...

  8. iptables之路由网关共享上网/端口映射

    linux-A 主机配置eth0即可: [root@linux-A ~]# ifconfig eth0|sed -n '2p' inet addr:192.168.20.3 Bcast:192.168 ...

  9. [Windows]远程管理服务WinRM远程管理Windows服务器 Invalid use of command line. Type "winrm -?" for help.

    运行环境 Windows 2012 R2 1. Windows需要打开WinRM服务,Server系统默认打开,默认端口5985 # WinRM服务查看 PS C:\Users\Administrat ...

  10. light oj 1095 - Arrange the Numbers排列组合(错排列)

    1095 - Arrange the Numbers Consider this sequence {1, 2, 3 ... N}, as an initial sequence of first N ...