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

📄 uploadaction.java

📁 Java开发的权限管理的例子
💻 JAVA
字号:
package org.artemis.manager.struts.action;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.upload.FormFile;
import org.artemis.manager.struts.form.FileDownloadForm;
import org.artemis.manager.struts.form.FileUploadForm;
import com.gsta.eshore.framework.struts.BaseAction;
import com.gsta.eshore.framework.util.EnvironmentConfig;
import com.gsta.eshore.framework.util.FileConfig;
import com.gsta.eshore.framework.util.FileProcessor;
import com.gsta.eshore.framework.util.download.DownloadFileUtil;

public class UploadAction extends BaseAction {
	private String filePath=null;
	private static Logger logger = Logger.getLogger(UploadAction.class.getName());
	/** 
	 * Method execute
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward executeAction(
		ActionMapping mapping,
		ActionForm form,
		HttpServletRequest request,
		HttpServletResponse response) {
		ActionForward myforward = null;
		String myaction = request.getParameter("myaction");
		//String myaction = mapping.getParameter();
		logger.info(" [UploadAction] action: " + myaction);
		if (isCancelled(request)) {
			if (logger.isInfoEnabled()) {
				logger.info(" [UploadAction] " + mapping.getAttribute()
						+ " - action was cancelled");
			}
			return mapping.findForward("cancel");
		}

		if ("".equalsIgnoreCase(myaction)) {
			myforward = mapping.findForward("error");
		    } else if ("UPLOAD".equalsIgnoreCase(myaction)) {
			myforward = upload(mapping, form, request, response);
		    } else if ("download".equalsIgnoreCase(myaction)) {
				myforward = download(mapping, form, request, response);
		    } else if ("delete".equalsIgnoreCase(myaction)) {
				myforward = delete(mapping, form, request, response);
		    }
		else {
			return handleException(mapping, request, ActionMessages.GLOBAL_MESSAGE, "error.File.Exception");		
		}

		return myforward;
	}
	
	private ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
		FileDownloadForm loadform=(FileDownloadForm)form;
		String filename=null;
		try {
			filename = java.net.URLDecoder.decode(loadform.getFilename(), "UTF-8");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		logger.debug("filename = "+filename);
        try {
        	FileProcessor.deleteFile(filename);
        	return mapping.findForward("success");
		} catch (Exception e) {
			e.printStackTrace();
			return handleException(mapping, request, ActionMessages.GLOBAL_MESSAGE, "error.File.Exception");
		}
	}

	private ActionForward download(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
		FileDownloadForm loadform=(FileDownloadForm)form;
		String filename=null;
		try {
			filename = java.net.URLDecoder.decode(loadform.getFilename(), "UTF-8");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		logger.debug("filename = "+filename);
        if(filename!=null){
    		try {
    			if (filePath==null){
	        	  filePath = EnvironmentConfig.getInstance().getPropertyValue(
			                 FileConfig.ConfigFile, "upLoadPath");

	        	 } 
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
        }
        try {
            DownloadFileUtil.getInstance().downloadFile(response,
            		filePath + filename, filename, true);
            return null;
		} catch (Exception e) {
			e.printStackTrace();
			return handleException(mapping, request, ActionMessages.GLOBAL_MESSAGE, "error.File.Exception");
		}
  
	}

	private ActionForward upload(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
		if (form instanceof FileUploadForm) { 
			FileUploadForm uploadform=(FileUploadForm)form;
			String subPath=null;
			String filename=null;
	  
		        String encoding = request.getCharacterEncoding();
		        logger.debug("encoding = "+encoding);
		        if ((encoding != null) && (encoding.equalsIgnoreCase("utf-8")))
		        {
		            response.setContentType("text/html; charset=GBK");//如果没有指定编码,编码格式为gb2312
		        }
		        FormFile formFile = uploadform.getTheFile();//取得上传的文件
		        Date date=new Date();
		        
//		         int thisYear = date.getYear() + 1900;//得到年
//		         int thisMonth = date.getMonth() + 1;//得到月
//		         subPath=String.valueOf(thisYear)+String.valueOf(thisMonth);
//		         logger.debug("subPath = "+subPath);
		        if((formFile.getFileSize()!=0)){
		         try {
		        	 if (filePath==null){
			        	  filePath = EnvironmentConfig.getInstance().getPropertyValue(
					                 FileConfig.ConfigFile, "upLoadPath");
//				        filePath = request.getRealPath("");//取当前系统路径
//				        logger.debug("filePath = "+filePath);
//				        filePath=filePath+"/upload/";
//			        	  WebConfig webConfig = WebConfig.getInstance();
//			              String rootpath = webConfig.getProperty(
//			                  "rootpath");
			        	 // String filePath=filePath+subPath+formFile.getFileName();
		        	 }

		          logger.debug("filePath : "+filePath+formFile.getFileName());	
		          
		          
		          //filename = FileProcessor.uploadFile(formFile,filePath+subPath,false,true);
		          filename = FileProcessor.uploadFile(formFile,filePath,false,true);
		          if(filename!=null){
		        	  //删除原先的文件,如果相等则不删除
//		        	  if(!bidbaseinfo.getUploadfile().equals(filename)){
//		        		  int i=FileProcessor.deleteFile(filePath+subPath+File.separator+bidbaseinfo.getUploadfile());
//		        	  } 
		          
		          }
		        }catch(Exception e){
		          System.err.print(e);
		        	}
		        }
		        else{//没有选择文件则是删除文件			          
			          try {
			        	  logger.debug(" 选择文件则是删除文件 : "+filePath+filename);
			        	  int i=FileProcessor.deleteFile(filePath+filename);
			        	  logger.debug("删除结果为 : "+i);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
		        }
		        //request.setAttribute("dat",file.getFileName());
//				request.setAttribute("AlertMsg","更新文件成功!");
//				request.setAttribute("CloseWin","N");
//				request.setAttribute("SuccessType","delete");
//				request.setAttribute("SuccessTarget","/member/wszb/wzcg/bidBaseInfoFindList.do");
		        return mapping.findForward("success");
		    }
		        return handleException(mapping, request, ActionMessages.GLOBAL_MESSAGE, "error.File.Exception");
		  }

}

⌨️ 快捷键说明

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