博客系统开发推送第五季----个人相册模块

来源:互联网 发布:eclipse的java程序编码 编辑:程序博客网 时间:2024/05/22 03:15

       # 博客模块的功能已经完善得差不多了,常见得功能基本实现,现在步入个人相册模块(这是本人自己添加展现用户个人风采的模块)

       # 部分核心代码:

Ascanner.jsp

<%@page import="bean.Album"%><%@page import="java.util.List"%><%@page import="dao.AlbumDaoImpl"%><%@page import="dao.AlbumDao"%><%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>我的相册</title></head><body><center><h1>我的所有相册</h1><hr/><table border="1" width="500px">   <tr>      <td>相册id</td><td>相册名称</td><td>操作</td>   </tr>    <%      request.setCharacterEncoding("utf-8");       response.setCharacterEncoding("utf-8");       response.setHeader("Content-Type","text/html; charset=utf-8");         AlbumDao dao = new AlbumDaoImpl();     List<Album> list = dao.findAll();     for(int i=0;i<list.size();i++){     Album album = list.get(i);     System.out.println(album.getAlbum_name());   %>     <tr>        <td><%=album.getAlbum_id() %></td>         <td><%=album.getAlbum_name() %></td>        <td>           <br/>   <a href="Pscanner.jsp?album_id=<%=album.getAlbum_id()%>">查看相册</a><br/>   <a href="Upload.jsp?album_id=<%=album.getAlbum_id()%>">添加照片</a><br/>   <a href="RenameAlbum.jsp?album_id=<%=album.getAlbum_id()%>&album_name=<%= java.net.URLEncoder.encode(album.getAlbum_name(),"UTF-8")%>">编辑</a><br/>   <a href="${pageContext.request.contextPath}/DeleteAlbum?album_id=<%=album.getAlbum_id()%>">删除</a><br/><br/>        </td>     <tr>   <%} %></table><br/><br/><center><a href="CreateAlbum.jsp">创建相册</a>    <a href="list.jsp"  >返回上一页</a>    <a href="form.jsp"  >返回主界面</a></center></center></body></html>


Pscanner.jsp

<%@page import="bean.Picture"%><%@page import="java.util.List"%><%@page import="dao.PictureDaoImpl"%><%@page import="dao.PictureDao"%><%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8" isELIgnored="false" %><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>相片浏览</title></head><body><center><table border="1" width="600">    <tr>       <td>相片id</td><td>相片名称</td><td>图片</td><td>相片描述</td><td>操作</td>    </tr>    <%       request.setCharacterEncoding("utf-8");         response.setCharacterEncoding("utf-8");         response.setHeader("Content-Type","text/html; charset=utf-8");        String id = request.getParameter("album_id");       int album_id = Integer.parseInt(id);       System.out.println("相册ID:"+album_id);       PictureDao dao = new PictureDaoImpl();       List<Picture> list = dao.getAllPicture(album_id);       for(int i=0;i<list.size();i++){       Picture picture = list.get(i);    // System.out.println("相片的名字为:" + picture.getPicture_name());    %>     <tr>        <td><%=picture.getPicture_id() %></td>        <td><%=picture.getPicture_name() %></td>        <td><img src="${pageContext.request.contextPath}/showImage?picture_id=<%=picture.getPicture_id() %>" width="200"></td>        <td><%=picture.getPicture_doc() %></td>        <td><a href="Pediet.jsp?picture_id=<%=picture.getPicture_id() %>&picture_name=<%=java.net.URLEncoder.encode(picture.getPicture_name(),"UTF-8")%>&picture_doc=<%=java.net.URLEncoder.encode(picture.getPicture_doc(),"UTF-8")%>">编辑</a><br><br><a href="${pageContext.request.contextPath}/DeletePicture?picture_id=<%=picture.getPicture_id() %>">删除</a><br></td>     </tr>    <%} %></table><br/><br/><a href="Ascanner.jsp">返回上一页</a></center></body></html>


