利用FCKEditor作为邮件内容编辑器

废话少说,我现在要在FCKEditor工具栏上自定义三个按钮:添加附件,选择邮件模版,发送邮件。
我用的是java版本的editor,将下载的war包在tomcat下解开,有如下目录结构:


一、开发添加附件插件
1,将fckeditor/_samples/_plugins/findreplace拷贝到fckeditor/editor/plugins/下,将findreplace重命名为attachfiles,将里面相应的文件修改,请看修改后的目录:




1)修改en.js内容:FCKLang.AttachFilesDlgTitle = 'Attach Files' ;其余类推
2)为按钮准备一张图片attach.jpg
3)添加attachfiles.jsp文件内容如下:

<%@ page language="java" import="java.util.*"
contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@page import="java.io.File"%>
<%@page import="org.apache.commons.fileupload.FileItem"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
String uploadPath = "D:\\upload\\"; // 用于存放上传文件的目录
String tempPath = "D:\\upload\\tmp\\"; // 用于存放临时文件的目录

request.setCharacterEncoding("UTF-8");

try {
// 文件上传示例程序
// 判断表单是否是 enctype="multipart/form-data"
if (ServletFileUpload.isMultipartContent(request)) {

DiskFileItemFactory dfif = new DiskFileItemFactory();
dfif.setSizeThreshold(5 * 1024 * 1024); // 设定使用内存超过5M时,将产生临时文件并存储于临时目录中。
dfif.setRepository(new File(tempPath)); // 设定存储临时文件的目录。

ServletFileUpload fileupload = new ServletFileUpload(
dfif);
fileupload.setSizeMax(50 * 1024 * 1024); // 设定最大允许上传50M的文件。

List<FileItem> files = (List<FileItem>) fileupload
.parseRequest(request);
System.out.println("filesize:" + files.size());
String attachments = "";
String filenames="";
for (FileItem f : files) {
// if(f.isFormField()){ //如果该项是表单项,不是文件上传项
// out.print(f.getFieldName());
// }

if (!f.isFormField() && f.getSize() > 0) {
String filename = f.getName();
System.out.println("f:" + f.toString());
//filename = filename
// .substring(filename.lastIndexOf("."));
f.write(new File(uploadPath
+ filename.substring(filename
.lastIndexOf("\\") + 1)));
if (attachments.equals("")){
attachments = uploadPath
+ filename.substring(filename
.lastIndexOf("\\") + 1);
filenames=filename.substring(filename
.lastIndexOf("\\") + 1);
}
else{
attachments += ","
+ uploadPath
+ filename.substring(filename
.lastIndexOf("\\") + 1);
filenames+=","+filename.substring(filename
.lastIndexOf("\\") + 1);
}
}
}
String fileString=(String)session.getAttribute("attach");
if(fileString!=null&&fileString.length()>0)
attachments=fileString+","+attachments;
session.setAttribute("attach", attachments);
String filenamesString=(String)session.getAttribute("attachString");
if(filenamesString!=null&& filenamesString.length()>0)
filenames=filenamesString+","+filenames;
session.setAttribute("attachString",filenames);
System.out.println("<script>parent.hiddenAttachDialog(\""+attachments+"\");</script>");
System.out.println("<script>parent.hiddenAttachDialog(\""+filenames+"\");</script>");
//out.print("<script>self.hiddenAttachDialog(\""+filenames+"\");</script>");
//request.getRequestDispatcher("a").forward(request,response);
}

} catch (Exception e) {
// 可以跳转出错页面
e.printStackTrace();
}


if("delete".equals(request.getParameter("flag"))){
String pos=request.getParameter("index");
int index=Integer.parseInt(pos);
String filenames=(String)request.getSession().getAttribute("attach");

String files[] =filenames.split(",");
String returnfilenames="";
filenames="";
if(files.length>0){
for(int i=0;i<files.length;i++){
  if(!"".equals(files[i])){
if(index!=i){
filenames+=files[i]+",";
returnfilenames+=files[i].substring(files[i]
.lastIndexOf("\\") + 1)+",";


}
else{
//delete file on disc
File f=new File(files[i]);
f.delete();
}
}

}

//System.out.println(filenames.substring(0,filenames.length()-1)+":"+returnfilenames.substring(0,returnfilenames.length()-1));
request.getSession().setAttribute("attach", filenames.equals("")?"":filenames.substring(0,filenames.length()-1));
request.getSession().setAttribute("attachString", returnfilenames.equals("")?"":returnfilenames.substring(0,returnfilenames.length()-1));

}else{
request.getSession().setAttribute("attach", null);
request.getSession().setAttribute("attachString", null);

}

response.sendRedirect("attachfiles.jsp");

}

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>


<title>My JSP 'upload.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script language="javascript">

var oEditor = window.parent.InnerDialogLoaded() ;
var FCK = oEditor.FCK ;
var FCKLang = oEditor.FCKLang ;
var FCKInsertLink = oEditor.FCKInsertLink ;



</script> 
</head>

<body scroll="no" style="OVERFLOW: hidden">
<div id="attachments">
<%
String filenames=(String)session.getAttribute("attachString");
if(filenames!=null){
String files[]=filenames.split(",");
for(int i=0;i<files.length;i++){
  if(!files[i].equals("")&&!files[i].equals("null"))
  {
%><li><%=files[i] %>     &nbsp;&nbsp;&nbsp;<a href="attachfiles.jsp?flag=delete&index=<%=i %>">Delete</a></li>
<%}}} %>
</div>
<table border="1"  style="border-color: blue;" width="100%">
<tr>
<td>
<form action="" method="post" enctype="multipart/form-data"
name="fileForm">
<input type="file" name="file1" />
<br />
<input type="file" name="file2" />
<br />
<input type="file" name="file3" />
<br><br />

<input type="submit" value="Upload" />

</form>
</td>
</tr>
</table>

</body>
</html>

4)修改fckplugin.js内容如下
/*
* FCKeditor - The text editor for Internet - http://www.fckeditor.net
* Copyright (C) 2003-2008 Frederico Caldeira Knabben
*
* == BEGIN LICENSE ==
*
* Licensed under the terms of any of the following licenses at your
* choice:
*
*  - GNU General Public License Version 2 or later (the "GPL")
*    http://www.gnu.org/licenses/gpl.html
*
*  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
*    http://www.gnu.org/licenses/lgpl.html
*
*  - Mozilla Public License Version 1.1 or later (the "MPL")
*    http://www.mozilla.org/MPL/MPL-1.1.html
*
* == END LICENSE ==
*
* Plugin to insert "Placeholders" in the editor.
*/

// Register the related command.
FCKCommands.RegisterCommand( 'attachfiles', new FCKDialogCommand('attachfiles',FCKLang.AttachFilesDlgTitle, FCKPlugins.Items['attachfiles'].Path + 'attachfiles.jsp', 400, 400 ) ) ;


// Create the "Plaholder" toolbar button.


var oAttachFileItem = new FCKToolbarButton( 'attachfiles', FCKLang.AttachFilesDlgTitle ) ;
oAttachFileItem.IconPath = FCKPlugins.Items['attachfiles'].Path + 'attach.jpg' ;
FCKToolbarItems.RegisterItem( 'attachfiles', oAttachFileItem ) ;


5)修改fckconfig.js
添加:FCKConfig.Plugins.Add('attachfiles');
修改default菜单:
FCKConfig.ToolbarSets["Default"] = [
['Source','DocProps','-','Save','NewPage','Preview','-','Templates'],
['Cut','Copy','Paste','PasteText','PasteWord','-','Print','SpellCheck'],
['Undo','Redo','-','Find','Replace','-','SelectAll','RemoveFormat'],
['Form','Checkbox','Radio','TextField','Textarea','Select','Button','ImageButton','HiddenField'],
'/',
['Bold','Italic','Underline','StrikeThrough','-','Subscript','Superscript'],
['OrderedList','UnorderedList','-','Outdent','Indent','Blockquote','CreateDiv'],
['JustifyLeft','JustifyCenter','JustifyRight','JustifyFull'],
['Link','Unlink','Anchor'],
['Image','Flash','Table','Rule','Smiley','SpecialChar','PageBreak'],
'/',
['Style','FontFormat','FontName','FontSize'],
['TextColor','BGColor'],
['FitWindow','ShowBlocks','-','About'],
'/',
['InsertTemplate','attachfiles','sendmail'] // No comma for the last row.
] ;

 

二、开发其余两个类似,我将其源码附上

相关文件:MailServlet,MailService,sample02.jsp(此文件覆盖原来的文件即可)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值