基于javaweb+mysql的jsp+servlet相册管理系统文件图片管理(java+jsp+servlet+mysql)
私信源码获取及调试交流
运行环境
Java≥8、MySQL≥5.7、Tomcat≥8
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
基于javaweb+mysql的JSP+Servlet相册管理系统文件图片管理(java+jsp+servlet+mysql)
管理员:
admin 123456
用户:
user 123456
request.setAttribute("sex", sex);
}
String parameter = request.getParameter("msg");
if (parameter != null && parameter != "") {
request.setAttribute("msg", parameter);
}
page = userService.page(user, page);
request.setAttribute("page", page);
request.getRequestDispatcher("/views/system/userList.jsp").forward(request, response);
}
//form跳转页面
private void form(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String id = request.getParameter("id");
User user = new User();
if (id != null && id != "") {
user = userService.getById(id);
}
request.setAttribute("user", user);
request.getRequestDispatcher("/views/system/userForm.jsp").forward(request, response);
}
//form跳转页面
private void pay(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String id = request.getParameter("id");
User user = new User();
user = userService.getById(id);
request.setAttribute("user", user);
request.getRequestDispatcher("/views/system/userPay.jsp").forward(request, response);
}
private void savePay(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String id = request.getParameter("id");
User user = new User();
user = userService.getById(id);
String parameter = request.getParameter("money");
Double money = Double.parseDouble(parameter);
user.setFileSize(user.getFileSize() + money % 5 * 10 * 1024);//5元10M空间换算
userService.update(user);
request.setAttribute("user", user);
response.sendRedirect(contextPath + "/user?method=list&msg=100");
}
request.setAttribute("user", user);
request.getRequestDispatcher("/views/system/userPay.jsp").forward(request, response);
}
private void savePay(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String id = request.getParameter("id");
User user = new User();
user = userService.getById(id);
String parameter = request.getParameter("money");
Double money = Double.parseDouble(parameter);
user.setFileSize(user.getFileSize() + money % 5 * 10 * 1024);//5元10M空间换算
userService.update(user);
request.setAttribute("user", user);
response.sendRedirect(contextPath + "/user?method=list&msg=100");
}
}
//设置查询页
String pageNoStr = request.getParameter("pageNo");
if (pageNoStr != null && pageNoStr != "") {
page.setPageNo(Integer.parseInt(pageNoStr));
}
//设置查询条件
String photoAlbumId = request.getParameter("photoAlbumId");
if (photoAlbumId != null && photoAlbumId != "") {
pictureInfo.setPhotoAlbumId(photoAlbumId);
request.setAttribute("photoAlbumId", photoAlbumId);
}
pictureInfo.setPicStatue("0");//
String backTimeLimit = request.getParameter("backTimeLimit");
if (backTimeLimit != null && backTimeLimit != "") {
request.setAttribute("backTimeLimit", backTimeLimit);
}
//判断提示信息
Object msg = request.getSession().getAttribute("msg");
if (msg != null) {
request.setAttribute("msg", msg.toString());
request.getSession().removeAttribute("msg");
}
page.setPageSize(200);
page = pictureInfoService.page(pictureInfo, page);
request.setAttribute("page", page);
request.getRequestDispatcher("/views/system/pictureInfoDetail.jsp").forward(request, response);
}
//相册详情
private void turnBack(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("---相册详情查询---");
PictureInfo pictureInfo = new PictureInfo();
//分页有关
Page<PictureInfo> page = new Page<PictureInfo>();
//设置查询页
String pageNoStr = request.getParameter("pageNo");
if (pageNoStr != null && pageNoStr != "") {
page.setPageNo(Integer.parseInt(pageNoStr));
}
//设置查询条件
String photoAlbumId = request.getParameter("photoAlbumId");
if (photoAlbumId != null && photoAlbumId != "") {
pictureInfo.setPhotoAlbumId(photoAlbumId);
request.setAttribute("photoAlbumId", photoAlbumId);
}
pictureInfo.setPicStatue("1");//
String backTimeLimit = request.getParameter("backTimeLimit");
if (backTimeLimit != null && backTimeLimit != "") {
//获取上传文件的路径
String savePath = PropertiesUtil.getValue("fileUpload");
File file = new File(savePath + "/");
if (!file.exists() && !file.isDirectory()) {
System.out.println(savePath + "目录不存在,需要创建");
//创建目录
file.mkdirs();
}
//消息提示
String msg = "";
try {
//使用Apache文件上传组件处理文件上传步骤:
//1、创建一个DiskFileItemFactory工厂
DiskFileItemFactory factory = new DiskFileItemFactory();
//2、创建一个文件上传解析器
ServletFileUpload upload = new ServletFileUpload(factory);
//解决上传文件名的中文乱码
upload.setHeaderEncoding("UTF-8");
//3、判断提交上来的数据是否是上传表单的数据
if (!ServletFileUpload.isMultipartContent(request)) {
//按照传统方式获取数据
return;
}
//4、使用ServletFileUpload解析器解析上传数据,解析结果返回的是一个List<FileItem>集合,每一个FileItem对应一个Form表单的输入项
List<FileItem> list = upload.parseRequest(request);
for (FileItem item : list) {
//如果fileitem中封装的是普通输入项的数据
String str = "";
String name = item.getFieldName();
if (item.isFormField()) {
str = item.getString("UTF-8");
} else {//如果fileitem中封装的是上传文件
//得到上传的文件名称,
String filename = item.getName();
if (filename == null || filename.trim().equals("")) {
continue;
}
//注意:不同的浏览器提交的文件名是不一样的,有些浏览器提交上来的文件名是带有路径的,如: c:\a\b\1.txt,而有些只是单纯的文件名,如:1.txt
//处理获取到的上传文件的文件名的路径部分,只保留文件名部分
// filename = filename.substring(filename.lastIndexOf("\\")+1);
filename = IdGenUtil.getUUID() + "." + filename.substring(filename.lastIndexOf(".") + 1);
System.out.println(filename);
//获取item中的上传文件的输入流
photoAlbum.setPassword(password);
request.setAttribute("password", password);
}
String downOrRead = request.getParameter("downOrRead");
if (downOrRead != null && downOrRead != "") {
photoAlbum.setDownOrRead(downOrRead);
request.setAttribute("downOrRead", downOrRead);
}
//判断提示信息
Object msg = request.getSession().getAttribute("msg");
if (msg != null) {
request.setAttribute("msg", msg.toString());
request.getSession().removeAttribute("msg");
}
page = photoAlbumService.page(photoAlbum, page);
request.setAttribute("page", page);
request.getRequestDispatcher("/views/system/photoAlbumMine.jsp").forward(request, response);
}
//form跳转页面
private void form(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String id = request.getParameter("id");
PhotoAlbum photoAlbum = new PhotoAlbum();
if (id!=null && id!="") {
photoAlbum = photoAlbumService.getById(id);
}
request.setAttribute("photoAlbum", photoAlbum);
request.getRequestDispatcher("/views/system/photoAlbumForm.jsp").forward(request, response);
}
}
pictureInfoService.update(pictureInfo);
response.sendRedirect(contextPath + "/pictureInfo?method=detail&photoAlbumId=" + pictureInfo.getPhotoAlbumId());
} else {
pictureInfo.setPicStatue("0");
pictureInfo.setUploadTime(DateUtils.getDateTime());
Double fileSize = pictureInfo.getFileSize();//文件大小
User currentUser = UserUtil.currentUser(request);
Double fileSize2 = currentUser.getFileSize();//剩余空间
if (fileSize2 > fileSize) {
pictureInfoService.add(pictureInfo);
fileSize2 = fileSize2 - fileSize;
UserUtil.currentUser(request);
currentUser.setFileSize(fileSize2);
userService.update(currentUser);//更新空间
response.sendRedirect(contextPath + "/pictureInfo?method=detail&photoAlbumId=" + pictureInfo.getPhotoAlbumId());
} else {
msg = "空间不足!";
request.setAttribute("msg", msg);
request.getRequestDispatcher("/views/system/pictureInfoForm.jsp").forward(request, response);
}
}
} catch (Exception e) {
msg = "服务异常!";
e.printStackTrace();
request.setAttribute("msg", msg);
request.getRequestDispatcher("/views/system/pictureInfoForm.jsp").forward(request, response);
}
}
//删除
private void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String id = request.getParameter("id");
PictureInfo pictureInfo = pictureInfoService.getById(id);
pictureInfo.setPicStatue("1");
long currentTime = System.currentTimeMillis() + 7 * 24 * 60 * 60 * 1000;
Date date = new Date(currentTime);
pictureInfo.setBackTimeLimit(date);
pictureInfoService.update(pictureInfo);
//更新用户可用空间
User currentUser = UserUtil.currentUser(request);
currentUser.setFileSize(currentUser.getFileSize() + pictureInfo.getFileSize());
userService.update(currentUser);
response.sendRedirect(contextPath + "/pictureInfo?method=detail&photoAlbumId=" + pictureInfo.getPhotoAlbumId());
}
//删除
private void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String id = request.getParameter("id");
pictureInfoService.delete(id);
response.sendRedirect(contextPath + "/pictureInfo?method=turnBack");
}
System.out.println(savePath + "目录不存在,需要创建");
//创建目录
file.mkdir();
}
//消息提示
String msg = "";
try {
//使用Apache文件上传组件处理文件上传步骤:
//1、创建一个DiskFileItemFactory工厂
DiskFileItemFactory factory = new DiskFileItemFactory();
//2、创建一个文件上传解析器
ServletFileUpload upload = new ServletFileUpload(factory);
//解决上传文件名的中文乱码
upload.setHeaderEncoding("UTF-8");
//3、判断提交上来的数据是否是上传表单的数据
if (!ServletFileUpload.isMultipartContent(request)) {
//按照传统方式获取数据
return;
}
//4、使用ServletFileUpload解析器解析上传数据,解析结果返回的是一个List<FileItem>集合,每一个FileItem对应一个Form表单的输入项
List<FileItem> list = upload.parseRequest(request);
for (FileItem item : list) {
//如果fileitem中封装的是普通输入项的数据
if (item.isFormField()) {
String name = item.getFieldName();
//解决普通输入项的数据的中文乱码问题
if ("id".endsWith(name)) {
if (item.getString("UTF-8") != null && item.getString("UTF-8") != "") {
user.setId(item.getString("UTF-8"));
}
}
if ("username".endsWith(name)) {
user.setUsername(item.getString("UTF-8"));
}
if ("password".endsWith(name)) {
user.setPassword(item.getString("UTF-8"));
}
if ("trueName".endsWith(name)) {
user.setName(item.getString("UTF-8"));
}
if ("sex".endsWith(name)) {
}
//删除
private void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String id = request.getParameter("id");
userService.delete(id);
response.sendRedirect(contextPath + "/user?method=list");
}
//修改
private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String id = request.getParameter("id");
String role = request.getParameter("role");
String username = request.getParameter("username");
String password = request.getParameter("password");
String name = request.getParameter("name");
String sex = request.getParameter("sex");
String phone = request.getParameter("phone");
String content = request.getParameter("content");
String fileSize = request.getParameter("fileSize");
User user = new User();
user.setId(id);
user.setRole(role);
user.setUsername(username);
user.setPassword(password);
user.setName(name);
user.setSex(sex);
user.setPhone(phone);
user.setContent(content);
user.setFileSize(Double.parseDouble(fileSize));
userService.update(user);
request.setAttribute("user", user);
request.setAttribute("msg", "个人信息修改成功!!");
request.getSession().setAttribute("login", user);
request.getRequestDispatcher("/views/system/userInfo.jsp").forward(request, response);
}
//列表查询
private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.err.println("---开始查询---");
User user = new User();
//分页有关
Page<User> page = new Page<User>();
//设置查询页
String pageNoStr = request.getParameter("pageNo");
@WebServlet("/user")
public class UserController extends HttpServlet {
private static final long serialVersionUID = 1L;
private String contextPath = "";
UserDao userDao = new UserDao();
UserService userService = new UserServiceImpl();
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
contextPath = request.getServletContext().getContextPath();
String method = request.getParameter("method");
if ("add".equals(method)) {
add(request, response);
} else if ("delete".equals(method)) {
delete(request, response);
} else if ("list".equals(method)) {
list(request, response);
} else if ("update".equals(method)) {
update(request, response);
}
if ("phone".endsWith(name)) {
user.setPhone(item.getString("UTF-8"));
}
if ("role".endsWith(name)) {
user.setRole(item.getString("UTF-8"));
}
if ("content".endsWith(name)) {
user.setContent(item.getString("UTF-8"));
}
if ("fileSize".endsWith(name)) {
user.setFileSize(Double.parseDouble(item.getString("UTF-8")));
}
} else {//如果fileitem中封装的是上传文件
//得到上传的文件名称,
String filename = item.getName();
System.out.println(filename);
if (filename == null || filename.trim().equals("")) {
continue;
}
//注意:不同的浏览器提交的文件名是不一样的,有些浏览器提交上来的文件名是带有路径的,如: c:\a\b\1.txt,而有些只是单纯的文件名,如:1.txt
//处理获取到的上传文件的文件名的路径部分,只保留文件名部分
filename = filename.substring(filename.lastIndexOf("\\") + 1);
//获取item中的上传文件的输入流
InputStream in = item.getInputStream();
//创建一个文件输出流
FileOutputStream out = new FileOutputStream(savePath + "\\" + filename);
//创建一个缓冲区
byte buffer[] = new byte[1024];
//判断输入流中的数据是否已经读完的标识
int len = 0;
//循环将输入流读入到缓冲区当中,(len=in.read(buffer))>0就表示in里面还有数据
while ((len = in.read(buffer)) > 0) {
//使用FileOutputStream输出流将缓冲区的数据写入到指定的目录(savePath + "\\" + filename)当中
out.write(buffer, 0, len);
}
//关闭输入流
in.close();
//关闭输出流
out.close();
//删除处理文件上传时生成的临时文件
//item.delete();
msg = "文件上传成功!";
photoAlbum.setDownOrRead(downOrRead);
request.setAttribute("downOrRead", downOrRead);
}
//判断提示信息
Object msg = request.getSession().getAttribute("msg");
if (msg != null) {
request.setAttribute("msg", msg.toString());
request.getSession().removeAttribute("msg");
}
page = photoAlbumService.page(photoAlbum, page);
request.setAttribute("page", page);
request.getRequestDispatcher("/views/system/photoAlbumMine.jsp").forward(request, response);
}
//form跳转页面
private void form(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String id = request.getParameter("id");
PhotoAlbum photoAlbum = new PhotoAlbum();
if (id!=null && id!="") {
photoAlbum = photoAlbumService.getById(id);
}
request.setAttribute("photoAlbum", photoAlbum);
request.getRequestDispatcher("/views/system/photoAlbumForm.jsp").forward(request, response);
}
}
/**
*
*
*/
@WebFilter("/*")
public class SerFilter implements Filter {
/**
* Default constructor.
*/
public SerFilter() {
if ("uploadTime".endsWith(name)) {
pictureInfo.setUploadTime(str);
}
if ("fileSize".endsWith(name)) {
}
if ("nameSuffix".endsWith(name)) {
pictureInfo.setNameSuffix(str);
}
if ("picStatue".endsWith(name)) {
pictureInfo.setPicStatue(str);
}
if ("backTimeLimit".endsWith(name)) {
pictureInfo.setBackTimeLimit(Timestamp.valueOf(str));
}
}
if (pictureInfo.getId() != null && !"".equals(pictureInfo.getId())) {
pictureInfoService.update(pictureInfo);
response.sendRedirect(contextPath + "/pictureInfo?method=detail&photoAlbumId=" + pictureInfo.getPhotoAlbumId());
} else {
pictureInfo.setPicStatue("0");
pictureInfo.setUploadTime(DateUtils.getDateTime());
Double fileSize = pictureInfo.getFileSize();//文件大小
User currentUser = UserUtil.currentUser(request);
Double fileSize2 = currentUser.getFileSize();//剩余空间
if (fileSize2 > fileSize) {
pictureInfoService.add(pictureInfo);
fileSize2 = fileSize2 - fileSize;
UserUtil.currentUser(request);
currentUser.setFileSize(fileSize2);
userService.update(currentUser);//更新空间
response.sendRedirect(contextPath + "/pictureInfo?method=detail&photoAlbumId=" + pictureInfo.getPhotoAlbumId());
} else {
msg = "空间不足!";
request.setAttribute("msg", msg);
request.getRequestDispatcher("/views/system/pictureInfoForm.jsp").forward(request, response);
}
}
photoAlbum.setDbUserId(dbUserId);
request.setAttribute("dbUserId", dbUserId);
}
String name = request.getParameter("name");
if (name != null && name != "") {
photoAlbum.setName(name);
request.setAttribute("name", name);
}
String isPublic = request.getParameter("isPublic");
if (isPublic != null && isPublic != "") {
photoAlbum.setIsPublic(isPublic);
request.setAttribute("isPublic", isPublic);
}
String password = request.getParameter("password");
if (password != null && password != "") {
photoAlbum.setPassword(password);
request.setAttribute("password", password);
}
String downOrRead = request.getParameter("downOrRead");
if (downOrRead != null && downOrRead != "") {
photoAlbum.setDownOrRead(downOrRead);
request.setAttribute("downOrRead", downOrRead);
}
//判断提示信息
Object msg = request.getSession().getAttribute("msg");
if (msg != null) {
request.setAttribute("msg", msg.toString());
request.getSession().removeAttribute("msg");
}
page = photoAlbumService.page(photoAlbum, page);
request.setAttribute("page", page);
request.getRequestDispatcher("/views/system/photoAlbumList.jsp").forward(request, response);
}
//列表查询
private void mainindex(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.err.println("---开始查询---");
PhotoAlbum photoAlbum = new PhotoAlbum();
//分页有关
Page<PhotoAlbum> page = new Page<PhotoAlbum>();
//设置查询页
String pageNoStr = request.getParameter("pageNo");
if (pageNoStr != null && pageNoStr != "") {
page.setPageNo(Integer.parseInt(pageNoStr));
}
//设置查询条件
String dbUserId = request.getParameter("dbUserId");
if (dbUserId != null && dbUserId != "") {
photoAlbum.setDbUserId(dbUserId);
request.setAttribute("dbUserId", dbUserId);
}
String name = request.getParameter("name");
}
request.setAttribute("photoAlbum", photoAlbum);
request.getRequestDispatcher("/views/system/photoAlbumForm.jsp").forward(request, response);
}
}
/**
*
*
*/
@WebFilter("/*")
public class SerFilter implements Filter {
/**
* Default constructor.
*/
public SerFilter() {
// TODO Auto-generated constructor stub
}
/**
* @see Filter#destroy()
*/
public void destroy() {
// TODO Auto-generated method stub
}
if (name != null && name != "") {
photoAlbum.setName(name);
request.setAttribute("name", name);
}
String isPublic = request.getParameter("isPublic");
if (isPublic != null && isPublic != "") {
photoAlbum.setIsPublic(isPublic);
request.setAttribute("isPublic", isPublic);
}
String password = request.getParameter("password");
if (password != null && password != "") {
photoAlbum.setPassword(password);
request.setAttribute("password", password);
}
String downOrRead = request.getParameter("downOrRead");
if (downOrRead != null && downOrRead != "") {
photoAlbum.setDownOrRead(downOrRead);
request.setAttribute("downOrRead", downOrRead);
}
//判断提示信息
Object msg = request.getSession().getAttribute("msg");
if (msg != null) {
request.setAttribute("msg", msg.toString());
request.getSession().removeAttribute("msg");
}
page = photoAlbumService.page(photoAlbum, page);
request.setAttribute("page", page);
request.getRequestDispatcher("/views/system/photoAlbumList.jsp").forward(request, response);
}
//列表查询
private void mainindex(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.err.println("---开始查询---");
PhotoAlbum photoAlbum = new PhotoAlbum();
//分页有关
Page<PhotoAlbum> page = new Page<PhotoAlbum>();
//设置查询页
String pageNoStr = request.getParameter("pageNo");
if (pageNoStr != null && pageNoStr != "") {
page.setPageNo(Integer.parseInt(pageNoStr));
}
//设置查询条件
String dbUserId = request.getParameter("dbUserId");
if (dbUserId != null && dbUserId != "") {
photoAlbum.setDbUserId(dbUserId);
request.setAttribute("dbUserId", dbUserId);
page = photoAlbumService.page(photoAlbum, page);
request.setAttribute("page", page);
request.getRequestDispatcher("/views/system/photoAlbumList.jsp").forward(request, response);
}
//列表查询
private void mainindex(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.err.println("---开始查询---");
PhotoAlbum photoAlbum = new PhotoAlbum();
//分页有关
Page<PhotoAlbum> page = new Page<PhotoAlbum>();
//设置查询页
String pageNoStr = request.getParameter("pageNo");
if (pageNoStr != null && pageNoStr != "") {
page.setPageNo(Integer.parseInt(pageNoStr));
}
//设置查询条件
String dbUserId = request.getParameter("dbUserId");
if (dbUserId != null && dbUserId != "") {
photoAlbum.setDbUserId(dbUserId);
request.setAttribute("dbUserId", dbUserId);
}
String name = request.getParameter("name");
if (name != null && name != "") {
photoAlbum.setName(name);
request.setAttribute("name", name);
}
photoAlbum.setIsPublic("1");
String password = request.getParameter("password");
if (password != null && password != "") {
photoAlbum.setPassword(password);
request.setAttribute("password", password);
}
String downOrRead = request.getParameter("downOrRead");
if (downOrRead != null && downOrRead != "") {
photoAlbum.setDownOrRead(downOrRead);
request.setAttribute("downOrRead", downOrRead);
}
//判断提示信息
Object msg = request.getSession().getAttribute("msg");
if (msg != null) {
request.setAttribute("msg", msg.toString());
request.getSession().removeAttribute("msg");
}
page = photoAlbumService.page(photoAlbum, page);
request.setAttribute("page", page);
request.getRequestDispatcher("/views/system/mainindex.jsp").forward(request, response);
}
//设置查询条件
String photoAlbumId = request.getParameter("photoAlbumId");
if (photoAlbumId != null && photoAlbumId != "") {
pictureInfo.setPhotoAlbumId(photoAlbumId);
request.setAttribute("photoAlbumId", photoAlbumId);
}
pictureInfo.setPicStatue("0");//
String backTimeLimit = request.getParameter("backTimeLimit");
if (backTimeLimit != null && backTimeLimit != "") {
request.setAttribute("backTimeLimit", backTimeLimit);
}
//判断提示信息
Object msg = request.getSession().getAttribute("msg");
if (msg != null) {
request.setAttribute("msg", msg.toString());
request.getSession().removeAttribute("msg");
}
page.setPageSize(200);
page = pictureInfoService.page(pictureInfo, page);
request.setAttribute("page", page);
String currentNum = request.getParameter("currentNum");
request.setAttribute("currentNum", Integer.parseInt(currentNum));
request.getRequestDispatcher("/views/system/pictureInfoShow.jsp").forward(request, response);
}
}