⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uploadaction.java

📁 文章管理系统 用Java开发,以Struts为框架,以Jdbc链接Mysql数据库。系统属性:系统属性设置、留言管理、友情链接管理、网站调查、公告管理 关于我们、版权声明、联系我们
💻 JAVA
字号:
package com.yhcms.manage.upload.action;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.upload.FormFile;

import com.yhcms.db.DBConnException;
import com.yhcms.manage.admin.bean.AdminUser;
import com.yhcms.manage.upload.bean.UpFile;
import com.yhcms.manage.upload.biz.UpLoadBiz;
import com.yhcms.manage.upload.dao.UpFileDaoIm;
import com.yhcms.manage.upload.form.UpLoadForm;
import com.yhcms.manage.upload.itface.UpFileDao;
import com.yhcms.utils.DateUtils;
import com.yhcms.utils.ReqUtils;
/**
 * <p>Title:系统上传附件Action</p>
 * <li>系统上传附件</li>
 * <br><b>CopyRight: yyhweb[由由华网]</b>
 * @author stephen
 * @version YH-2.0
 */
public class UpLoadAction extends Action{
	public ActionForward execute(ActionMapping actionmapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response) throws DBConnException, UnsupportedEncodingException{

		Logger yhlog = Logger.getLogger(UpLoadAction.class.getName());
		ActionMessages errors = new ActionMessages();
		HttpSession session = request.getSession();
				
		AdminUser curUser = (AdminUser) session.getAttribute("yhcmsuser");
		int userid = 0;
		if(curUser!=null){
			userid = curUser.getId();
		}
		List attlist = null; // 用来获得该文章的所有附件列表
		int allowsize = 6;  // 允许上传文件数
		int leaveNum = 0;	 // 还可以上传文件数
		
        // 取得上传文件的两个列表 
		UpLoadForm upform = (UpLoadForm )form;
		UpFileDao updao = UpFileDaoIm.getInstance();
		boolean isUploadOk = false;
        boolean isDataOk = false;
        List uplist = null;   // 上传文件对应系统上传文件列表
        List formlist = null; // 上传文件列表
        int artId = 0;
        int fId = 0;
        String fileaction="";
        String action = "";
        String delFilePath = "";
        uplist = upform.getUplist();
        formlist = upform.getFormlist();
        action = upform.getAction();
        artId = upform.getArtId();
        fId = ReqUtils.getInt(request,"fId");
        delFilePath = ReqUtils.getString(request,"filePath");
        fileaction = ReqUtils.getString(request,"faction");
        String ymd = "";
        String filePath = "";
        String uploadDir = "";
        
        if(fileaction.equals("delete")){
        	uploadDir = request.getRealPath("")+"/app/upload/";
        	UpLoadBiz.delFile(uploadDir+delFilePath);
        	UpLoadBiz.deleteDao(fId);
        	
        	attlist = getArtFiles(artId,yhlog);
        	leaveNum = allowsize-attlist.size();
        	request.setAttribute("allattmts", attlist);
    		request.setAttribute("curNum",leaveNum);
    		request.setAttribute("action",action);
    		request.setAttribute("artId",artId);
    		return actionmapping.findForward("Display");
        }
        if(uplist!=null && uplist.size()>0){
        	
	        //  创建路径
        	uploadDir = request.getRealPath("")+"/app/upload";
	        int year = DateUtils.getYear();
	        int month = DateUtils.getMonth();
	        int day = DateUtils.getDay();
	        ymd = year + "/" + month + "/" + day;
	        filePath = uploadDir + "/" + ymd;
	        File file = new File(filePath);
	        if(!file.exists())
	            file.mkdirs();
	        else
	        	file = null;
        }
        // 系统属性对上传文件限制
        int maxid = 0;
        long maxSize = 2048000;
        String allow = "gif,jpg,png,bmp,swf,rar,zip,doc,txt";
        String ftime = DateUtils.getCurFormatDate("yyyy-MM-dd");
        UpFile curFile = null;
        // 循环处理上传的文件
        for(int i=0;i<uplist.size();i++){
        	curFile = (UpFile) uplist.get(i);	   // 取得一个上传文件
        	if(curFile!=null){
        		if(curFile.getFilesize()>maxSize){ // 如果该文件大小超过规定大小
        			if(!isUploadOk){
        				isUploadOk = true;
        			}
        			yhlog.warn("The upload file's size is bigger than maxsize!");
        			((FormFile)formlist.get(i)).destroy();
        		}else{
        			if(curFile.getFiletype().length()<=0 || curFile.getFiletype().indexOf(",")!=-1 || allow.indexOf(curFile.getFiletype())==-1){
        				if(!isUploadOk){
        					isUploadOk = true;
        				}
        				yhlog.warn("The Upload file ext not right!");
        				((FormFile)formlist.get(i)).destroy();
        			}else{						  // 设置该上传文件对应系统文件的属性.
        				maxid = updao.getFileMaxId()+1;
        				curFile.setArtid(artId);
        				curFile.setAuthor(userid);
        				curFile.setFilename(userid+"_"+maxid+"."+curFile.getFiletype());
        				curFile.setPath(ymd+"/"+curFile.getFilename());
        				curFile.setPtime(ftime);
        				// 将上传文件写到系统目录下
        				if(UpLoadBiz.writeFile((FormFile)formlist.get(i),filePath+"/"+curFile.getFilename())){
        					UpLoadBiz.uploadDao(curFile);	// 将上传文件信息写到数据库
        					if(!isDataOk)
        						isDataOk = true;
        				}
        			}
        		}
        	}
        }
        
        if(isDataOk){
			 uplist.clear();
			 uplist = null;
        }
		 if(isUploadOk){
            errors.add("admin.upload.fail", new ActionMessage("admin.upload.fail"));
            saveErrors(request, errors);
        }
        
		attlist = getArtFiles(artId,yhlog);
    	leaveNum = allowsize-attlist.size();
    	request.setAttribute("allattmts", attlist);
		request.setAttribute("curNum",leaveNum);
		request.setAttribute("action",action);
		request.setAttribute("artId",artId);
		return actionmapping.findForward("Display");
						
	}
	public List getArtFiles(int artId,Logger yhlog){
		UpFileDao filedao = UpFileDaoIm.getInstance();
		List attlist = null; // 用来获得该文章的所有附件列表
		try {
			attlist = filedao.getArtFiles(artId);
		}
		catch (DBConnException e) {
			yhlog.warn("When get a article's attachments,throw an Exception!The article id is:"+artId+".");
		}
		if(attlist==null){
			attlist = new ArrayList();
		}
		return attlist;
		
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -