📄 attachmentsaction.java
字号:
package cn.jsprun.struts.action;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import cn.jsprun.domain.Members;
import cn.jsprun.page.LogPage;
import cn.jsprun.service.posts.AttachmentsService;
import cn.jsprun.service.system.DataBaseService;
import cn.jsprun.struts.form.posts.AttachmentsForm;
import cn.jsprun.utils.BeanFactory;
import cn.jsprun.utils.Common;
import cn.jsprun.utils.DataParse;
public class AttachmentsAction extends DispatchAction {
private AttachmentsService attachmentsService = (AttachmentsService) BeanFactory.getBean("attachments_postService");
private DataBaseService dataBaseService = (DataBaseService) BeanFactory.getBean("dataBaseService");
private DataParse dataParse= (DataParse)BeanFactory.getBean("dataParse");
@SuppressWarnings("unchecked")
public ActionForward fromAttachments(ActionMapping mapping,
ActionForm form, HttpServletRequest request, HttpServletResponse response) {
String searchsubmit = request.getParameter("searchsubmit");
if(searchsubmit==null){
Common.requestforward(response, "admincp.jsp?action=attachments");
return null;
}
AttachmentsForm attachementsForm = null;
HttpSession session = request.getSession();
if (form != null) {
ServletContext context = session.getServletContext();
Map<String,String> settings = (Map<String,String>)context.getAttribute("fsmap");
int currentpage = 1;
String page = request.getParameter("page");
currentpage = page == null || page.equals("") ? 1 : Integer.valueOf(page.trim());
attachementsForm = (AttachmentsForm) form;
String sql = attachmentsService.findByAttachmentsForm(attachementsForm);
List<Map<String,String>> count = dataBaseService.executeQuery("select count(*) as count "+sql);
int totalsize = Common.toDigit(count.get(0).get("count"));
int currpage = currentpage;
LogPage loginpage = new LogPage(totalsize,10,currpage);
if(currentpage>loginpage.getTotalPage()){
currentpage = loginpage.getTotalPage();
}
int beginsize = (currentpage-1)*10;
session.setAttribute("attaforms",attachementsForm);
List<Map<String,String>> list = dataBaseService.executeQuery("select a.aid,t.tid,f.fid,a.filename,a.attachment,t.author,f.name,a.filesize,a.downloads,t.subject,a.remote "+sql+" limit "+beginsize+",10");
if(list!=null && list.size()>0){
List<Map<String,String>> showlist = new ArrayList<Map<String,String>>();
String path = servlet.getServletContext().getRealPath("/")+settings.get("attachdir")+"/";
Map<String,String> ftpmap = dataParse.characterParse(settings.get("ftp"), false);
String attachurl = ftpmap.get("attachurl");
ftpmap = null;
for(Map<String,String> map:list){
if(map.get("remote").equals("1")){
File file = new File(attachurl+"/"+map.get("attachment"));
if(!file.exists()){
map.put("nomatched", "远程附件");
}
map.put("attachment", attachurl+"/"+map.get("attachment"));
}else{
File file = new File(path+map.get("attachment"));
if(!file.exists()){
map.put("nomatched", "附件文件缺失!");
}
map.put("attachment", path+map.get("attachment"));
}
if(attachementsForm.getNomatched()==0||(attachementsForm.getNomatched()==1&&map.get("nomatched")!=null)){
showlist.add(map);
}
}
request.setAttribute("showlist", showlist);
}else{
request.setAttribute("showlist", list);
}
request.setAttribute("logpage", loginpage);
}
short groupid = (Short)session.getAttribute("jsprun_groupid");
Members member = (Members)session.getAttribute("user");
request.setAttribute("forumselect", Common.forumselect(false, false,groupid,member!=null?member.getExtgroupids():"",attachementsForm.getInforum()+"",false));
request.setAttribute("notfirst", "notfirst");
return mapping.findForward("toAttachments");
}
@SuppressWarnings("unchecked")
public ActionForward deleteAttachments(ActionMapping mapping,ActionForm form, HttpServletRequest request,HttpServletResponse resposne) {
String deletesubmit = request.getParameter("deletesubmit");
if(deletesubmit==null){
Common.requestforward(resposne, "admincp.jsp?action=attachments");
return null;
}
String[] deleteAids = request.getParameterValues("delete[]");
if (deleteAids != null) {
HttpSession session = request.getSession();
ServletContext context = session.getServletContext();
Map<String,String> settings = (Map<String,String>)context.getAttribute("fsmap");
String path = servlet.getServletContext().getRealPath("/")+settings.get("attachdir");
String aids="0";
for(String aid:deleteAids){
aids =aids+ ","+aid;
}
String tids = "0";
String pids = "0";
List<Map<String,String>> attachmap = dataBaseService.executeQuery("select tid,pid,attachment,thumb,remote from jrun_attachments where aid in ("+aids+")");
for(Map<String,String> attach:attachmap){
tids += ","+attach.get("tid");
pids += ","+attach.get("pid");
Common.dunlink(attach.get("attachment"), Byte.valueOf(attach.get("thumb")), Byte.valueOf(attach.get("remote")), path);
}
dataBaseService.runQuery("delete from jrun_attachments where aid in ("+aids+")");
dataBaseService.runQuery("update jrun_posts set attachment='0' where pid in ("+pids+")");
List<Map<String,String>> attachment = dataBaseService.executeQuery("SELECT tid FROM jrun_attachments WHERE tid IN ("+tids+") GROUP BY tid ORDER BY pid DESC");
String attachtids="0";
for(Map<String,String> attach:attachment){
attachtids += ","+attach.get("tid");
}
dataBaseService.runQuery("update jrun_threads set attachment='0' where tid in ("+tids+") and tid not in ("+attachtids+")");
attachment = null;attachmap=null;
}
try {
String shalert = "附件列表成功更新。";
resposne.getWriter().write( "<script type='text/javascript'>alert('" + shalert + "');</script>");
resposne.getWriter().write("<script>parent.$('attachforum').searchsubmit.click();</script>");
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@SuppressWarnings("unchecked")
public ActionForward pageAttachments(ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response) {
AttachmentsForm attachementsForm = (AttachmentsForm) request.getSession().getAttribute("attaforms");
if (attachementsForm != null) {
HttpSession session = request.getSession();
ServletContext context = session.getServletContext();
Map<String,String> settings = (Map<String,String>)context.getAttribute("fsmap");
int currentpage = 1;
String page = request.getParameter("page");
currentpage = page == null || page.equals("") ? 1 : Integer.valueOf(page.trim()) ;
String sql = attachmentsService.findByAttachmentsForm(attachementsForm);
List<Map<String,String>> count = dataBaseService.executeQuery("select count(*) as count "+sql);
int totalsize = Common.toDigit(count.get(0).get("count"));
LogPage loginpage = new LogPage(totalsize,10,currentpage);
if(currentpage>loginpage.getTotalPage()){
currentpage = loginpage.getTotalPage();
}
int beginsize = (currentpage-1)*10;
List<Map<String,String>> list = dataBaseService.executeQuery("select a.aid,t.tid,f.fid,a.filename,a.attachment,t.author,f.name,a.filesize,a.downloads,t.subject,a.remote "+sql+" limit "+beginsize+",10");
request.getSession().setAttribute("attaforms",attachementsForm);
if(list!=null && list.size()>0){
List<Map<String,String>> showlist = new ArrayList<Map<String,String>>();
String path = servlet.getServletContext().getRealPath("/")+settings.get("attachdir")+"/";
Map<String,String> ftpmap = dataParse.characterParse(settings.get("ftp"), false);
String attachurl = ftpmap.get("attachurl");
ftpmap = null;
for(Map<String,String> map:list){
if(map.get("remote").equals("1")){
File file = new File(attachurl+"/"+map.get("attachment"));
if(!file.exists()){
map.put("nomatched", "远程附件");
}
map.put("attachment", attachurl+"/"+map.get("attachment"));
}else{
File file = new File(path+map.get("attachment"));
if(!file.exists()){
map.put("nomatched", "附件文件缺失!");
}
map.put("attachment", path+map.get("attachment"));
}
if(attachementsForm.getNomatched()==0||(attachementsForm.getNomatched()==1&&map.get("nomatched")!=null)){
showlist.add(map);
}
}
request.setAttribute("showlist", showlist);
}else{
request.setAttribute("showlist", list);
}
request.setAttribute("logpage", loginpage);
}else{
Common.requestforward(response, "admincp.jsp?action=attachments");
return null;
}
HttpSession session = request.getSession();
short groupid = (Short)session.getAttribute("jsprun_groupid");
Members member = (Members)session.getAttribute("user");
request.setAttribute("forumselect", Common.forumselect(false, false,groupid,member!=null?member.getExtgroupids():"",attachementsForm.getInforum()+"",false));
request.setAttribute("notfirst", "notfirst");
return mapping.findForward("toAttachments");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -