工具:eclipse,oracle,smartupload.jar;语言:jsp,java;数据存储:oracle。
实现功能介绍:
主要是新建相册,可以建多个相册,在相册中添加多张照片,删除照片,删除相册,当相册下有照片时先删除照片才能删除相册。
因为每个相册和照片要有所属人,所以顺带有登录功能。
声明:只是后端实现代码,前台无任何样式,代码测试可行,仅供参考。
代码:
数据库连接帮助类:
public class jdbchelper {
public static final string driver = "oracle.jdbc.driver.oracledriver";
public static final string url = "jdbc:oracle:thin:@localhost:1521:xxxx";
public static final string dbname = "scott";
public static final string password = "xxxx";
public static connection getconn() throws exception{
class.forname(driver);
connection conn = drivermanager.getconnection(url, dbname, password);
return conn;
}
}
图片上传时,要修改图片名称,防止上传重名图片将上一张覆盖,这里的做法是将图片名改为由用户id和精确到毫秒的时间组成,修改图片名的帮助类:
public class photoname {
private string ip;
public photoname(string ip) {
super();
this.ip = ip;
}
public string getip() {
return ip;
}
public void setip(string ip) {
this.ip = ip;
}
public string gettime(){
date date = new date();
dateformat df = new simpledateformat("yyyymmddhhmmsssss");
return df.format(date);
}
public string getphotoname(){
return this.ip + this.gettime();
}
}
实现所有这些的接口类:
public interface updao {
/**
* 创建相册名称
*
*/
public int crealbum(albumpojo ap);
/**
*显示所创建的所有相册名称
*/
public list<albumpojo> findallalbum(int id);
public list<photopojo> findallphoto(int id);
/**
* 上传照片
*/
public int upphoto(photopojo pp);
/**
* 删除相册
* @param id 相册id
* @return
*/
public int delalbum(int id);
/**
* 删除照片
* @param id 照片id
* @return
*/
public int delphoto(int id);
/**
* 登录
* @param username
* @param password
* @return
*/
public userpojo login(string username,string password);
}
接口的具体实现类:
public class updaoimpl implements updao {
/* (non-javadoc)
* @see cn.jvsun.dao.updao#crealbum(cn.jvsun.pojo.albumpojo)
* 创建相册名称
*/
public int crealbum(albumpojo ap) {
int albumnum=this.getalbumnum();
connection conn = null;
preparedstatement pstate = null;
try {
conn=jdbchelper.getconn();
conn.setautocommit(false);
string sql="insert into album(id,a_name,user_id)values(?,?,?)";
pstate = conn.preparestatement(sql);
pstate.setint(1, albumnum);
pstate.setstring(2,ap.geta_name());
pstate.setint(3, ap.getuser_id());
pstate.execute();
conn.commit();
} catch (exception e) {
e.printstacktrace();
try {
conn.rollback();//出问题就撤回,全不提交
} catch (sqlexception e1) {
e1.printstacktrace();
}
}finally{
try {
pstate.close();
conn.close();
} catch (sqlexception e) {
e.printstacktrace();
}
}
return albumnum;
}
/* (non-javadoc)
* @see cn.jvsun.dao.updao#upphoto(java.lang.string, java.lang.string, int)
* 上传照片
*/
public int upphoto(photopojo pp) {
int pnum=this.getphotonum();
connection conn = null;
preparedstatement pstate = null;
try {
conn=jdbchelper.getconn();
conn.setautocommit(false);
string sql="insert into photo(id,p_name,p_url,p_albumid)values(?,?,?,?)";
pstate = conn.preparestatement(sql);
pstate.setint(1, pnum);
pstate.setstring(2,pp.getp_name());
pstate.setstring(3, pp.getp_url());
pstate.setint(4, pp.getp_albumid());
pstate.execute();
conn.commit();
} catch (exception e) {
e.printstacktrace();
try {
conn.rollback();//出问题就撤回,全不提交
} catch (sqlexception e1) {
e1.printstacktrace();
}
}finally{
try {
pstate.close();
conn.close();
} catch (sqlexception e) {
e.printstacktrace();
}
}
return pnum;
}
/* (non-javadoc)
* @see cn.jvsun.dao.updao#delalbum(int)
* 删除相册
*/
public int delalbum(int id) {
int result=0;
connection conn = null;
preparedstatement pstate = null;
string sql="delete from album where id="+id+"";
try {
conn=jdbchelper.getconn();
pstate = conn.preparestatement(sql);
result=pstate.executeupdate();
} catch (sqlexception e) {
e.printstacktrace();
} catch (exception e) {
// todo auto-generated catch block
e.printstacktrace();
}finally{
try {
pstate.close();
conn.close();
} catch (sqlexception e) {
// todo auto-generated catch block
e.printstacktrace();
}
}
return result;
}
/* (non-javadoc)
* @see cn.jvsun.dao.updao#delphoto(int)
* 删除照片
*/
public int delphoto(int id) {
int result=0;
connection conn = null;
preparedstatement pstate = null;
string sql="delete from photo where id="+id+"";
try {
conn=jdbchelper.getconn();
pstate = conn.preparestatement(sql);
result=pstate.executeupdate();
} catch (sqlexception e) {
e.printstacktrace();
} catch (exception e) {
// todo auto-generated catch block
e.printstacktrace();
}finally{
try {
pstate.close();
conn.close();
} catch (sqlexception e) {
// todo auto-generated catch block
e.printstacktrace();
}
}
return result;
}
/* (non-javadoc)
* @see cn.jvsun.dao.updao#login(java.lang.string, java.lang.string)
* 用户登录
*/
public userpojo login(string username, string password) {
userpojo user=null;
connection conn = null;
preparedstatement pstate = null;
resultset res = null;
try {
conn=jdbchelper.getconn();
string sql="select id,username from userinfo where username=? and password=?";
pstate = conn.preparestatement(sql);
pstate.setstring(1, username);
pstate.setstring(2, password);
res = pstate.executequery();
while(res.next()){
user=new userpojo(res.getint(1),username,null);
}
} catch (exception e) {
e.printstacktrace();
}finally{
try {
res.close();
pstate.close();
conn.close();
} catch (sqlexception e) {
e.printstacktrace();
}
}
return user;
}
/**
* 相册序列号
*/
public int getalbumnum(){
int albumnum=-1;
connection conn = null;
preparedstatement pstate = null;
resultset res = null;
try {
conn=jdbchelper.getconn();
string sql="select aid.nextval from dual";
pstate=conn.preparestatement(sql);
res=pstate.executequery();
while(res.next()){
albumnum=res.getint(1);
}
} catch (exception e) {
e.printstacktrace();
}finally{
try {
res.close();
pstate.close();
conn.close();
} catch (sqlexception e) {
e.printstacktrace();
}
}
return albumnum;
}
/**
*照片序列号
*/
public int getphotonum(){
int photonum=-1;
connection conn = null;
preparedstatement pstate = null;
resultset res = null;
try {
conn=jdbchelper.getconn();
string sql="select pid.nextval from dual";
pstate=conn.preparestatement(sql);
res=pstate.executequery();
while(res.next()){
photonum=res.getint(1);
}
} catch (exception e) {
e.printstacktrace();
}finally{
try {
res.close();
pstate.close();
conn.close();
} catch (sqlexception e) {
e.printstacktrace();
}
}
return photonum;
}
/* (non-javadoc)
* @see cn.jvsun.dao.updao#findall()
* 显示所创建的相册名
*/
public list<albumpojo> findallalbum(int id) {
list<albumpojo> list= new arraylist<albumpojo>();
connection conn = null;
preparedstatement pstate = null;
resultset res = null;
try {
conn=jdbchelper.getconn();
string sql="select id,a_name,user_id from album where user_id=?";
pstate = conn.preparestatement(sql);
pstate.setint(1, id);
res = pstate.executequery();
while(res.next()){
albumpojo ap=new albumpojo(res.getint(1),res.getstring(2),res.getint(3));
list.add(ap);
}
} catch (exception e) {
e.printstacktrace();
}finally{
try {
res.close();
pstate.close();
conn.close();
} catch (sqlexception e) {
e.printstacktrace();
}
}
return list;
}
/* (non-javadoc)
* @see cn.jvsun.dao.updao#findallphoto(int)
* 显示照片
*/
public list<photopojo> findallphoto(int aid) {
list<photopojo> list= new arraylist<photopojo>();
connection conn = null;
preparedstatement pstate = null;
resultset res = null;
try {
conn=jdbchelper.getconn();
string sql="select id,p_name,p_url from photo where p_albumid=?";
pstate = conn.preparestatement(sql);
pstate.setint(1, aid);
res = pstate.executequery();
while(res.next()){
photopojo pojo=new photopojo(res.getint(1),res.getstring(2),res.getstring(3), aid);
list.add(pojo);
}
} catch (exception e) {
e.printstacktrace();
}finally{
try {
res.close();
pstate.close();
conn.close();
} catch (sqlexception e) {
e.printstacktrace();
}
}
return list;
}
}
用户,相册,照片三个pojo类:
/** * 用户实体类
*
*/
public class userpojo implements serializable{
private static final long serialversionuid = 7554548269035753256l;
private int id;
private string username;
private string password;
public int getid() {
return id;
}
public void setid(int id) {
this.id = id;
}
public string getusername() {
return username;
}
public void setusername(string username) {
this.username = username;
}
public string getpassword() {
return password;
}
public void setpassword(string password) {
this.password = password;
}
public userpojo(int id, string username, string password) {
super();
this.id = id;
this.username = username;
this.password = password;
}
public userpojo(string username, string password) {
this.username = username;
this.password = password;
}
public userpojo() {
super();
// todo auto-generated constructor stub
}
}
/**
* 相册实体类
*
*/
public class albumpojo implements serializable{
private int id;
private string a_name;
private int user_id;
public int getid() {
return id;
}
public void setid(int id) {
this.id = id;
}
public string geta_name() {
return a_name;
}
public void seta_name(string a_name) {
this.a_name = a_name;
}
public int getuser_id() {
return user_id;
}
public void setuser_id(int user_id) {
this.user_id = user_id;
}
public albumpojo(int id, string a_name, int user_id) {
super();
this.id = id;
this.a_name = a_name;
this.user_id = user_id;
}
public albumpojo(string a_name, int user_id) {
this.a_name = a_name;
this.user_id = user_id;
}
public albumpojo() {
super();
// todo auto-generated constructor stub
}
}
/**
*照片实体类
*
*/
public class photopojo implements serializable{
private static final long serialversionuid = 5937149639009957458l;
private int id;
private string p_name;
private string p_url;
private int p_albumid;
public int getid() {
return id;
}
public void setid(int id) {
this.id = id;
}
public string getp_name() {
return p_name;
}
public void setp_name(string p_name) {
this.p_name = p_name;
}
public string getp_url() {
return p_url;
}
public void setp_url(string p_url) {
this.p_url = p_url;
}
public int getp_albumid() {
return p_albumid;
}
public void setp_albumid(int p_albumid) {
this.p_albumid = p_albumid;
}
public photopojo(int id, string p_name, string p_url, int p_albumid) {
super();
this.id = id;
this.p_name = p_name;
this.p_url = p_url;
this.p_albumid = p_albumid;
}
public photopojo(string p_name, string p_url, int p_albumid) {
this.p_name = p_name;
this.p_url = p_url;
this.p_albumid = p_albumid;
}
public photopojo() {
super();
// todo auto-generated constructor stub
}
}
login.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+"/";
%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
<head>
<base href="<%=basepath%>" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
<title>login</title>
</head>
<body>
<%
request.setcharacterencoding("utf-8");
string action=request.getparameter("action");
updao ud=new updaoimpl();
string username=request.getparameter("username");
string password=request.getparameter("password");
userpojo pojo=ud.login(username, password);
if("log".equals(action)){
if(pojo==null){
%>
<h1>登录失败</h1>
<%
}else{
request.getsession().setattribute("username", username);
request.getsession().setattribute("userid", pojo.getid());
response.sendredirect("index.jsp");
}
}
%>
<form action="login.jsp?action=log" method="post">
<input type="text" name="username" placeholder="请输入用户名"/>
<input type="password" name="password" placeholder="请输入密码"/>
<input type="submit"/>
</form>
</body>
</html>
index.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+"/";
%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
<head>
<base href="<%=basepath%>" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
<title>person message</title>
</head>
<body>
<center>相册界面</center>
当前用户:<%=request.getsession().getattribute("username")%> <br>
<a href="cre.jsp" rel="external nofollow" >去创建相册</a><br>
我的所有相册:<br>
<%
int userid=(integer)request.getsession().getattribute("userid");
updao dao=new updaoimpl();
list<albumpojo> list=dao.findallalbum(userid);
for(albumpojo pojo:list){
%>
<tr>
<a>相册id:</a><td><%=pojo.getid() %></td>
<a>相册名称:</a><td><%=pojo.geta_name() %></td>
<a>创建者id:</a><td><%=pojo.getuser_id() %></td>
<td><a href="up.jsp?aid=<%=pojo.getid() %>" rel="external nofollow" >添加照片</a></td>
<td><a href="show.jsp?phid=<%=pojo.getid() %>" rel="external nofollow" >查看照片</a></td>
<td><a href="del.jsp?aid=<%=pojo.getid() %>" rel="external nofollow" >删除相册</a></td>
</tr><br>
<%
}
%>
</body>
</html>
cre.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+"/";
%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
<head>
<base href="<%=basepath%>" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
<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" name="cre" 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+"/";
%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
<head>
<base href="<%=basepath%>" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
<title>上传照片</title>
</head>
<body>
<%
int aid=integer.parseint(request.getparameter("aid"));
%>
<form action="upcheck.jsp" method="post" enctype="multipart/form-data">
<input type="hidden" name="aid" value="<%=aid %>"/>
<input type="file" name="photo"/>
<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("/")+
"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+"/";
%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
<head>
<base href="<%=basepath%>" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
<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() %>" width="100" height="100"/></td>
<a href="photo_del.jsp?pid=<%=pojo.getid() %>" rel="external nofollow" >删除照片:</a></td><br>
</tr><br>
<%} %>
</body>
</html>
photo_del.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+"/";
%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
<head>
<base href="<%=basepath%>" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
<title>del</title>
</head>
<body>
<%
int pid=integer.parseint(request.getparameter("pid"));
int result=0;
updao dao=new updaoimpl();
result=dao.delphoto(pid);
if(result==1){
out.println("<script>alert('删除成功');window.location.href('show.jsp');</script>");
}else{
out.println("<script>alert('出错了');window.location.href('show.jsp');</script>");
}
%>
</body>
</html>
del.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+"/";
%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
<head>
<base href="<%=basepath%>" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >
<title>del</title>
</head>
<body>
<%
int aid=integer.parseint(request.getparameter("aid"));
int result=0;
updao dao=new updaoimpl();
result=dao.delalbum(aid);
if(result==1){
out.println("<script>alert('删除成功');window.location.href('index.jsp');</script>");
}else{
out.println("<script>alert('删除失败,请先把相册中的照片删掉');window.location.href('index.jsp');</script>");
}
%>
</body>
</html>
数据库的建表语句:
-- create table create table userinfo ( id number, username varchar2(30), password varchar2(30) ) tablespace users pctfree 10 initrans 1 maxtrans 255 storage ( initial 64 minextents 1 maxextents unlimited ); -- create/recreate primary, unique and foreign key constraints alter table userinfo add constraint pid primary key (id) disable; --上传者 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -- create table create table album ( id number not null, a_name varchar2(30), user_id number ) tablespace users pctfree 10 initrans 1 maxtrans 255 storage ( initial 64 minextents 1 maxextents unlimited ); -- create/recreate primary, unique and foreign key constraints alter table album add constraint al_pid primary key (id) using index tablespace users pctfree 10 initrans 2 maxtrans 255 storage ( initial 64k minextents 1 maxextents unlimited ); alter table album add constraint userid foreign key (user_id) references userinfo (id) disable; --相册表 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -- create table create table photo ( id number, p_name varchar2(30), p_url varchar2(50), p_albumid number(30) ) tablespace users pctfree 10 initrans 1 maxtrans 255 storage ( initial 64 minextents 1 maxextents unlimited ); -- create/recreate primary, unique and foreign key constraints alter table photo add constraint alb_id foreign key (p_albumid) references album (id); --相片表
好了,所有代码就写完了,切记,需要smartupload.jar包,没有的童鞋可以去下载:
smartuploadjar包
以上所述是小编给大家介绍的jsp实现仿qq空间新建多个相册名称并向相册中添加照片功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对硕编程网站的支持!
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!