struts + kindeditor + base64 将编辑的内容保存到本地

三天了,在”神仙”的带领下写出来了,虽然界面很烂,但是可以将就用!希望能给你有帮助。

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="UTF-8"%>
<%@ page import="javax.servlet.http.HttpSession"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
    Integer userType = 0;
%>
        <script src="js/jquery.min.js"></script>
        <script src="js/jquery.easyui.min.js"></script>
        <script src="js/easyui-lang-zh_CN.js"></script>
        <link rel="stylesheet" type="text/css" href="themes/default/easyui.css">
        <link rel="stylesheet" type="text/css" href="themes/icon.css">
            <-- 这里引用了kindeditor的相关样式和js -->
            <link rel="stylesheet" href="js/kindeditor-4.1.10/themes/default/default.css">
            <script charset="utf-8" src="js/kindeditor-4.1.10/kindeditor-min.js"></script>
            <script charset="utf-8" src="js/kindeditor-4.1.10/lang/zh_CN.js"></script>
            <script charset="utf-8" src="js/jbase64.js"></script>
<head>
    <body>
            <!-- form表单 
                文本框输入内容
            -->
            <form action="/fileSaveAction" method="post">
                <input type="submit" value="提交" onclick="save()"/>
                <textarea id="saveId" name="content" style="width:800px;height:200px;border: 0 none;visibility:hidden;" charset="utf-8"></textarea>
            </form>
            <script language="JavaScript" type="text/javascript">
                //创建文本框
                var editor;
                KindEditor.ready(function(K) {
                    editor = K.create('textarea[name="content"]', {          
                            resizeType : 1,
                            allowPreviewEmoticons : false,
                             filterMode : false,
                            items : [   'source', '|', 'undo','redo','|','formatblock','fontname', 'fontsize', '|', 
                                        'forecolor', 'hilitecolor', 'bold', 'italic', 'underline',
                                        'strikethrough','removeformat', '|', 'justifyleft', 'justifycenter', 
                                        'justifyright', 'insertorderedlist','insertunorderedlist',
                                        '|', 'emoticons', 'image', 'link','table','hr','preview'],
                    });
                    //清空KindEditor, 0表示第一个KindEditor编辑器对象
                    KindEditor.instances[0].html("");
                });
                //提交内容到处理的程序中(action)
                function save(){
                    //获取文本框的值
                    editor.sync();
                    var html = document.getElementById('saveId').value;

                    htmln = editor.html="<!DOCTYPE html><html><head><mate charset='utf-8'></head><body>"+html+"</body></html>";

                    var base64 = BASE64.encoder(htmln);
                    alert(base64);
                    document.forms[0].action="fileSaveAction.action?html="+base64;
                    document.forms[0].submit;
                }
            </script>   
    </body>
</html>

struts.xml文件中配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <constant name="struts.i18n.encoding" value="UTF-8" />
    <constant name="struts.convention.default.parent.package" value="convention-default" />
    <constant name="struts.convention.package.locators" value="web" />
    <constant name="struts.convention.package.locators.basePackage"
        value="ct.box.support.action" /> 

        <package name="ciSchemeInfo" namespace="/"  extends="convention-default,json-default">
            <action name="fileSaveAction" class="com.op.action.FileSaveAction" method="saveFile">
                <result name="succ">/succ.jsp</result>  
            </action>
        </package>    
</struts>   

action中的代码

package com.op.action;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;

import sun.misc.BASE64Decoder;

import com.opensymphony.xwork2.ActionSupport;

public class FileSaveAction extends ActionSupport {
    private static final long serialVersionUID = 1L;
    public String saveFile(){
        try {
            HttpServletRequest request = ServletActionContext.getRequest();
            request.setCharacterEncoding("utf-8");
            String htmln = request.getParameter("html").replace(" ",  "+" );
            String htmlnr = new String(htmln.getBytes(),"utf-8");
            System.out.println(htmlnr);
            BASE64Decoder decoder = new BASE64Decoder(); 
            byte[] b = decoder.decodeBuffer(htmlnr); 
            String html = new String(b,"utf-8");
            File file = new File("D:/file.html");
             OutputStream os = new FileOutputStream(file);
             byte bytes[] = html.getBytes();
             os.write(bytes);
             os.flush();
             os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("已保成功存!");
        return "succ";
    }
}

succ.jsp页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <h1 color="red">恭喜你保存成功到D盘名为file.html的文件!</h1>
    <a href="QrActivityInfos.jsp"><input type="button" value="返回"></input></a>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值