<span style="font-size:14px;">上传的jsp页面---------------
</span><pre name="code" class="html"><form id="myForm" name="myForm" action="<span style="color:#FF0000;">email_e_Send.action</span>"
method="post" <span style="color:#FF0000;">enctype="multipart/form-data"</span>><pre name="code" class="html"><table width="90%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="right" width="30%">收件人:</td>
<td align="left"><select name="email.email_Receiver.user_Nickname" id="receiveName">
<s:iterator value="#session.userReceive" var="userReceive">
<option value="${userReceive.user_Nickname }">${userReceive.user_Nickname}</option>
</s:iterator>
</select>
</td>
</tr>
<tr>
<td align="right" width="30%">邮件标题:</td>
<td align="left">
<input type="text" name="email.email_Title" οnblur="checkEmail_Title()" id="email_Title" />
<font color="red" style="font-size: 12px" id="showemail_Title">*</font>
</td>
</tr>
<tr>
<td align="right" width="30%">邮件内容:</td>
<td align="left">
<textarea rows="5" cols="40" name="email.email_Content" οnblur="checkEmail_Content()" id="Email_Content"></textarea>
<font color="red" style="font-size: 12px" id="showemail_Content">*</font>
</td>
</tr>
<span style="color:#FF0000;"><tr>
<td align="right" width="30%">上传附件:</td>
<td align="left"><input type="file" name="<span style="color:#000099;">upload</span>" /></td>
</tr></span>
<tr>
<td id="td" colspan="2"><br />
<input type="submit" id="subEmail" value="发送邮件" />
</td>
</tr>
</table>
带有下载页面的jsp-------------------->
<table width="90%" border="0" cellspacing="0" cellpadding="0" id="tab_">
<tr>
<td align="right" width="30%">邮件标题:</td>
<td align="left"><input type="text" name="e_selfNews.email_Title" value="${e_selfNews.email_Title }" disabled="disabled" id=email_Title />
</td>
</tr>
<tr>
<td align="right" width="30%">邮件内容:</td>
<td align="left"><textarea rows="5" cols="50" name="e_selfNews.email_Content" disabled="disabled" id="content">${e_selfNews.email_Content }</textarea>
</td>
</tr>
<tr>
<td align="right" width="30%">发信时间:</td>
<td align="left"><fmt:formatDate value="${e_selfNews.email_Time }"pattern="yyyy-MM-dd" /></td>
</tr>
<tr>
<td align="right" width="30%">来自:</td>
<td align="left">
<input type="text" name="e_selfNews.email_Sender.user_Nickname" value="${e_selfNews.email_Sender.user_Nickname }" disabled="disabled" id="user_Nickname" />
</td>
</tr>
<span style="color:#FF0000;"> <tr>
<td align="right" width="30%">文件:</td>
<c:choose>
<c:when test="${e_selfNews.email_Url==null }">
<td align="left">无附件</td>
</c:when>
<c:otherwise>
<td align="left"><a href="email_e_download.action?email.email_Url=${e_selfNews.email_Url }">下载</a></td>
</c:otherwise>
</c:choose>
</tr></span>
</table>
先写一个util包,包中含有一个UpLoadFile.java类功能是完成上传操作,供之后要跳转的Action使用
<pre name="code" class="html">UpLoadFile ----------------------------
<span style="color:#FF0000;">package com.swe.oa.util;
import java.io.File;
import java.util.UUID;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
/**
*
* 项目名称 OA
* 作者 tim
* 创建时间 2014-7-29
* 类描述 文件的上传
*/</span><span style="color:#FF0000;">
public class UpLoadFile {
public <span style="color:#996633;">static</span> String upload(File image, String imageFileName, String imageContentType){
String realPath = ServletActionContext.getServletContext().getRealPath("/upload");// 相对于站点的目录,images
//如果没有上传文件,不执行操作
if (image != null) {
imageFileName = setImageFileName(imageFileName);
File saveFile = new File(new File(realPath), imageFileName);// file(directory,name);
if (!saveFile.getParentFile().exists()) {
saveFile.getParentFile().mkdirs();
}
try {
FileUtils.copyFile(image, saveFile);
return imageFileName;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
return null;
}
//生成文件名,防止文件名称相同产生冲突
private static String setImageFileName(String imageFileName) {
return UUID.randomUUID().toString()+imageFileName.substring(imageFileName.indexOf("."),imageFileName.length());
}
}
</span>
提交给Action后
Action代码--------------------
package com.swe.oa.action;
import java.io.File;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.swe.oa.model.Email;
import com.swe.oa.model.User;
import com.swe.oa.service.EmailService;
import com.swe.oa.service.UserService;
import com.swe.oa.util.UpLoadFile;
@Controller("emailAction")
public class EmailAction extends ActionSupport implements ModelDriven<Email> {
/**
*
*/
private static final long serialVersionUID = 1L;
private Email email;
//文件上传//
<span style="color:#FF0000;">private File upload;
private String uploadContentType;
private String uploadFileName;</span>
/get()/set()/
<span style="color:#FF0000;">
<span style="color:#000000;">
public Email getEmail() {
return email;
}
public void setEmail(Email email) {
this.email = email;
}
</span>
</span>
public Email getModel() {
return email;
}
上传附件使用的get()set()方法/
<span style="color:#FF0000;">public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
public String getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}</span>
//方法///
@Resource
private EmailService emailService;
@Resource
private UserService userService;
/*
* 写邮件点击发送时触发的Action函数
* */
public String<span style="color:#FF0000;"> e_Send()</span> throws Exception{
//上传附件
<span style="color:#FF0000;">String url = UpLoadFile.upload(upload, uploadFileName, uploadContentType);</span>//上传成功
//获取当前系统时间
Date time = new Date();
//得到当前登录用户作为发邮件的人
User loginU = (User) ActionContext.getContext().getSession()
.get("loginUser");
//获取接收人
String hql = "from Users u where u.user_Nickname = ?";
String[] params = { email.getEmail_Receiver().getUser_Nickname() };
User u_receive = userService.find(hql, params).get(0);
//填充各个字段
<span style="color:#FF0000;">email.setEmail_Url(url);</span>
email.setEmail_Time(time);
email.setEmail_Sender(loginU);
email.setEmail_Receiver(u_receive);
email.setEmail_ReadStatus(0);
email.setEmail_Status(0);
emailService.save(email);
return "e_Send";
}
/*
* 点击下载按钮触发该Action函数并且下载相应附件
* */
<span style="color:#FF0000;">public String e_download(){
String realPath = ServletActionContext.getServletContext().getRealPath(
"/upload");
File saveFile = new File(new File(realPath), email.getEmail_Url());
System.out.println("xiazai");
return "e_download";
}
/**
* 下载附件
*/
public InputStream getMailUrl() {
System.out.println("liu");
return ServletActionContext.getServletContext().getResourceAsStream(
"upload/" + email.getEmail_Url());
}
</span>
}
其间注意Struts.xml 的配置
<pre name="code" class="html"><pre name="code" class="html"><package name="email" namespace="/" extends="struts-default">
<action name="email_*" class="com.swe.oa.action.EmailAction" method="{1}">
<!-- 下载附件时的配置 -->
<span style="color:#FF0000;"> <result name="e_download" type="stream">
<param name="contentType">text/plain</param>
<param name="contentDisposition">attachment;fileName="${email.email_Url}"</param>
<span style="font-size:14px;"></span></span><pre name="code" class="html"><pre name="code" class="html"><pre name="code" class="html"><span style="color:#FF0000;"> </span>
<param name="inputName"><span style="color:#000099;">mailUrl</span></param> <span style="color:#FF0000;">
<param name="bufferSize">9216</param>
</result></span>
<span style="color:#000000;"></action> </span>
<span style="color:#000000;"></package></span>
说明:
<pre name="code" class="html"><pre name="code" class="html"><pre name="code" class="html"><pre name="code" class="html"><pre name="code" class="html">(1) name="e_download"是点击下载按钮触发的Action相关方法<span style="color:#FF0000;"> e_download()返回的值,</span> <pre name="code" class="html"><pre name="code" class="html"><pre name="code" class="html"><pre name="code" class="html"><pre name="code" class="html"><pre name="code" class="html">此处type必须为stream.
(2)<span style="color:#FF0000;"><param name="contentType">text/plain</param></span>固定
(3)<span style="color:#FF0000;"><param name="contentDisposition">attachment;fileName="${email.email_Url}"</param> </span>${是你表中的那个字段,即对应
实体类的属性,也就是上传的文件所生成的名字,此处即为了email_Url}
(4)<param name="inputName">
mailUrl</param> 当对应的下载Action中的方法
e_download()执行完毕后返回
e_download,
通过此处的配置会去Action中找一个叫get MailUrl()的方法执行
(5) <param name="bufferSize">9216</param> 设置大小。