本系统是一个简单的相册管理系统,可以在线管理本地相册,实现图片的预览和管理。
(文末查看完整源码)
登录
添加图片

添加分类

首页

图片详情


本系统实现了以下功能:
1、登录
2、管理首页
3、添加图片
4、分类管理
5、修改密码
6、图片详情
7、退出登录
等
数据库:mysql
开发工具:Idea(Myeclispe、Eclipse也可以)
知识点:javaweb(servlet+jsp)
项目结构

java端
实体类
Photo.java
package com.fsq.beans;
public class Photo {
private int id = 0;
private String name;
private String path = "";
private int dianji ;
private String contentTime = "";
private String shuoming;
private int lid;
private int count;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public int getDianji() {
return dianji;
}
public void setDianji(int dianji) {
this.dianji = dianji;
}
public String getContentTime() {
return contentTime;
}
public void setContentTime(String contentTime) {
this.contentTime = contentTime;
}
public String getShuoming() {
return shuoming;
}
public void setShuoming(String shuoming) {
this.shuoming = shuoming;
}
public int getLid() {
return lid;
}
public void setLid(int lid) {
this.lid = lid;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
Dao层
PhotoDAO.java
package com.fsq.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.fsq.beans.Photo;
import com.fsq.util.JdbcUtil;
public class PhotoDAO {
public List<Photo> getAllPhotos() throws SQLException {
List<Photo> photoList = new ArrayList<Photo>();
String sql = "select * from photo";
Connection conn = JdbcUtil.getConnection();
Statement stmt = null;
ResultSet rs = null;
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while (rs.next()) {
Photo photo = new Photo();
photo.setId(rs.getInt("id"));
photo.setName(rs.getString("name"));
photo.setPath(rs.getString("path"));
photo.setDianji(rs.getInt("dianji"));
photo.setContentTime(rs.getString("contenttime"));
photo.setShuoming(rs.getString("shuoming"));
photo.setLid(rs.getInt("lid"));
photoList.add(photo);
}
} catch (SQLException e) {
e.printStackTrace();
throw e;
} finally {
JdbcUtil.close(rs, null);
JdbcUtil.close();
}
return photoList;
}
public void delete(Photo photo) throws SQLException {
Connection conn = JdbcUtil.getConnection();
PreparedStatement ps = null;
String sql = "delete from photo where id=?";
try {
ps = conn.prepareStatement(sql);
ps.setInt(1, photo.getId());
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
throw e;
} finally {
JdbcUtil.close(null, ps);
JdbcUtil.close();
}
}
public boolean updateDianJi(Photo photo) throws SQLException {
boolean flag = false;
PreparedStatement ps = null;
String sql = "UPDATE photo SET dianji=dianji+1 where id=?";
try {
Connection conn = JdbcUtil.getConnection();
ps = conn.prepareStatement(sql);
ps.setInt(1, photo.getId());
int n = ps.executeUpdate();
if (n != 0) {
flag = true;
} else {
flag = false;
}
} catch (SQLException e) {
e.printStackTrace();
throw e;
} finally {
JdbcUtil.close(null, ps);
JdbcUtil.close();
}
return flag;
}
public void update(Photo photo) throws SQLException {
Connection conn = JdbcUtil.getConnection();
PreparedStatement ps = null;
String sql = "update photo set name=?,shuoming=?,lid=? where id=?";
try {
ps = conn.prepareStatement(sql);
ps.setString(1, photo.getName());
ps.setString(2, photo.getShuoming());
ps.setInt(3, photo.getLid());
ps.setInt(4, photo.getId());
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
throw e;
} finally {
JdbcUtil.close(null, ps);
JdbcUtil.close();
}
}
public List<Photo> getAllPhotosByClassId(int id) throws SQLException {
List<Photo> photoList = new ArrayList<Photo>();
String sql = "select id,name,path,dianji,shuoming from photo where lid=?";
PreparedStatement ps = null;
ResultSet rs = null;
try {
Connection conn = JdbcUtil.getConnection();
ps = conn.prepareStatement(sql);
ps.setInt(1, id);
rs = ps.executeQuery();
while (rs.next()) {
Photo photo = new Photo();
photo.setId(rs.getInt(1));
photo.setName(rs.getString(2));
photo.setPath(rs.getString(3));
photo.setDianji(rs.getInt(4));
photo.setShuoming(rs.getString(5));
photoList.add(photo);
}
} catch (SQLException e) {
e.printStackTrace();
throw e;
} finally {
JdbcUtil.close(rs, ps);
JdbcUtil.close();
}
return photoList;
}
public Photo getPhotoById(int id) throws SQLException {
String sql = "select id,name,path,contentTime,shuoming,lid from photo where id=?";
PreparedStatement ps = null;
ResultSet rs = null;
try {
Connection conn = JdbcUtil.getConnection();
ps = conn.prepareStatement(sql);
ps.setInt(1, id);
rs = ps.executeQuery();
while (rs.next()) {
Photo photo = new Photo();
photo.setId(rs.getInt(1));
photo.setName(rs.getString(2));
photo.setPath(rs.getString(3));
photo.setContentTime(rs.getString(4));
photo.setShuoming(rs.getString(5));
photo.setLid(rs.getInt(6));
return photo;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
JdbcUtil.close(rs, ps);
JdbcUtil.close();
}
return null;
}
public boolean insert(Photo photo) throws SQLException {
Connection conn = null;
PreparedStatement ps = null;
try {
String sql = "insert into photo (name,path,shuoming,contentTime,lid,dianji) values(?,?,?,?,?,0)";
conn = JdbcUtil.getConnection();
ps = conn.prepareStatement(sql);
ps.setString(1, photo.getName());
ps.setString(2, photo.getPath());
ps.setString(3, photo.getShuoming());
ps.setString(4, photo.getContentTime());
ps.setInt(5, photo.getLid());
int num = ps.executeUpdate();
if (num != 0) {
return true;
}
return false;
} catch (SQLException e) {
e.printStackTrace();
throw e;
} finally {
JdbcUtil.close(null, ps);
JdbcUtil.close();
}
}
}
servlet类
PhotoServlet.java
package com.fsq.servlet;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import com.fsq.beans.Photo;
import com.fsq.beans.PhotoClass;
import com.fsq.beans.PingLun;
import com.fsq.biz.PhotoBiz;
import com.fsq.dao.PhotoClassDAO;
import com.fsq.dao.PhotoDAO;
import com.fsq.dao.PingLunDAO;
import com.fsq.util.ImageUtils;
public class PhotoServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public PhotoServlet() {
super();
}
public void destroy() {
super.destroy();
}
@SuppressWarnings("unused")
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
String toUrl = "";
String action = request.getParameter("action");
if ("getAll".equals(action)) { // admin的管理主页面 amain.jsp
PhotoBiz photoBiz = new PhotoBiz();
List<Photo> list = null;
HttpSession session = request.getSession();
try {
list = photoBiz.getAllPhotos();
request.setAttribute("selAllList", list);
request.getRequestDispatcher("/admin/amain.jsp").forward(
request, response);
} catch (Exception e) {
e.printStackTrace();
}
}
if ("addPhoto".equals(action)) {
List<PhotoClass> list = new PhotoClassDAO().getAllClass();
request.setAttribute("list", list);
request.getRequestDispatcher("admin/photo_add.jsp").forward(
request, response);
}
if ("getAllClient".equals(action)) { // 客户的登录页面 main.jsp
PhotoBiz photoBiz = new PhotoBiz();
List<Photo> list = null;
HttpSession session = request.getSession();
try {
list = photoBiz.getAllPhotos();
request.setAttribute("selAllList", list);
request.getRequestDispatcher("main.jsp").forward(request,
response);
} catch (Exception e) {
e.printStackTrace();
}
}
if ("selOne".equals(action)) {
try {
String id = request.getParameter("id");
String str = request.getParameter("str");
if ("client".equals(str)) {
Photo photo = new PhotoDAO().getPhotoById(Integer
.parseInt(id));
PhotoDAO photoDAO = new PhotoDAO();
photoDAO.updateDianJi(photo);
String pathMax = photo.getPath();
String s[] = pathMax.split("_min");
photo.setPath("uploadimg/" + s[0] + s[1]);
request.setAttribute("photo", photo);
List<PingLun> pingLunList = new PingLunDAO()
.searchByPhotoId(Integer.parseInt(id));
request.setAttribute("pingLunList", pingLunList);
request.getRequestDispatcher("/admin/photo_one.jsp")
.forward(request, response);
} else if ("admin".equals(str)) {
Photo photo = new PhotoDAO().getPhotoById(Integer
.parseInt(id));
String pathMax = photo.getPath();
String s[] = pathMax.split("_min");
photo.setPath("uploadimg/" + s[0] + s[1]);
request.setAttribute("photo", photo);
List<PingLun> pingLunList = new PingLunDAO()
.searchByPhotoId(Integer.parseInt(id));
request.setAttribute("pingLunList", pingLunList);
request.getRequestDispatcher("/admin/photo_one_pinglun.jsp")
.forward(request, response);
}
} catch (Exception e) {
e.printStackTrace();
}
}
if ("toupdate".equals(action)) {
String id = request.getParameter("id");
try {
Photo photo = new PhotoDAO().getPhotoById(Integer.parseInt(id));
request.setAttribute("photo", photo);
List<PhotoClass> list = new PhotoClassDAO().getAllClass();
request.setAttribute("list", list);
request.getRequestDispatcher("/admin/photo_manager.jsp")
.forward(request, response);
} catch (SQLException e) {
request.setAttribute("msg", "数据库错误");
request.getRequestDispatcher("/admin/result.jsp").forward(
request, response);
}
}
if ("update1".equals(action)) {
String id = request.getParameter("id");
String name = request.getParameter("name");
String shuoming = request.getParameter("shuoming");
String lid = request.getParameter("lid");
try {
Photo photo = new PhotoDAO().getPhotoById(Integer.parseInt(id));
photo.setName(name);
photo.setShuoming(shuoming);
photo.setLid(Integer.parseInt(lid));
new PhotoDAO().update(photo);
request.setAttribute("msg", "恭喜您,图片信息修改成功");
request.getRequestDispatcher("/admin/result.jsp").forward(
request, response);
} catch (NumberFormatException e) {
request.setAttribute("msg", "数据格式错误");
request.getRequestDispatcher("/admin/result.jsp").forward(
request, response);
e.printStackTrace();
} catch (SQLException e) {
request.setAttribute("msg", "数据库错误");
request.getRequestDispatcher("/admin/result.jsp").forward(
request, response);
e.printStackTrace();
}
}
if ("delete".equals(action)) {
String id = request.getParameter("id");
try {
Photo photo = new PhotoDAO().getPhotoById(Integer.parseInt(id));
new PhotoDAO().delete(photo);
request.setAttribute("msg", "恭喜您,图片已成功删除");
request.getRequestDispatcher("/admin/result.jsp").forward(
request, response);
} catch (NumberFormatException e) {
request.setAttribute("msg", "数据格式错误");
request.getRequestDispatcher("/admin/result.jsp").forward(
request, response);
e.printStackTrace();
} catch (SQLException e) {
request.setAttribute("msg", "数据库错误");
request.getRequestDispatcher("/admin/result.jsp").forward(
request, response);
e.printStackTrace();
}
}
if ("seeAllPhotosByClassId".equals(action)) { // 分类查看是 photoa_all.jsp
String id = request.getParameter("id");
String num = request.getParameter("num");
if (Integer.parseInt(num) == 0) {
request.setAttribute("msg", "对不起该分类下没有文件");
request.getRequestDispatcher("/admin/result.jsp").forward(
request, response);
} else {
try {
List<Photo> photoList = new ArrayList<Photo>();
PhotoDAO photoDAO = new PhotoDAO();
photoList = photoDAO.getAllPhotosByClassId(Integer
.parseInt(id));
request.setAttribute("photoList", photoList);
request.getRequestDispatcher("/admin/photo_all.jsp")
.forward(request, response);
} catch (NumberFormatException e) {
request.setAttribute("msg", "数据格式错误");
request.getRequestDispatcher("/admin/result.jsp").forward(
request, response);
e.printStackTrace();
} catch (SQLException e) {
request.setAttribute("msg", "数据库错误");
request.getRequestDispatcher("/admin/result.jsp").forward(
request, response);
e.printStackTrace();
}
}
}
if ("add".equals(action)) {
boolean flag = false;
Photo photo = new Photo();
PhotoDAO photoDAO = new PhotoDAO();
String msg = "";
try {
Calendar calendar = Calendar.getInstance();
String newfilename = String.valueOf(calendar.getTimeInMillis());
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List<?> items = upload.parseRequest(request);
Iterator<?> iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (item.isFormField()) {
String fieldName = item.getFieldName();
String fieldValue = new String(item.getString("utf-8"));
if ("shuoming".equals(fieldName)) {
photo.setShuoming(fieldValue);
} else if ("name".equals(fieldName)) {
photo.setName(fieldValue);
} else if ("lid".equals(fieldName)) {
photo.setLid(Integer.parseInt(fieldValue));
}
} else {
String fieldName = item.getFieldName();
String fileName = item.getName();
String ext = fileName.substring(fileName
.lastIndexOf('.') + 1);
String contentType = item.getContentType();
boolean isInMemory = item.isInMemory();
long sizeInBytes = item.getSize();
String saveurl = request.getSession()
.getServletContext().getRealPath("/")
+ "uploadimg\\";
String s = this.getServletContext().getRealPath("/");
String newfilenamewithext = newfilename + "." + ext;
File uploadedFile = new File(saveurl
+ newfilenamewithext);
item.write(uploadedFile);
String newfilename_min = ImageUtils.createMinPic(
saveurl, newfilename, ext);
photo.setPath(newfilename_min);
String contenttime = "";
Date dt = new Date();
SimpleDateFormat sdf = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss ");
contenttime = sdf.format(dt);
photo.setContentTime(contenttime);
}
}
flag = photoDAO.insert(photo);
if (flag) {
msg = "恭喜,图片添加成功!";
} else {
msg = "对不起,图片插入失败!";
}
} catch (Exception e) {
e.printStackTrace();
msg = "图片插入失败!";
}
request.setAttribute("msg", msg);
request.getRequestDispatcher("/admin/result.jsp").forward(request,
response);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
public void init() throws ServletException {
}
}
觉得有用,记得一键三连哦!