Pediet.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>编辑相片</title></head><body><hr/><center>  <%    String picture_name=request.getParameter("picture_name");    picture_name=java.net.URLDecoder.decode(picture_name,"UTF-8");    String picture_doc=request.getParameter("picture_doc");    picture_doc=java.net.URLDecoder.decode(picture_doc,"UTF-8");  %>  <form action="${pageContext.request.contextPath}/EdietPicture" method="post" >     <p>照片id:<input type="text" name="picture_id" value="<%=request.getParameter("picture_id") %>" readonly></p>     <p>照片名称:<input type="text" name="picture_name" value="<%=picture_name %>" ></p>     <p>描述:<textarea name="picture_doc" cols="50" rows="10"><%=picture_doc %></textarea></p>     <p><input type="submit" value="修改"></p>  </form></center></body></html>

RenameAlbum.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>重命名相册</title></head><body><center><h2>相册重命名</h2><hr/><%   //String album_name = new String(request.getParameter("album_name").getBytes("UTF-8"));  // album_name=java.net.URLDecoder.decode(album_name, "UTF-8");      String album_name=request.getParameter("album_name");   album_name=java.net.URLDecoder.decode(album_name,"UTF-8");%><form action="${pageContext.request.contextPath}/RenameAlbum" method="post" ><p>相册id:<input type="text" name="albumId" value="<%=request.getParameter("album_id") %>" readonly></p><p>相册名:<input type="text" name="albumName" value="<%=album_name%>"></p> <p><input type="submit" value="修改"></p></form></center></body></html>


AlbumDaoImpl.java

