UE.Editor下载

前台页面: 

  <script type="text/javascript" charset="utf-8" src="/script/ueditor/ueditor.config.js"></script>
    <script type="text/javascript" charset="utf-8" src="/script/ueditor/ueditor.all.min.js"></script>

 <a id="btnDownloadWord" class="mini-button fake-no-status" onclick="downloadWord()">下载Word</a>

  downloadWord: function(e) {
                mini.get('btnDownloadWord').setEnabled(false);
                var data = {content: encodeURIComponent(getUEEditor().getContent()), name: encodeURIComponent(getForm().getData().contractname)};
                var inputs = '<input type="hidden" name="content" value="'+data.content+'"/>';
                inputs += '<input type="hidden" name="name" value="'+data.name+'"/>';        
                jQuery('<form method="post" action="/richeditor/exportWord.do">'+inputs+'</form>').appendTo('body').submit().remove();
                mini.get('btnDownloadWord').setEnabled(true);
            }

后台处理:

public void exportWord() {
        OutputStream os = null;
        ByteArrayInputStream bais = null;
        POIFSFileSystem poifs = null;

        String method = request.getMethod();

        try {
            if (POST.equalsIgnoreCase(method)) {

                String content = URLDecoder.decode(request.getParameter("content"), UTF8);
                String name = URLDecoder.decode(request.getParameter("name"), UTF8);

                byte[] buf = buildContent(content).getBytes();
                bais = new ByteArrayInputStream(buf);
                poifs = new POIFSFileSystem();
                DirectoryEntry directory = poifs.getRoot();
                directory.createDocument("WordDocument", bais);

                response.reset();
                response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(name + ".doc", UTF8));
                response.setContentType("application/msword");

                os = new BufferedOutputStream(response.getOutputStream());
                poifs.writeFilesystem(os);
                os.flush();
            } else if (GET.equalsIgnoreCase(method)) {
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != bais) {
                    bais.close();
                }
                if (null != os) {
                    os.close();
                }
                if (null != poifs) {
                    poifs.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private String buildContent(String content) throws Exception {
        VelocityEngine ve = new VelocityEngine();
        Properties p = new Properties();
        p.put(Velocity.FILE_RESOURCE_LOADER_PATH,this.getClass().getClassLoader().getResource("conf/template").getPath()); //模板路径
        ve.init(p);

        Template template = ve.getTemplate("richeditor.vm", UTF8);  //获取模板
        VelocityContext context = new VelocityContext();
        context.put("content", content);
        StringWriter writer = new StringWriter();
        template.merge(context, writer);

        return writer.toString();
    }

注意:

this.getClass().getResource("/").getPath() 获取classes根路径

在Windows下的路径分隔符和Linux下的路径分隔符是不一样的,当直接使用绝对路径时,跨平台会暴出“No such file or diretory”的异常。

如果要考虑跨平台,则最好是这么写:
File myFile = new File("C:" + File.separator + "tmp" + File.separator, "test.txt");

原文地址:https://www.cnblogs.com/xiaoQ0725/p/8316518.html