多文件上传


replyemail.jsp
<%@ page language="java" pageEncoding="gbk"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<%@ taglib uri="/WEB-INF/app.tld" prefix="app"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
<title>无标题文档</title>

<script language="javascript" type="text/javascript">

var count=2;

function addfujian(){

if(count>6){
window.alert("最多只能上传6个附件");
return;
}
var tb=document.getElementById("tbody");
var newTR=document.createElement("tr");
var td1=document.createElement("td");
td1.setAttribute("align","center");
td1.setAttribute("innerText","附件"+count+":");

var td2=document.createElement("td");
td2.setAttribute("align","left");
var inputFile=document.createElement("input");
inputFile.setAttribute("type","file");
inputFile.setAttribute("name","attachment["+(count-1)+"].file");
inputFile.setAttribute("class","input_button");
//window.alert(inputFile.name);
td2.appendChild(inputFile);

//删除按钮
var delBtn=document.createElement("input");
delBtn.setAttribute("type","button");
delBtn.setAttribute("value","删除");
delBtn.onclick=function(){
tb.removeChild(event.srcElement.parentElement.parentElement);
count--;
}
td2.appendChild(delBtn);

newTR.appendChild(td1);
newTR.appendChild(td2);
tb.appendChild(newTR);
count++;
}

function saveit() {
onlineEditor.save();

var theform=document.forms[0];
if(check()) {
theform.submit();
}
return false;
}

function $(id){
return document.getElementById(id);
}

function check(){
var regEmail=/[\w\.\_]+[@]{1}[\w]+[.]{1}[\w]+/;

var receiveEmail=document.getElementsByName("receiveEmail")[0].value;
var span_receiveEmail=$("span_receiveEmail");
if(!regEmail.test(receiveEmail)){
span_receiveEmail.innerHTML="收件人邮箱格式错误!";
span_receiveEmail.className="STYLE1";
return false;
}
else{
if(!document.getElementsByName("receiveEmail")[0].readOnly){
span_receiveEmail.innerHTML="输入正确!";
span_receiveEmail.className="STYLE2";
}
}

return true;
}

function focuss(){
var span_receiveEmail=$("span_receiveEmail");
span_receiveEmail.innerHTML="请输入收件人邮箱地址!";
span_receiveEmail.className="STYLE2";

}




</script>

<script language="javascript" type="text/javascript"
src="js/validator.js"></script>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
.STYLE1 {color: #FF0000}
.STYLE2 {color: blue}
-->
</style>
</head>

<body>
<div>
<html:messages id="m" name="msg" property="nullreceiveEmail">${m}</html:messages>
<html:messages id="m" name="msg" property="errorreceiveEmail">${m}</html:messages>
<html:messages id="m" name="msg" property="nullcontents">${m}</html:messages>
<html:messages id="m" name="msg" property="contentsOverLength">${m}</html:messages>
</div>
<c:set value="${param.s}" var="ss"></c:set>
<form action="/LhCard/GeRen/mEmailQueue.do?method=sendEmail"
enctype="multipart/form-data" method="post"
onsubmit="return saveit();" name="upload_form">
<table width="100%" border="0" cellpadding="0" cellspacing="1"
id="tb">
<tbody id="tbody">
<tr>
<td colspan="2" class="td_left">
<input type="submit" class="input_button" value="发送" />
<input type="button" class="input_button" value="添加附件"
onclick="addfujian()" />
<span class="STYLE1">最多只能上传6个附件,大小不超过10MB</span>
</td>
</tr>
<tr style="display: none">
<td>
发件人:
</td>
<td>
<input name="sendEmail" type="text" class="text"
onfocus="this.className='bor'" onblur="this.className='text'"
size="31" value="${sessionScope.user.email }" />
</td>
</tr>
<tr>
<td>
收件人:
</td>
<td class="td_left">
<logic:notEqual value="send" name="ss">
<input name="receiveEmail" type="text" class="text"
id="receiveEmail" size="31" value="${requestScope.mEmailQueueForm.receiveEmail}"
readonly="readonly" />
<span class="STYLE1" id="span_receiveEmail"></span>
</logic:notEqual>
<logic:equal value="send" name="ss">
<input name="receiveEmail" type="text" class="text"
id="receiveEmail" onfocus="this.className='bor';focuss();"
size="31" maxlength="40"
onblur="this.className='text';check();"
value="${requestScope.mEmailQueueForm.receiveEmail}" />
<span class="STYLE1" id="span_receiveEmail"></span>
</logic:equal>

</td>
</tr>
<tr>
<td>
主题:
</td>
<td class="td_left">
<input name="title" type="text" class="text"
onfocus="this.className='bor'" onblur="this.className='text'"
size="31" maxlength="40"
value="${requestScope.mEmailQueueForm.title}" />
<span class="STYLE1" id="span_title"></span>
</td>
</tr>
<tr>
<td>
附件:
</td>
<td class="td_left">
<input name="attachment[0].file" type="file" class="text" />
</td>
</tr>

</tbody>
</table>
<textarea name="contents" style="display:none">${requestScope.mEmailQueueForm.contents }</textarea>
<iframe src="editarea/html/htmleditor.html" id="onlineEditor"
width="100%" height="408"></iframe>

<logic:equal value="send" name="ss">
<input type="hidden" name="type" value="00" />
</logic:equal>
<logic:notEqual value="send" name="ss">
<input type="hidden" name="type" value="${ss}" />
</logic:notEqual>

</form>
</body>
</html>



UploadFile.java
package com.lhcard.entity;

import org.apache.struts.upload.FormFile;

public class UploadFile {
private FormFile file;

public FormFile getFile() {
return file;
}

public void setFile(FormFile file) {
this.file = file;
}

}



MEmailQueueForm.java
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.lhcard.struts.form;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

import com.lhcard.entity.UploadFile;

/**
* MyEclipse Struts Creation date: 04-18-2008
*
* XDoclet definition:
*
* @struts.form name="mEmailQueueForm"
*/
public class MEmailQueueForm extends BaseForm {
/*
* Generated Methods
*/

/**
*
*/
private static final long serialVersionUID = 1L;

/**
* Method validate
*
* @param mapping
* @param request
* @return ActionErrors
*/
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
// TODO Auto-generated method stub
return null;
}

/**
* Method reset
*
* @param mapping
* @param request
*/
public void reset(ActionMapping mapping, HttpServletRequest request) {
// TODO Auto-generated method stub
}

private List<UploadFile> myFiles = new ArrayList<UploadFile>();

public MEmailQueueForm() {
// 为了能够在页面初始显示一个file
myFiles.add(new UploadFile());
}

private Integer id;

private String receiveEmail;

private String sendEmail;

private String title;

private String contents;

private String operator;

private Date addDate;

private String sendName;

// 标记是发送(00)还是回复(非00)
private String type;

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public Date getAddDate() {
return addDate;
}

public void setAddDate(Date addDate) {
this.addDate = addDate;
}

public String getOperator() {
return operator;
}

public void setOperator(String operator) {
this.operator = operator;
}

public String getContents() {
return contents;
}

public void setContents(String contents) {
this.contents = contents;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getReceiveEmail() {
return receiveEmail;
}

public void setReceiveEmail(String receiveEmail) {
this.receiveEmail = receiveEmail;
}

public String getSendEmail() {
return sendEmail;
}

public void setSendEmail(String sendEmail) {
this.sendEmail = sendEmail;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getSendName() {
return sendName;
}

public void setSendName(String sendName) {
this.sendName = sendName;
}

public List<UploadFile> getMyFiles() {
return myFiles;
}

public void setMyFiles(List<UploadFile> myFiles) {
this.myFiles = myFiles;
}

// 注意这个方法的定义
public UploadFile getAttachment(int index) {
System.out.println("in the getAttachment method====" + index + ">>>>>");
int size = myFiles.size();
if (index > size) {
for (int i = size - 1; i < index; i++) {
myFiles.add(new UploadFile());
}
} else if (index > size - 1) {
myFiles.add(new UploadFile());
}
return (UploadFile) myFiles.get(index);
}

}



MEmailQueueAction.java

public ActionForward sendEmail(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {

HttpSession session = request.getSession();
Object obj = session.getAttribute("user");
OUsers ousers = (OUsers)session.getAttribute("YunYingUser");
if (obj == null && ousers == null){
return mapping.findForward("globalLogin");
}
MEmailQueueForm frm = (MEmailQueueForm) form;
ActionForward af = this.checkForm(mapping, frm, request);
if (af != null)
return af;

UHoldCard user = (UHoldCard) obj;
if (user == null) {
user = new UHoldCard();
}
if (ousers != null) {
user.setName(ousers.getUserName());
}
String strResult = memailqueueDao.saveWithattachment(frm, user,
this.servlet.getServletContext().getRealPath("/"));
ActionMessages messages = new ActionMessages();
if ("00".equals(strResult)) {
com.lhcard.common.ForwardOperation.putinfo(session, "邮件发送成功",
"/LhCard/GeRen/mEmailQueue.do?method=loginornot", "返回");

return mapping.findForward("success");
} else if ("01".equals(strResult)) {
messages.add("oversize", new ActionMessage(
"geren.replyemail.error.oversize"));
request.setAttribute("msg", messages);
return mapping.findForward("replyemail");
} else if ("02".equals(strResult)) {
com.lhcard.common.ForwardOperation.putinfo(session,
"邮件发送成功,您上传的附件中包含exe文件,为了系统的安全已自动过滤掉",
"/LhCard/GeRen/mEmailQueue.do?method=loginornot", "返回");

return mapping.findForward("success");
} else if ("03".equals(strResult)) {
com.lhcard.common.ForwardOperation.putinfo(session, "邮件发送失败",
"../GeRen/replyemail.jsp", "重新发送邮件");
return mapping.findForward("fail");
}
return null;
}



MEmailQueueDAO.java
/**
* 保存邮件内容及附件
*
* @author 陈伟
* @date 2008-4-18
* @param instance
* @param config
* @param request
* @param response
* @return String
* <p>
* "00":正常执行
* <p>
* "01":大小超过限制
* <p>
* "02":附件中有exe文件(系统自动屏蔽掉)
* <p>
* "03":出现错误
*/
public String saveWithattachment(MEmailQueueForm frm, UHoldCard user,
String context) {
log.debug("saving MEmailQuseue instance");

Session ses = null;
Transaction tran = null;
int hasExe = 0; // 附件中是否有exe文件
try {
ses = this.getSession();
tran = ses.beginTransaction();

MEmailQueue instance = new MEmailQueue();
instance.setSendEmail(frm.getSendEmail());
instance.setReceiveEmail(frm.getReceiveEmail());
if (frm.getTitle() == null || "".equals(frm.getTitle())) {
instance.setTitle("来自" + frm.getSendEmail() + "的邮件");
} else
instance.setTitle(frm.getTitle());
instance.setContents(frm.getContents());
instance.setAddDate(DateTime.switchStringToDate1(DateTime
.getCurrentDate5()));
instance.setOperator(user.getName());
instance.setSendName(user.getName());
// if (frm.getReceiveEmail() != null)
ses.save(instance); // 保存网外邮件队列表

List myFiles = frm.getMyFiles();
java.io.InputStream is = null;
java.io.OutputStream os = null;
StringBuffer filename = new StringBuffer("");

if (myFiles != null && myFiles.size() > 0) {

int sizeCount = 0;
for (int i = 0; i < myFiles.size(); i++) {
UploadFile uploadFile = (UploadFile) myFiles.get(i);
FormFile formfile = uploadFile.getFile();
if (formfile != null && formfile.getFileSize() > 0) {
sizeCount += formfile.getFileSize();
}
}

if (sizeCount > 10000000) { // 大小超过限制10MB
return "01";
}

for (int i = 0; i < myFiles.size(); i++) {
filename = new StringBuffer("");
UploadFile uploadFile = (UploadFile) myFiles.get(i);
FormFile formfile = uploadFile.getFile();
if (formfile != null && formfile.getFileSize() > 0) {
try {
is = formfile.getInputStream();

String[] arr = formfile.getFileName().split("\\.");
String ext = "";
if (arr.length > 1)
ext = arr[1];
if (!"exe".equals(ext)) { // 附件格式后缀不是exe的
filename.append(new MD5().toMD5(formfile
.getFileName()
+ DateTime.getCurrentDate5())
+ "." + ext);
os = new FileOutputStream(context
+ "/GeRen/upload/" + filename);

byte[] bytes = new byte[8192];
while ((is.read(bytes, 0, 8192)) != -1) {
os.write(bytes, 0, 8192);
}

MAttachment attachment = new MAttachment();
attachment.setMHistoryEmail(null);
attachment.setMEmailList(null);
// instance.getMAttachments().add(attachment);
attachment.setMEmailQueue(instance);
attachment.setTitle("");
attachment.setAttachmentUrl("/GeRen/upload/"
+ filename);
System.out
.println(("文件长度====" + "/GeRen/upload/" + filename)
.length());
ses.save(attachment);
} else { // 附件格式后缀是exe的
hasExe++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

if (os != null)
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
if (is != null)
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
tran.commit();
} catch (RuntimeException re) {
re.printStackTrace();
if (tran != null)
tran.rollback();
return "03";
}
if (hasExe != 0)
return "02";
return "00";
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值