package dao;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.List;import com.mysql.jdbc.Connection;import com.mysql.jdbc.PreparedStatement;import DBConnect.DButil;import bean.Album;import bean.Article;public class AlbumDaoImpl implements AlbumDao {@Overridepublic void add(Album album) {// TODO Auto-generated method stubString sql="insert into album(album_name) values(?)";          Connection conn=DButil.open();         try {  PreparedStatement pstmt=(PreparedStatement) conn.prepareStatement(sql);    pstmt.setString(1,album.getAlbum_name());  pstmt.executeUpdate();  System.out.println("添加数据成功!");} catch (SQLException e) {// TODO: handle exceptionSystem.out.println("添加数据失败!");          e.printStackTrace();  } finally {              DButil.close(conn);       }   }@Overridepublic List<Album> findAll() {// TODO Auto-generated method stubString sql="select * from album";Connection conn=DButil.open(); List<Album> list = new ArrayList<Album>();try {  PreparedStatement pstmt=(PreparedStatement) conn.prepareStatement(sql);              ResultSet rs=pstmt.executeQuery(sql);//执行查询该表              while(rs.next())              {               int album_id = rs.getInt(1);            String album_name = rs.getString(2);            Album album = new Album();            album.setAlbum_id(album_id);            album.setAlbum_name(album_name);                         System.out.println(album);                list.add(album);                System.out.println("查询相册成功!");            }          } catch (SQLException e) {              // TODO Auto-generated catch block              e.printStackTrace();             System.out.println("查询相册失败!");        }          finally {              DButil.close(conn);          }  return list;}@Overridepublic int deleteAlbum(int album_id) {// TODO Auto-generated method stubString sql="delete from album where album_id=?";Connection conn=DButil.open(); try {              PreparedStatement pstmt=(PreparedStatement) conn.prepareStatement(sql);              pstmt.setInt(1, album_id);int n=pstmt.executeUpdate();System.out.println("已成功删除相册!");return n;         } catch (SQLException e) {              // TODO Auto-generated catch block          System.out.println("删除相册失败!");            e.printStackTrace();          }          finally {               DButil.close(conn);          }    return 0;}@Overridepublic void renameAlbum(Album album) {// TODO Auto-generated method stubString sql="update album set album_name=? where album_id=?";Connection conn=DButil.open(); try {              PreparedStatement pstmt=(PreparedStatement) conn.prepareStatement(sql);              pstmt.setString(1, album.getAlbum_name());   //占位符必须按顺序来排            pstmt.setInt(2, album.getAlbum_id());int n=pstmt.executeUpdate();System.out.println("已成功修改相册!");         } catch (SQLException e) {              // TODO Auto-generated catch block          System.out.println("修改相册失败!");            e.printStackTrace();          }          finally {               DButil.close(conn);          }    }}

PictureDaoImpl.java

package dao;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.List;import com.mysql.jdbc.Connection;import com.mysql.jdbc.PreparedStatement;import DBConnect.DButil;import bean.Article;import bean.Picture;public class PictureDaoImpl implements PictureDao {public void AddPicture(Picture picture) {// TODO Auto-generated method stubString sql="insert into picture(picture_doc,picture_file,album_id,picture_name) values(?,?,?,?)";          Connection conn=DButil.open();         try {  PreparedStatement pstmt=(PreparedStatement) conn.prepareStatement(sql);    pstmt.setString(1, picture.getPicture_doc());  pstmt.setString(2, picture.getPicture_file());  pstmt.setInt(3, picture.getAlbum_id());  pstmt.setString(4, picture.getPicture_name());  pstmt.executeUpdate();  System.out.println("添加相片成功!");} catch (SQLException e) {// TODO: handle exceptionSystem.out.println("添加相片失败!");        e.printStackTrace();  } finally {              DButil.close(conn);       }   }@Overridepublic List<Picture> getAllPicture(int album_id) {// TODO Auto-generated method stubString sql="select * from picture where album_id=?";          Connection conn=DButil.open();          List<Picture> list = new ArrayList<Picture>();        try {              PreparedStatement pstmt=(PreparedStatement) conn.prepareStatement(sql);             pstmt.setInt(1,album_id);              ResultSet rs=pstmt.executeQuery();              while(rs.next())              {               Picture picture = new Picture();            int picture_id = rs.getInt("picture_id");            picture.setPicture_id(picture_id);            String picture_doc = rs.getString("picture_doc");            picture.setPicture_doc(picture_doc);            String picture_file = rs.getString("picture_file");            picture.setPicture_file(picture_file);            String picture_name = rs.getString("picture_name");            picture.setPicture_name(picture_name);//            int picture_id = rs.getInt(2);//            String picture_doc = rs.getString(3);//            String picture_file = rs.getString(4);//            String picture_name = rs.getString(5);   //            //            Picture picture = new Picture();//              picture.setPicture_id(picture_id);//              picture.setPicture_doc(picture_doc);//              picture.setPicture_file(picture_file);//              picture.setAlbum_id(album_id);//              picture.setPicture_name(picture_name);                                System.out.println(picture);                System.out.println("查询相片成功!");                list.add(picture);            }          } catch (SQLException e) {              // TODO Auto-generated catch block          System.out.println("查询相片失败!");            e.printStackTrace();          }          finally {               DButil.close(conn);          }  return list;}@Overridepublic Picture get(int picture_id) {// TODO Auto-generated method stubString sql="select * from picture where picture_id=?";          Connection conn=DButil.open();          Picture picture = new Picture();        try {              PreparedStatement pstmt=(PreparedStatement) conn.prepareStatement(sql);             pstmt.setInt(1,picture_id);              ResultSet rs=pstmt.executeQuery();              if(rs.next())              {               String picture_doc = rs.getString(2);            String picture_file = rs.getString(3);            int album_id = rs.getInt(4);            String picture_name = rs.getString(5);                picture.setPicture_id(picture_id);                picture.setPicture_doc(picture_doc);                picture.setPicture_file(picture_file);                picture.setAlbum_id(album_id);                picture.setPicture_name(picture_name);                                System.out.println(picture);                System.out.println("查询相片成功!");                return picture;            }          } catch (SQLException e) {              // TODO Auto-generated catch block          System.out.println("查询相片失败!");            e.printStackTrace();          }          finally {               DButil.close(conn);          }  return picture;}@Overridepublic void update(Picture picture) {// TODO Auto-generated method stubString sql="update picture set picture_name=? , picture_doc=? where picture_id=?";Connection conn=DButil.open(); try {              PreparedStatement pstmt=(PreparedStatement) conn.prepareStatement(sql);              pstmt.setString(1, picture.getPicture_name());   //占位符必须按顺序来排            pstmt.setString(2, picture.getPicture_doc());            pstmt.setInt(3, picture.getPicture_id());int n=pstmt.executeUpdate();System.out.println("已成功修相片信息");         } catch (SQLException e) {              // TODO Auto-generated catch block          System.out.println("修改相片信息失败!");            e.printStackTrace();          }          finally {               DButil.close(conn);          }     }@Overridepublic int delete(int picture_id) {// TODO Auto-generated method stubString sql="delete from picture where picture_id=?";Connection conn=DButil.open();  try {              PreparedStatement pstmt=(PreparedStatement) conn.prepareStatement(sql);              pstmt.setInt(1, picture_id);int n=pstmt.executeUpdate();System.out.println("已成功删除数据!");return n;         } catch (SQLException e) {              // TODO Auto-generated catch block          System.out.println("删除数据失败!");            e.printStackTrace();          }          finally {               DButil.close(conn);          }    return 0;}}


UploadPicture.java

package servlet;import java.io.IOException;import java.util.Collection;import javax.servlet.ServletException;import javax.servlet.annotation.MultipartConfig;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.Part;import bean.Picture;import dao.PictureDao;import dao.PictureDaoImpl;@MultipartConfig(location = "C://picture", maxFileSize = 8388608, fileSizeThreshold = 819200)public class UploadPicture extends HttpServlet {private static final long serialVersionUID = 1L;@SuppressWarnings("unused")private static final String imageDir = "C://picture";           public UploadPicture() {        super();        // TODO Auto-generated constructor stub    }//protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//// TODO Auto-generated method stub//doPost(request, response); //}protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stub//设置编码          request.setCharacterEncoding("utf-8");          response.setCharacterEncoding("utf-8");          response.setHeader("Content-Type","text/html; charset=utf-8");                  String picture_doc = request.getParameter("picturedoc");//相片描述        String album = request.getParameter("albumid");//相册ID        int album_id = Integer.parseInt(album);        String picture_name = request.getParameter("picturename");        String picture_file = picture_name + System.nanoTime();                Picture picture = new Picture();        picture.setPicture_doc(picture_doc);        picture.setPicture_file(picture_file);        picture.setAlbum_id(album_id);        picture.setPicture_name(picture_name);        PictureDao dao = new PictureDaoImpl();                //上传图片,与数据库相关联        try {dao.AddPicture(picture);//获得相片的id,传递到jsp页面中Collection<Part> parts = request.getParts();for(Part part : parts){if (part.getName().equals("picturefile")) {part.write(picture_file);System.out.println("图片上传成功!");break;}response.sendRedirect("/UseTest/ueditor/Ascanner.jsp");}} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}}

EdietPicture.java

package servlet;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import bean.Picture;import dao.PictureDao;import dao.PictureDaoImpl;public class EdietPicture extends HttpServlet {private static final long serialVersionUID = 1L;    public EdietPicture() {        super();        // TODO Auto-generated constructor stub    }protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stubdoPost(request, response);}protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stub//设置编码          request.setCharacterEncoding("utf-8");          response.setCharacterEncoding("utf-8");          response.setHeader("Content-Type","text/html; charset=utf-8");                  String ID = request.getParameter("picture_id");        int picture_id = Integer.parseInt(ID);        String picture_name = request.getParameter("picture_name");        String picture_doc = request.getParameter("picture_doc");        Picture picture = new Picture();        picture.setPicture_id(picture_id);        picture.setPicture_name(picture_name);        picture.setPicture_doc(picture_doc);                PictureDao dao = new PictureDaoImpl();        try {dao.update(picture);;System.out.println("修改信息成功!");response.sendRedirect("/UseTest/ueditor/Ascanner.jsp");} catch (Exception e) {// TODO Auto-generated catch blockSystem.out.println("修改信息失败!");e.printStackTrace();}}}

 # 部分功能截图:











  # 查看"五一出游"相册相片


原创粉丝点击