package com.v512;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.MultiPartEmail;
public class SendAttachmentMailServlet extends HttpServlet {
private static final long serialVersionUID = -1408700020471642036L;
private ServletContext sc;
private String savePath;
private Map<String, String> parameters = new HashMap<String, String>();
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
File file = this.doAttachment(request);
MultiPartEmail email = new MultiPartEmail();
email.setCharset("UTF-8");
email.setHostName("smtp.sina.com");
email.setAuthentication("web08", "web2008");
try {
email.addTo(parameters.get("to"));
email.setFrom(parameters.get("from"));
email.setSubject(parameters.get("subject"));
email.setMsg(parameters.get("content"));
if (file != null) {
EmailAttachment attachment = new EmailAttachment();
attachment.setPath(file.getPath());
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setName(file.getName());
email.attach(attachment);
}
email.send();
request.setAttribute("sendmail.message", "邮件发送成功!");
} catch (EmailException e) {
e.printStackTrace();
request.setAttribute("sendmail.message", "邮件发送不成功!");
}
request.getRequestDispatcher("/sendResult.jsp").forward(request,
response);
}
public File doAttachment(HttpServletRequest request)
throws ServletException, IOException {
File file = null;
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
try {
List items = upload.parseRequest(request);
Iterator it = items.iterator();
while (it.hasNext()) {
FileItem item = (FileItem) it.next();
if (item.isFormField()) {
parameters
.put(item.getFieldName(), item.getString("UTF-8"));
} else {
if (item.getName() != null && !item.getName().equals("")) {
File tempFile = new File(item.getName());
file = new File(sc.getRealPath("/") + savePath,
tempFile.getName());
item.write(file);
}
}
}
} catch (Exception e) {
e.printStackTrace();
request.setAttribute("upload.message", "上传文件不成功!");
}
return file;
}
public void init(ServletConfig config) {
savePath = config.getInitParameter("savePath");
sc = config.getServletContext();
}
}
jsp文件上传源代码
4星 · 超过85%的资源 需积分: 10 27 浏览量
2008-10-28
13:09:10
上传
评论
收藏 16KB RAR 举报
professormissyou
- 粉丝: 2
- 资源: 6
最新资源
- 计算机专业毕业设计,基于Vue + ECharts的数据可视化系统的设计与实现
- Word1_20240424_000136.docx
- 图片网站图片爬虫Python代码
- 同城佳俪
- MATLAB实现LSTM神经网络预测数值(调用MATLAB自带lstm工具箱)
- %E5%B0%8F%E5%B0%8F%E5%A3%B0%E6%98%8E 3.zip
- jdk和idea安装使用教程
- python绘图时渐变的处理-以一个扇形图的渐变为例.html
- 基于STC15F104W的学习型无线遥控解码原理图+程序+说明和433程序+说明
- RIME-BP霜冰优化BP神经网络多特征分类预测(Matlab实现完整源码和数据)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