在线编辑器(WangEditor)
  ⾃⼰之前写了⼀篇关于POI 相关的博客,想了想在公司中⼀般常⽤的不就是上传下载,poi,分页,定时等。好像还有个在线编辑器,于是⾃⼰就花了两个多⼩时把编辑器相关的代码撸了遍,当然了是先百度了资料,看了看实现的逻辑,然后⾃⼰撸的。编辑器⾃⼰使⽤的是WangEditor,⽹上也有很多关于Editor,kindEitor 的⽂章,不过貌似好像没⽤。业务⽅⾯:在编辑器中编辑,然后保存为word,或者将word中的内容加载进在线编辑器中再次编辑。效果图:
  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.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 = ContextPath();
String basePath = Scheme()+"://"+ServerName()+":"+ServerPort()+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 = ateParagraph();
XWPFRun run = ateRun();
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);
ists()){
//设置相应类型让浏览器知道⽤什么打开⽤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 = OutputStream();
byte b[] = new byte[1024];
int n ;
网页模板编辑器while((n = ad(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();
}
}
String();
}
//将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 = Bytes("utf-8");
fos = new FileOutputStream(sb.append(path).append(fileName).append(".doc").toString());
fos.write(b);
fos.close();
flag = true;
}
}catch (IOException e)
}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 = adFile(htmlPath);
// 替换后的内容
String endContent = place("${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 = Bytes("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 = WindowsTxtPath(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();
}
}
String();
}
}
  其中主要的代码就是⼯具类,代码都是能直接使⽤的。当然了,代码我还有10%没弄上来,不过我相信有了这些代码,看到此篇博客的⼈应该没问题。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。