<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ page import="cn.jvsun.DAO.Impl.*" %> <%@ page import="cn.jvsun.POJO.*" %> <%@ page import="cn.jvsun.DAO.*" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"https://www.jb51.net/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="https://www.jb51.net/<%=basePath%>" > <title>up photo</title> </head> <body> <% request.setCharacterEncoding("utf-8"); String action=request.getParameter("action"); UpDAO ud=new UpDAOImpl(); String toCre=request.getParameter("cre"); int userId=(Integer)request.getSession().getAttribute("userid"); if("cre".equals(action)){ AlbumPOJO ap=new AlbumPOJO(toCre,userId); int aNum=ud.creAlbum(ap); if(aNum!=-1){ response.sendRedirect("index.jsp"); }else{ %> <h1>创建相册失败</h1> <% } } %> <form action="cre.jsp?action=cre" method="post"> <input type="text" placeholder="请输入您要创建的相册名称"/> <input type="submit" value="确定"> </form> </body> </html>
up.jsp上传照片
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ page import="cn.jvsun.DAO.Impl.*" %> <%@ page import="cn.jvsun.POJO.*" %> <%@ page import="cn.jvsun.DAO.*" %> <%@ page import="cn.jvsun.tools.*" %> <%@page import="org.lxh.smart.*" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"https://www.jb51.net/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="https://www.jb51.net/<%=basePath%>" > <title>上传照片</title> </head> <body> <% int aid=Integer.parseInt(request.getParameter("aid")); %> <form action="upCheck.jsp" method="post" enctype="multipart/form-data"> <input type="hidden" value="<%=aid %>"/> <input type="file"/> <input type="submit" value="确认上传"/> </form> </body> </html>
upCheck.jsp上传照片的处理页
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ page import="cn.jvsun.DAO.Impl.*" %> <%@ page import="cn.jvsun.POJO.*" %> <%@ page import="cn.jvsun.DAO.*" %> <%@ page import="cn.jvsun.tools.*" %> <%@page import="org.lxh.smart.*" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title></title> </head> <body> <% String ip = request.getRemoteAddr(); ip = ip.replaceAll(":",""); PhotoName pn=new PhotoName(ip); String pName = pn.getPhotoName();//照片名字,是由IP加当前时间组成 SmartUpload smartupload = new SmartUpload();//实例化上传操作的对象 //初始化上传文件 smartupload.initialize(pageContext); //准备上传 smartupload.upload(); int albumId=Integer.parseInt(smartupload.getRequest().getParameter("aid")); //取得文件的后缀 String endName = smartupload.getFiles().getFile(0).getFileExt(); //文件保存的路径 /*String p_url = getServletContext().getRealPath("https://www.jb51.net/")+ "file/"+pName+"."+endName;*/ String p_url="K:/workspace/Xiangce/WebRoot/file/"+pName+"."+endName; //保存文件 smartupload.getFiles().getFile(0).saveAs(p_url); UpDAO ad=new UpDAOImpl(); PhotoPOJO pojo=new PhotoPOJO(pName+"."+endName,p_url,albumId); int photoNum=ad.upPhoto(pojo); if(photoNum != -1){ request.getSession().setAttribute("phid", albumId); response.sendRedirect("show.jsp"); } else { %> 上传失败 <% } %> </body> </html>
show.jsp显示照片及信息页:
代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ page import="cn.jvsun.DAO.Impl.*" %> <%@ page import="cn.jvsun.POJO.*" %> <%@ page import="cn.jvsun.DAO.*" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"https://www.jb51.net/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="https://www.jb51.net/<%=basePath%>" > <title>My JSP 'show.jsp' starting page</title> </head> <body> <center>相册界面</center> 当前用户:<%=request.getSession().getAttribute("username")%> <br> <% int phid=(Integer)request.getSession().getAttribute("phid"); UpDAO dao=new UpDAOImpl(); List<PhotoPOJO> list=dao.findAllPhoto(phid); for(PhotoPOJO pojo:list){ %> <tr> <a>照片id:</a><td><%=pojo.getId() %></td><br> <a>照片名称:</a><td><%=pojo.getP_name() %></td><br> <a>照片路径:</a><td><%=pojo.getP_url() %></td><br> <a>照片所属相册名称:</a><td><%=pojo.getP_albumId() %></td><br> <td><img src="<%=path%>/file/<%=pojo.getP_name() %>"/></td> <a href="photo_del.jsp?pid=<%=pojo.getId() %>" >删除照片:</a></td><br> </tr><br> <%} %> </body> </html>
photo_del.jsp删除照片