论坛代码Word下载.docx
《论坛代码Word下载.docx》由会员分享,可在线阅读,更多相关《论坛代码Word下载.docx(27页珍藏版)》请在冰豆网上搜索。
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{
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="
<
li>
你要为谁添加附件?
啊!
/li>
forward=RUNFAIL;
}
elseif(topicId!
=null){//为主题添加附件
TopicBeantopic=newTopicDao().getTopicSingle(topicId);
if(topic==null){
message="
要添加附件的主题不存在或已经被删除!
forward=RUNFAIL;
}
else{
forward=INDEXTEMP;
request.setAttribute("
mainPage"
getInitParameter("
addattachmentTopicmain"
attachtopic"
topic);
}
else{//为回复帖添加附件
ReplyBeanreply=newReplyDao().getReplySingle(replyId);
if(reply==null){
要添加附件的回复帖不存在或已经被删除!
addattachmentReplymain"
attachreply"
reply);
}catch(SQLExceptione){
e.printStackTrace();
request.setAttribute("
message"
message);
RequestDispatcherrd=request.getRequestDispatcher(forward);
privatevoiduploadrun(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{
intk=-1;
IntegertopicId=StringHandler.strToint(request.getParameter("
IntegerreplyId=StringHandler.strToint(request.getParameter("
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;
i<
count;
i++){//逐个获取上传的文件
Filesinglefile=files.getFile(i);
if(!
singlefile.isMissing()){//如果有文件
intfilesize=singlefile.getSize();
if(filesize==0)
message+="
文件"
+(i+1)+"
上传失败!
上传的文件大小为0!
br>
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+="
else{//信息保存成功
if(replyId==-1)//为主题添加附件
newTopicDao().updateTopicAttachment("
1"
topicId);
//将主题的topic_attachment字段设为"
,表示该主题带有附件
else//为回复帖添加附件
newReplyDao().updateReplyAttachment("
replyId);
//将回复帖的reply_attachment字段设为"
,表示该回复帖带有附件
singlefile.saveAs(filedir+savename,File.SAVEAS_VIRTUAL);
//保存文件到磁盘的指定目录下
●文件"
:
b>
fontcolor='
red'
>
+singlefile.getFilePathName()+"
/b>
/font>
上传成功!
attachDao.closed();
}catch(java.lang.SecurityExceptione1){
k=-1;
message="
上传的文件总大小不允许超过"
+(maxsize/1024/1024)+"
兆!
e1.printStackTrace();
catch(Exceptione2){
message="
文件上传失败!
e2.printStackTrace();
}catch(java.lang.OutOfMemoryErrore3){
你上传的文件太大!
e3.printStackTrace();
if(k<
=0){
message+="
ahref='
javascript:
window.history.go(-1)'
返回重试<
/a>
+request.getContextPath()+"
/visit/topic/a/view?
topicId="
+topicId+"
'
target='
_parent'
返回主题<
else{
newTopicDao().updateTopicAttachment("
message+="
继续上传<
}
uploadresult"
privatevoiddownloadrun(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{
Integerattachmentid=StringHandler.strToint(request.getParameter("
attachmentid"
if(attachmentid==null){
"
附件ID值错误!
返回<
RequestDispatcherrd=request.getRequestDispatcher(RUNFAIL);
try{
AttachmentDaoattachmentDao=newAttachmentDao();
AttachmentBeanattachmentsingle=attachmentDao.getAttachmentSingle(attachmentid);
attachmentDao.closed();
if(attachmentsingle==null){
要下载的文件不存在或已经被删除!
RequestDispatcherrd=request.getRequestDispatcher(RUNFAIL);
rd.forward(request,response);
Stringfiledir=getServletContext().getInitParameter("
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("
RequestDispatcherrd=request.getRequestDispatcher(RUNFAIL);
}
}
}catch(Exceptione){
request.setAttribute("
文件下载失败!
RequestDispatcherrd=request.getRequestDispatcher(RUNFAIL);
rd.forward(request,response);
privatevoiddeleteview(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{
forward=RUNFAIL;
forward=INDEXTEMP;
request.s