基于javaweb+mysql的ssm邮箱邮件收发管理系统(java+ssm+jsp+jq+mysql)
私信源码获取及调试交流
运行环境
Java≥8、MySQL≥5.7、Tomcat≥8
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
基于javaweb的SSM邮箱邮件收发管理系统(java+ssm+jsp+jq+mysql)
项目介绍
本项目为基于SSM的邮件收发管理系统;
用户角色包含以下功能: 用户登录,写信给好友,查看收件箱,查看已发送的邮件,草稿箱查看,通讯录设置,个人资料管理等功能。
环境需要
1.运行环境:最好是java jdk 1.8,我们在这个平台上运行的。其他版本理论上也可以。 2.IDE环境:IDEA,Eclipse,Myeclipse都可以。推荐IDEA; 3.tomcat环境:Tomcat 7.x,8.x,9.x版本均可 4.硬件环境:windows 7/8/10 1G内存以上;或者 Mac OS; 5.数据库:MySql 5.7版本; 6.是否Maven项目:否;
技术栈
- 后端:Spring+SpringMVC+Mybatis 2. 前端:JSP+CSS+JavaScript+jQuery
使用说明
- 使用Navicat或者其它工具,在mysql中创建对应名称的数据库,并导入项目的sql文件; 2. 使用IDEA/Eclipse/MyEclipse导入项目,Eclipse/MyEclipse导入时,若为maven项目请选择maven; 若为maven项目,导入成功后请执行maven clean;maven install命令,然后运行; 3. 将项目中config/db.properties配置文件中的数据库配置改为自己的配置; 4. 运行项目,输入localhost:8080/xx登录
email.setAddressee_id(rs.getString("friend_id"));
email.setTile(rs.getString("friend_name"));
emails.add(email);
}
}
request.setAttribute("friends", emails);
request.getRequestDispatcher("../friend.jsp").forward(request, response);
return "inbox";
}
}
/**
* Servlet implementation class ImgDown
*/
@WebServlet("/downloadFile")
public class FileDown extends HttpServlet {
private static final long serialVersionUID = 1L;
/*
* @see HttpServlet#HttpServlet()
*/
public FileDown() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if(request.getParameter("id")!=null){
}
return "login";
}
@RequestMapping(value = "/login.do")
public String login(String userid, String password, HttpServletRequest request) throws SQLException {
if (userid != null && password != null && !userid.equals("") && !password.equals("")) {
DBConnection dao = new DBConnection();
Connection conn = dao.getConnection();
String sql = "select * from user where userid='" + userid + "'";
PreparedStatement ptmt = conn.prepareStatement(sql);
ResultSet rs = ptmt.executeQuery();
String username = "";
while (rs.next()) {
if (rs.getString("password").equals(password)) {
username = rs.getString("username");
HttpSession session = request.getSession();
session.setAttribute("userid", userid);
session.setAttribute("username", username);
return "main";
} else {
return "login";
}
}
}
return "login";
}
@RequestMapping(value = "/inbox.do")
public String inbox(HttpServletRequest request, HttpServletResponse response) throws SQLException, ServletException, IOException {
HttpSession session = request.getSession();
String userid = session.getAttribute("userid").toString();
List<Email> emails = new ArrayList<>();
if (userid != null && !userid.equals("")) {
DBConnection dao = new DBConnection();
Connection conn = dao.getConnection();
String sql = "select * from email where addresser_id='" + userid + "'";
PreparedStatement ptmt = conn.prepareStatement(sql);
ResultSet rs = ptmt.executeQuery();
while (rs.next()) {
Email email = new Email();
email.setId(rs.getInt("id"));
email.setAddressee_id(rs.getString("addressee_id"));
email.setAddresser_id(rs.getString("addresser_id"));
email.setReaded(rs.getInt("readed"));
email.setTile(rs.getString("title"));
email.setTime(rs.getString("time"));
if (rs.getString("filepath") != null) {
/**
* Servlet implementation class ImgDown
*/
@WebServlet("/downloadFile")
public class FileDown extends HttpServlet {
private static final long serialVersionUID = 1L;
/*
* @see HttpServlet#HttpServlet()
*/
public FileDown() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if(request.getParameter("id")!=null){
System.out.println(request.getParameter("id"));
}else{
System.out.println("!!");
}
int id=Integer.parseInt(request.getParameter("id"));
EmailDao dao=new EmailDao();
Email email=new Email();
try {
email=dao.Findfile(id);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
File file = new File(email.getFilepath());
//如果文件不存在
if(!file.exists()){
request.setCharacterEncoding("utf-8");
}
//处理文件名
// String realname = fileName.substring(fileName.indexOf("_")+1);
*方法2的AttFilePath位webxml里面配置的路径名称,此目录为固定硬盘目录,不会因为项目移除而消失,稳定可靠
*2、String savePath = this.getServletContext().getInitParameter("AttFilePath");
*/
//得到上传文件的保存目录
//String savePath = this.getServletContext().getRealPath("/WEB-INF/upload");
//得到上传文件的保存目录
String tempPath = this.getServletContext().getRealPath("/WEB-INF/temp");
String title = "";
String addresser_id = "";
String email_content = "";
String userid = "";
String savePath = "D:\\email_system\\upload";
String saveFilename = "";
String realSavePath = "";
String time = "";
String filename = "";
String type = request.getParameter("type");
File tmpFile = new File(tempPath);
//如果临时文件不存在,创建临时目录e
if (!tmpFile.exists()) {
tmpFile.mkdir();
}
String message = "";
try {
//创建一个DiskFileItemFactory工厂
DiskFileItemFactory factory = new DiskFileItemFactory();
//设置缓冲区的大小为1000KB,如果不指定,那么缓冲区的大小默认是10KB
factory.setSizeThreshold(1024 * 100);
//设置上传时生成的临时文件的保存目录
factory.setRepository(tmpFile);
//创建一个文件上传解析器
ServletFileUpload upload = new ServletFileUpload(factory);
//监听文件上传进度
upload.setProgressListener(new ProgressListener() {
public void update(long pBytesRead, long pContentLength, int arg2) {
//System.out.println("文件大小为:" + pContentLength + ",当前已处理:" + pBytesRead);
}
});
//设置上传单个文件的大小的最大值,目前是设置为1024*1024字节,也就是1MB
upload.setFileSizeMax(1024 * 1024 * 10);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
/**
* user/list.do
* user/add.do
*
*/
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping(value = "/register.do")
public String register(String userid, String password, String username, HttpServletRequest request) throws SQLException {
String sql = "insert into `user` (`userid`,`password`,`username`) values(?,?,?)";
try {
//设置上传时生成的临时文件的保存目录
factory.setRepository(tmpFile);
//创建一个文件上传解析器
ServletFileUpload upload = new ServletFileUpload(factory);
//监听文件上传进度
upload.setProgressListener(new ProgressListener() {
public void update(long pBytesRead, long pContentLength, int arg2) {
//System.out.println("文件大小为:" + pContentLength + ",当前已处理:" + pBytesRead);
}
});
//设置上传单个文件的大小的最大值,目前是设置为1024*1024字节,也就是1MB
upload.setFileSizeMax(1024 * 1024 * 10);
//设置上传文件总量的最大值,最大值=同时上传的多个文件的大小的最大值的和,目前设置为10MB
upload.setSizeMax(1024 * 1024 * 20);
//4、使用ServletFileUpload解析器解析上传数据,解析结果返回的是一个List<FileItem>集合,每一个FileItem对应一个Form表单的输入项
List<FileItem> list = upload.parseRequest(request);
upload.setHeaderEncoding("UTF-8");
for (FileItem item : list) {
//如果fileitem中封装的是普通输入项的数据
if (item.isFormField()) {
String name = item.getFieldName();
//解决普通输入项的数据的中文乱码问题
String value = item.getString("UTF-8");
//value = new String(value.getBytes("iso8859-1"),"UTF-8");
System.out.println(name + "=" + value);
if (name.equals("userid")) {
userid = value;
}
if (name.equals("title")) {
title = value;
}
if (name.equals("receiver")) {
addresser_id = value;
}
if (name.equals("email_content")) {
email_content = value;
}
} else {
//得到上传的文件名称,
filename = item.getName();
System.out.println(filename);
if (filename == null || filename.trim().equals("")) {
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
/**
* user/list.do
* user/add.do
*
*/
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping(value = "/register.do")
public String register(String userid, String password, String username, HttpServletRequest request) throws SQLException {
String sql = "insert into `user` (`userid`,`password`,`username`) values(?,?,?)";
try {
DBConnection dao = new DBConnection();
Connection c = dao.getConnection();
PreparedStatement ps = c.prepareStatement(sql);
title = value;
}
if (name.equals("receiver")) {
addresser_id = value;
}
if (name.equals("email_content")) {
email_content = value;
}
} else {
//得到上传的文件名称,
filename = item.getName();
System.out.println(filename);
if (filename == null || filename.trim().equals("")) {
}
//解决上传文件名的中文乱码
upload.setHeaderEncoding("UTF-8");
//注意:不同的浏览器提交的文件名是不一样的,有些浏览器提交上来的文件名是带有路径的,如: c:\a\b\1.txt,而有些只是单纯的文件名,如:1.txt
//处理获取到的上传文件的文件名的路径部分,只保留文件名部分
filename = filename.substring(filename.lastIndexOf("\\") + 1);
/**
* 将上传的文件保存到数据库
* time上传时间
* filename文件名
* savePath文件路径
* */
Date date = new Date();
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
time = format.format(date);
//得到上传文件的扩展名
String fileExtName = filename.substring(filename.lastIndexOf(".") + 1);
//如果需要限制上传的文件类型,那么可以通过文件的扩展名来判断上传的文件类型是否合法
System.out.println("上传的文件的扩展名是:" + fileExtName);
//获取item中的上传文件的输入流
InputStream in = item.getInputStream();
//得到文件保存的名称
saveFilename = makeFileName(title + "." + fileExtName);
//得到文件的保存目录
realSavePath = makePath(saveFilename, savePath);
System.out.println(realSavePath + "\\" + saveFilename);
//创建一个文件输出流
FileOutputStream out = new FileOutputStream(realSavePath + "\\" + saveFilename);
//用日期得到文件名的
Calendar date=Calendar.getInstance();
SimpleDateFormat format1=new SimpleDateFormat( "yyyy-MM-dd");
String name=format1.format(date.getTime());
String dir = saveRootPath + "\\" + name;
File file=new File(dir);
//如果目录不存在
if(!file.exists()){
//创建目录
file.mkdirs();
}
return dir;
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
@Controller
@RequestMapping("/email")
public class EmailController {
@RequestMapping(value="/inbox.do")
public String inbox(HttpServletRequest request) throws SQLException{
HttpSession session = request.getSession();
String userid=session.getAttribute("userid").toString();
List<Email> emails=new ArrayList<>();
if(userid!=null&&!userid.equals("")){
DBConnection dao=new DBConnection();
Connection conn = dao.getConnection();
/*
* @see HttpServlet#HttpServlet()
*/
public FileDown() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if(request.getParameter("id")!=null){
System.out.println(request.getParameter("id"));
}else{
System.out.println("!!");
}
int id=Integer.parseInt(request.getParameter("id"));
EmailDao dao=new EmailDao();
Email email=new Email();
try {
email=dao.Findfile(id);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
File file = new File(email.getFilepath());
//如果文件不存在
if(!file.exists()){
request.setCharacterEncoding("utf-8");
}
//处理文件名
// String realname = fileName.substring(fileName.indexOf("_")+1);
String realname=email.getFilename();
//设置响应头,控制浏览器下载该文件
response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(realname, "UTF-8"));
//读取要下载的文件,保存到文件输入流
FileInputStream in = new FileInputStream(email.getFilepath());
//创建输出流
OutputStream out = response.getOutputStream();
//创建缓冲区
byte buffer[] = new byte[1024];
int len = 0;
//循环将输入流中的内容读取到缓冲区当中
while((len=in.read(buffer))>0){
//输出缓冲区的内容到浏览器,实现文件下载
out.write(buffer, 0, len);
}
//关闭文件输入流
in.close();
@WebServlet("/upload")
public class UploadHandleServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public UploadHandleServlet() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//得到上传时生成的临时文件保存目录
/**
* 得到上传文件的保存目录的两种方法
* 方法1的目录生成在tomcat目录下,一旦tomcat清除项目,此文件就会消失,不为考虑
*1、String savePath = this.getServletContext().getRealPath("/WEB-INF/upload");
*方法2的AttFilePath位webxml里面配置的路径名称,此目录为固定硬盘目录,不会因为项目移除而消失,稳定可靠
*2、String savePath = this.getServletContext().getInitParameter("AttFilePath");
*/
//得到上传文件的保存目录
//String savePath = this.getServletContext().getRealPath("/WEB-INF/upload");
//得到上传文件的保存目录
String tempPath = this.getServletContext().getRealPath("/WEB-INF/temp");
String title = "";
String addresser_id = "";
String email_content = "";
String userid = "";
String savePath = "D:\\email_system\\upload";
String saveFilename = "";
String realSavePath = "";
String time = "";
String filename = "";
String type = request.getParameter("type");
File tmpFile = new File(tempPath);
//如果临时文件不存在,创建临时目录e
if (!tmpFile.exists()) {
tmpFile.mkdir();
}
String message = "";
try {
//创建一个DiskFileItemFactory工厂
DiskFileItemFactory factory = new DiskFileItemFactory();
//设置缓冲区的大小为1000KB,如果不指定,那么缓冲区的大小默认是10KB
factory.setSizeThreshold(1024 * 100);
//设置上传时生成的临时文件的保存目录
factory.setRepository(tmpFile);
//创建一个文件上传解析器
* @param filename 要下载的文件名
* @param saveRootPath 上传文件保存的根目录,也就是/WEB-INF/upload目录
* @return 要下载的文件的存储目录
*/
public String findFileSavePathByFileName(String filename,String saveRootPath){
//用日期得到文件名的
Calendar date=Calendar.getInstance();
SimpleDateFormat format1=new SimpleDateFormat( "yyyy-MM-dd");
String name=format1.format(date.getTime());
String dir = saveRootPath + "\\" + name;
File file=new File(dir);
//如果目录不存在
if(!file.exists()){
//创建目录
file.mkdirs();
}
return dir;
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
@Controller
@RequestMapping("/email")
public class EmailController {
@RequestMapping(value="/inbox.do")
public String inbox(HttpServletRequest request) throws SQLException{
HttpSession session = request.getSession();
String userid=session.getAttribute("userid").toString();
List<Email> emails = new ArrayList<>();
if (userid != null && !userid.equals("")) {
DBConnection dao = new DBConnection();
Connection conn = dao.getConnection();
PreparedStatement ptmt = conn.prepareStatement(sql);
ResultSet rs = ptmt.executeQuery();
while (rs.next()) {
Email email = new Email();
email.setAddressee_id(rs.getString("friend_id"));
email.setTile(rs.getString("friend_name"));
emails.add(email);
}
}
request.setAttribute("friends", emails);
request.getRequestDispatcher("../friend.jsp").forward(request, response);
return "inbox";
}
}
/**
/**
* user/list.do
* user/add.do
*
*/
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping(value = "/register.do")
public String register(String userid, String password, String username, HttpServletRequest request) throws SQLException {
String sql = "insert into `user` (`userid`,`password`,`username`) values(?,?,?)";
try {
DBConnection dao = new DBConnection();
Connection c = dao.getConnection();
PreparedStatement ps = c.prepareStatement(sql);
ps.setString(1, userid);
ps.setString(2, password);
ps.setString(3, username);
ps.execute();
ps.close();
c.close();
} catch (Exception e) {
e.printStackTrace();
}
return "login";
}
@RequestMapping(value = "/login.do")
public String login(String userid, String password, HttpServletRequest request) throws SQLException {
if (userid != null && password != null && !userid.equals("") && !password.equals("")) {
DBConnection dao = new DBConnection();
Connection conn = dao.getConnection();
String sql = "select * from user where userid='" + userid + "'";
@RequestMapping(value = "/login.do")
public String login(String userid, String password, HttpServletRequest request) throws SQLException {
if (userid != null && password != null && !userid.equals("") && !password.equals("")) {
DBConnection dao = new DBConnection();
Connection conn = dao.getConnection();
String sql = "select * from user where userid='" + userid + "'";
PreparedStatement ptmt = conn.prepareStatement(sql);
ResultSet rs = ptmt.executeQuery();
String username = "";
while (rs.next()) {
if (rs.getString("password").equals(password)) {
username = rs.getString("username");
HttpSession session = request.getSession();
session.setAttribute("userid", userid);
session.setAttribute("username", username);
return "main";
} else {
return "login";
}
}
}
return "login";
}
@RequestMapping(value = "/inbox.do")
public String inbox(HttpServletRequest request, HttpServletResponse response) throws SQLException, ServletException, IOException {
HttpSession session = request.getSession();
String userid = session.getAttribute("userid").toString();
List<Email> emails = new ArrayList<>();
if (userid != null && !userid.equals("")) {
DBConnection dao = new DBConnection();
Connection conn = dao.getConnection();
String sql = "select * from email where addresser_id='" + userid + "'";
PreparedStatement ptmt = conn.prepareStatement(sql);
ResultSet rs = ptmt.executeQuery();
while (rs.next()) {
Email email = new Email();
email.setId(rs.getInt("id"));
email.setAddressee_id(rs.getString("addressee_id"));
email.setAddresser_id(rs.getString("addresser_id"));
email.setReaded(rs.getInt("readed"));
* @see HttpServlet#HttpServlet()
*/
public FileDown() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if(request.getParameter("id")!=null){
System.out.println(request.getParameter("id"));
}else{
System.out.println("!!");
}
int id=Integer.parseInt(request.getParameter("id"));
EmailDao dao=new EmailDao();
Email email=new Email();
try {
email=dao.Findfile(id);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
File file = new File(email.getFilepath());
//如果文件不存在
if(!file.exists()){
request.setCharacterEncoding("utf-8");
}
//处理文件名
// String realname = fileName.substring(fileName.indexOf("_")+1);
String realname=email.getFilename();
//设置响应头,控制浏览器下载该文件
response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(realname, "UTF-8"));
//读取要下载的文件,保存到文件输入流
FileInputStream in = new FileInputStream(email.getFilepath());
//创建输出流
OutputStream out = response.getOutputStream();
//创建缓冲区
byte buffer[] = new byte[1024];
int len = 0;
//循环将输入流中的内容读取到缓冲区当中
while((len=in.read(buffer))>0){
//输出缓冲区的内容到浏览器,实现文件下载
out.write(buffer, 0, len);
}
//关闭文件输入流
in.close();
//关闭输出流
out.close();
}
/*
* @Method: findFileSavePathByFileName
* @Description: 通过文件名和存储上传文件根目录找出要下载的文件的所在路径
email.setAddresser_id(rs.getString("addresser_id"));
email.setReaded(rs.getInt("readed"));
email.setTile(rs.getString("title"));
email.setTime(rs.getString("time"));
if (rs.getString("filepath") != null) {
email.setFilepath(rs.getString("filepath"));
;
}
emails.add(email);
}
}
request.setAttribute("save_emails", emails);
request.getRequestDispatcher("../save.jsp").forward(request, response);
return "save";
}
@RequestMapping(value = "/sinbox.do")
public String s_inbox(HttpServletRequest request, HttpServletResponse response) throws SQLException, ServletException, IOException {
HttpSession session = request.getSession();
String userid = session.getAttribute("userid").toString();
String title = request.getParameter("stitle");
String addressee_id = request.getParameter("addressee_id");
String readed = request.getParameter("readed");
String sql = "select * from email where addresser_id='" + userid + "'";
if (title != null && !title.trim().equals("")) {
sql = sql + " and title like'%" + title + "%'";
}
if (addressee_id != null && !addressee_id.trim().equals("")) {
sql = sql + " and addressee_id like '%" + addressee_id + "%'";
}
if (readed != null && !readed.trim().equals("")) {
sql = sql + " and readed='" + readed + "'";
}
List<Email> emails = new ArrayList<>();
if (userid != null && !userid.equals("")) {
DBConnection dao = new DBConnection();
Connection conn = dao.getConnection();
PreparedStatement ptmt = conn.prepareStatement(sql);
ResultSet rs = ptmt.executeQuery();
while (rs.next()) {
Email email = new Email();
email.setId(rs.getInt("id"));
email.setAddressee_id(rs.getString("addressee_id"));
email.setAddresser_id(rs.getString("addresser_id"));
email.setReaded(rs.getInt("readed"));
email.setTile(rs.getString("title"));
email.setTime(rs.getString("time"));
//得到上传时生成的临时文件保存目录
/**
* 得到上传文件的保存目录的两种方法
* 方法1的目录生成在tomcat目录下,一旦tomcat清除项目,此文件就会消失,不为考虑
*1、String savePath = this.getServletContext().getRealPath("/WEB-INF/upload");
*方法2的AttFilePath位webxml里面配置的路径名称,此目录为固定硬盘目录,不会因为项目移除而消失,稳定可靠
*2、String savePath = this.getServletContext().getInitParameter("AttFilePath");
*/
//得到上传文件的保存目录
//String savePath = this.getServletContext().getRealPath("/WEB-INF/upload");
//得到上传文件的保存目录
String tempPath = this.getServletContext().getRealPath("/WEB-INF/temp");
String title = "";
String addresser_id = "";
String email_content = "";
String userid = "";
String savePath = "D:\\email_system\\upload";
String saveFilename = "";
String realSavePath = "";
String time = "";
String filename = "";
String type = request.getParameter("type");
File tmpFile = new File(tempPath);
//如果临时文件不存在,创建临时目录e
if (!tmpFile.exists()) {
tmpFile.mkdir();
}
String message = "";
try {
//创建一个DiskFileItemFactory工厂
DiskFileItemFactory factory = new DiskFileItemFactory();
//设置缓冲区的大小为1000KB,如果不指定,那么缓冲区的大小默认是10KB
factory.setSizeThreshold(1024 * 100);
//设置上传时生成的临时文件的保存目录
factory.setRepository(tmpFile);
//创建一个文件上传解析器
ServletFileUpload upload = new ServletFileUpload(factory);
//监听文件上传进度
upload.setProgressListener(new ProgressListener() {
public void update(long pBytesRead, long pContentLength, int arg2) {
//System.out.println("文件大小为:" + pContentLength + ",当前已处理:" + pBytesRead);
}
});