自己之前写了一篇关于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我,看到就会回复

最新文章

  1. c#设计模式-组合模式
  2. java是值传递还是引用传递
  3. nginx安装方式
  4. ueditor工具栏更改按钮的默认操作
  5. Oracle常用的性能诊断语句
  6. Makefile-2
  7. C++你不知道的那些事儿—C++语言的15个晦涩特性
  8. day5----正则
  9. AWE、加载计数器错误
  10. 让图片完全显示出来,应对overflow,以及在背景中完全显示出来
  11. php注册登录系统(一)-极简
  12. iOS集成微信支付各种坑收录
  13. cf D. Renting Bikes
  14. Git 和 SVN之间的五个基本区别
  15. java jstack dump 线程 介绍 解释
  16. Python脚本收集腾讯云CDN日志,并入ELK日志分析
  17. Ionic在Android上部署app步骤
  18. [C++]智能指针的实现与使用
  19. 视频云SDK iOS持续集成项目实践
  20. android SQLite数据库的基本操作

热门文章

  1. tomcat的编码设置
  2. BZOJ 1601 [Usaco2008 Oct]灌水 (建图+mst)
  3. python练习——第2题
  4. DNS 查询 - Domain Name Server
  5. Ubuntu 18.04下用户的创建、修改权限及删除用户的方法
  6. java连接Oracle数据库,从ResultSet中提取数据出现java.sql.sqlException结果集已耗尽
  7. asp.net MVC项目开发之统计图的使用(前言)
  8. VS2015中使用qt开发客户端,QPluginLoader加载dll为null的解决办法
  9. 【HDU - 2859 】Phalanx (dp 最大对称子图)
  10. 快速筛出topK的快速选择算法和BFPRT优化