论坛代码.docx
《论坛代码.docx》由会员分享,可在线阅读,更多相关《论坛代码.docx(27页珍藏版)》请在冰豆网上搜索。
论坛代码
packagecom.servlet;
importjava.io.IOException;
importjavax.servlet.RequestDispatcher;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
publicclassAdminIndexServletextendsSuperServlet{
privatestaticfinallongserialVersionUID=1L;
protectedvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{
doPost(request,response);
}
protectedvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{
request.getSession().setAttribute("activation",true);
RequestDispatcherrd=request.getRequestDispatcher(getInitParameter("adminindex"));
rd.forward(request,response);
}
}
packagecom.servlet;
importjava.io.IOException;
importjava.lang.reflect.Method;
importjava.sql.SQLException;
importjava.util.Date;
importjavax.servlet.RequestDispatcher;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importcom.dao.AttachmentDao;
importcom.dao.ReplyDao;
importcom.dao.TopicDao;
importcom.jspsmart.upload.File;
importcom.jspsmart.upload.Files;
importcom.jspsmart.upload.SmartUpload;
importcom.toolsbean.MyFileHandler;
importcom.toolsbean.StringHandler;
importcom.valuebean.AttachmentBean;
importcom.valuebean.ReplyBean;
importcom.valuebean.TopicBean;
importcom.valuebean.UserBean;
publicclassAttachmentServletextendsSuperServlet{
protectedvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{
doPost(request,response);
}
protectedvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{
String[]subPaths=(String[])request.getAttribute("subPaths");
if(subPaths!
=null&&subPaths.length==4){
Stringoperate=subPaths[3];
try{
Methodmd=getClass().getDeclaredMethod(operate,newClass[]{HttpServletRequest.class,HttpServletResponse.class});//根据opName变量值获取名称为opName变量值的方法。
Class数组指定该方法中各参数的类型
md.setAccessible(true);//设为true后将能调用私有方法
md.invoke(this,newObject[]{request,response});//调用方法,Object数组指定方法需要的参数
}catch(Exceptione){
e.printStackTrace();
invalidate(request,response);
}
}
else{
invalidate(request,response);
}
}
privatevoiduploadview(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{
Stringmessage="";
Stringforward="";
try{
IntegertopicId=StringHandler.strToint(request.getParameter("topicId"));
IntegerreplyId=StringHandler.strToint(request.getParameter("replyId"));
if(topicId==null&&replyId==null){
message="
你要为谁添加附件?啊!
";
forward=RUNFAIL;
}
elseif(topicId!
=null){//为主题添加附件
TopicBeantopic=newTopicDao().getTopicSingle(topicId);
if(topic==null){
message="
要添加附件的主题不存在或已经被删除!";
forward=RUNFAIL;
}
else{
forward=INDEXTEMP;
request.setAttribute("mainPage",getInitParameter("addattachmentTopicmain"));
request.setAttribute("attachtopic",topic);
}
}
else{//为回复帖添加附件
ReplyBeanreply=newReplyDao().getReplySingle(replyId);
if(reply==null){
message="
要添加附件的回复帖不存在或已经被删除!";
forward=RUNFAIL;
}
else{
forward=INDEXTEMP;
request.setAttribute("mainPage",getInitParameter("addattachmentReplymain"));
request.setAttribute("attachreply",reply);
}
}
}catch(SQLExceptione){
e.printStackTrace();
}
request.setAttribute("message",message);
RequestDispatcherrd=request.getRequestDispatcher(forward);
rd.forward(request,response);
}
privatevoiduploadrun(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{
intk=-1;
Stringmessage="";
IntegertopicId=StringHandler.strToint(request.getParameter("topicId"));
IntegerreplyId=StringHandler.strToint(request.getParameter("replyId"));
if(replyId==null)//为主题添加附件
replyId=-1;
longmaxsize=Long.parseLong(getInitParameter("maxsize"));//获取允许上传文件的总长度
try{
SmartUploadmyup=newSmartUpload();
myup.initialize(this,request,response);
myup.setTotalMaxFileSize(maxsize);//设置允许上传文件的总长度
myup.upload();//上传文件
Datedate=newDate();//获取当前时间
Stringloginername=((UserBean)request.getSession().getAttribute("loginer")).getMemberName();
Filesfiles=myup.getFiles();//获取所有的上传文件
intcount=files.getCount();//获取上传文件数量
Stringuptime=StringHandler.timeTostr(date);//转化当前时间为字符串
String[]serials=StringHandler.getSerial(date,count);//获取count个以当前时间作为内容的标识符,它们将用来命名上传的文件
AttachmentDaoattachDao=newAttachmentDao();
for(inti=0;iFilesinglefile=files.getFile(i);
if(!
singlefile.isMissing()){//如果有文件
intfilesize=singlefile.getSize();
if(filesize==0)
message+="
文件"+(i+1)+"上传失败!
上传的文件大小为0!
";
else{
Stringfilename=singlefile.getFileName();//获取上传文件的名称
Stringsavename=serials[i]+"_"+loginername+"."+singlefile.getFileExt();
Stringfiletype=singlefile.getContentType().trim();
Stringfiledir=getServletContext().getInitParameter("filedir");//获取存放文件的目录(该目录位于web应用根目录下)
Object[]params={topicId,replyId,savename,filename,filetype,filesize,uptime};
k=attachDao.addAttachInfo(params);//保存文件信息到数据库中
if(k<=0){//信息保存失败!
message+="
文件"+(i+1)+"上传失败!";
}
else{//信息保存成功
if(replyId==-1)//为主题添加附件
newTopicDao().updateTopicAttachment("1",topicId);//将主题的topic_attachment字段设为"1",表示该主题带有附件
else//为回复帖添加附件
newReplyDao().updateReplyAttachment("1",replyId);//将回复帖的reply_attachment字段设为"1",表示该回复帖带有附件
singlefile.saveAs(filedir+savename,File.SAVEAS_VIRTUAL);//保存文件到磁盘的指定目录下
message+="●文件"+(i+1)+":
"+singlefile.getFilePathName()+"上传成功!
";
}
}
}
}
attachDao.closed();
}catch(java.lang.SecurityExceptione1){
k=-1;
message="
上传的文件总大小不允许超过"+(maxsize/1024/1024)+"兆!";
e1.printStackTrace();
}
catch(Exceptione2){
k=-1;
message="
文件上传失败!";
e2.printStackTrace();
}catch(java.lang.OutOfMemoryErrore3){
k=-1;
message="
你上传的文件太大!";
e3.printStackTrace();
}
if(k<=0){
message+="window.history.go(-1)'>>>返回重试
";
message+="topicId="+topicId+"'target='_parent'>>>返回主题";
}
else{
newTopicDao().updateTopicAttachment("1",topicId);
message+="window.history.go(-1)'>>>继续上传
";
message+="topicId="+topicId+"'target='_parent'>>>返回主题";
}
request.setAttribute("message",message);
RequestDispatcherrd=request.getRequestDispatcher(getInitParameter("uploadresult"));
rd.forward(request,response);
}
privatevoiddownloadrun(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{
Integerattachmentid=StringHandler.strToint(request.getParameter("attachmentid"));
if(attachmentid==null){
request.setAttribute("message","
附件ID值错误!
window.history.go(-1)'>返回");
RequestDispatcherrd=request.getRequestDispatcher(RUNFAIL);
rd.forward(request,response);
}
else{
try{
AttachmentDaoattachmentDao=newAttachmentDao();
AttachmentBeanattachmentsingle=attachmentDao.getAttachmentSingle(attachmentid);
attachmentDao.closed();
if(attachmentsingle==null){
request.setAttribute("message","
要下载的文件不存在或已经被删除!
window.history.go(-1)'>返回");
RequestDispatcherrd=request.getRequestDispatcher(RUNFAIL);
rd.forward(request,response);
}
else{
Stringfiledir=getServletContext().getInitParameter("filedir");
Stringsavename=attachmentsingle.getAttachmentSaveName();
Stringfilename=attachmentsingle.getAttachmentFileName();
filename=newString(filename.getBytes("gb2312"),"ISO-8859-1");
Stringfiletype=attachmentsingle.getAttachmentFileType();
Stringdownfile=filedir+savename;
Stringdestfilepathname=getServletContext().getRealPath(downfile);
java.io.Filedeletefile=newjava.io.File(destfilepathname);
if(deletefile.exists()){
SmartUploadmydown=newSmartUpload();
mydown.initialize(this,request,response);
mydown.setContentDisposition(null);
mydown.downloadFile(downfile,filetype,filename);
mydown=null;
}
else{
request.setAttribute("message","
要下载的文件不存在或已经被删除!
window.history.go(-1)'>返回");
RequestDispatcherrd=request.getRequestDispatcher(RUNFAIL);
rd.forward(request,response);
}
}
}catch(Exceptione){
e.printStackTrace();
request.setAttribute("message","
文件下载失败!
window.history.go(-1)'>返回");
RequestDispatcherrd=request.getRequestDispatcher(RUNFAIL);
rd.forward(request,response);
}
}
}
privatevoiddeleteview(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{
Stringmessage="";
Stringforward="";
Integerattachmentid=StringHandler.strToint(request.getParameter("attachmentid"));
if(attachmentid==null){
message="
附件ID值错误!
";
message+="
window.history.go(-1)'>返回";
forward=RUNFAIL;
}
else{
forward=INDEXTEMP;
request.s