BaseAction

来源:互联网 发布:淘宝店刷排名靠前 编辑:程序博客网 时间:2024/05/18 01:46
BaseAction 
Java代码  收藏代码
  1. package com.easyjob.cnhuike.actions;  
  2. import javax.servlet.ServletContext;  
  3. import javax.servlet.http.HttpServletRequest;  
  4. import javax.servlet.http.HttpServletResponse;  
  5. import javax.servlet.http.HttpSession;  
  6. import org.apache.struts2.ServletActionContext;  
  7. import org.springframework.context.ApplicationContext;  
  8. import java.io.*;  
  9. import java.util.ArrayList;  
  10. import java.util.List;  
  11. import com.easyjob.cnhuike.util.modelConstant.LoginConstant;  
  12. import com.framework.util.DateUtil;  
  13. import com.opensymphony.xwork2.ActionSupport;  
  14.   
  15. public class BaseAction extends ActionSupport {  
  16.   
  17.     public static final String  LIST="list";  
  18.     public static final String NEXT="next";  
  19.     String inputAction;  
  20.     String nextAction;  
  21.       
  22.       
  23.     public HttpServletRequest getRequest(){  
  24.         return ServletActionContext.getRequest();  
  25.     }  
  26.       
  27.     public HttpServletResponse getResponse(){  
  28.         return ServletActionContext.getResponse();  
  29.     }  
  30.       
  31.     public HttpSession getSession(){  
  32.         return getRequest().getSession();  
  33.     }  
  34.       
  35.     public ServletContext getServletContext(){  
  36.         return ServletActionContext.getServletContext();  
  37.     }  
  38.       
  39.     public String getRealyPath(String path){  
  40.         return getServletContext().getRealPath(path);  
  41.     }  
  42.           
  43.       
  44.     public String getInputAction() {  
  45.         return inputAction;  
  46.     }  
  47.     public void setInputAction(String inputAction) {  
  48.         this.inputAction = inputAction;  
  49.     }  
  50.     public String getNextAction() {  
  51.         return nextAction;  
  52.     }  
  53.     public void setNextAction(String nextAction) {  
  54.         this.nextAction = nextAction;  
  55.     }  
  56. }  


上传用的BaseAction 
Java代码  收藏代码
  1. package com.easyjob.cnhuike.actions;  
  2.     
  3.   
  4. import java.io.BufferedInputStream;  
  5. import java.io.BufferedOutputStream;  
  6. import java.io.File;  
  7. import java.io.FileInputStream;  
  8. import java.io.FileOutputStream;  
  9. import java.io.IOException;  
  10. import java.io.InputStream;  
  11. import java.io.OutputStream;  
  12. import java.util.ArrayList;  
  13. import java.util.List;  
  14.   
  15. import com.framework.util.DateUtil;  
  16.     
  17. public class BaseUploadFileAction extends BaseAction {     
  18.     public static final int BUFFER_SIZE=16*1024;     
  19.     // 用File数组来封装多个上传文件域对象     
  20.     public File[] upload;     
  21.     // 用String数组来封装多个上传文件名     
  22.     public String[] uploadFileName;     
  23.     // 用String数组来封装多个上传文件类型     
  24.     public String[] uploadContentType;     
  25.     // 保存文件的目录路径(通过依赖注入)     
  26.     public String savePath="";     
  27.       
  28.     public BaseUploadFileAction(){  
  29.         savePath="\\uploadFile";      
  30.     }  
  31.   
  32.   //文件复制  
  33.     public  boolean  copy(File src, File dst,int BUFFER_SIZE) {     
  34.         boolean result=false;     
  35.         InputStream in = null;     
  36.         OutputStream out = null;     
  37.         try {     
  38.             in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);     
  39.             out = new BufferedOutputStream(new FileOutputStream(dst),     
  40.                     BUFFER_SIZE);     
  41.             byte[] buffer = new byte[BUFFER_SIZE];     
  42.             int len = 0;     
  43.             while ((len = in.read(buffer)) > 0) {     
  44.                 out.write(buffer, 0, len);     
  45.             }     
  46.             result=true;     
  47.         } catch (Exception e) {     
  48.             e.printStackTrace();     
  49.             result=false;     
  50.         } finally {     
  51.             if (null != in) {     
  52.                 try {     
  53.                     in.close();     
  54.                 } catch (IOException e) {     
  55.                     e.printStackTrace();     
  56.                 }     
  57.             }     
  58.             if (null != out) {     
  59.                 try {     
  60.                     out.close();     
  61.                 } catch (IOException e) {     
  62.                     e.printStackTrace();     
  63.                 }     
  64.             }     
  65.         }     
  66.         return result;     
  67.     }     
  68.       
  69.       
  70.     //上传文件方法  
  71.       public List<String> uploadFile(String path,boolean isFullPath) throws IOException{  
  72.        List<String> successFileList=new ArrayList<String>();     
  73.           // 处理每个要上传的文件     
  74.           for (int i = 0; i < upload.length; i++) {     
  75.               // 根据服务器的文件保存地址和原文件名创建目录文件全路径    
  76.               String srcFilesInfo=uploadFileName[i].toString();  
  77.               //取得文件的后缀  
  78.               String  FileExtensions=srcFilesInfo.substring(srcFilesInfo.lastIndexOf("."),srcFilesInfo.length());  
  79.               String fileName=DateUtil.getNowDate(null)+"-"+DateUtil.getHour()+DateUtil.getMinute()+DateUtil.getSecond()+i+FileExtensions;  
  80.               String dstPath = getRealyPath(path) + "\\"+fileName;    
  81.               File dstFile = new File(dstPath);     
  82.               if(copy(upload[i], dstFile,BUFFER_SIZE)){     
  83.                   if(isFullPath){  
  84.                     successFileList.add(path+fileName);    
  85.                   }else{  
  86.                     successFileList.add(fileName);  
  87.                   }  
  88.               }    
  89.               if(successFileList.size()<1){  
  90.                 throw new IOException();  
  91.               }  
  92.           }   
  93.           return successFileList;  
  94.       }  
  95.         
  96.       public List<String> uploadFile(String path) throws IOException{  
  97.           return uploadFile(path,false);  
  98.       }  
  99.         
  100.       //取得路径中的文件名  
  101.      public List<String> getFileNames(String path){  
  102.           File file=new File(getRealyPath(path));  
  103.           List<String> resultList=new ArrayList<String>();  
  104.           File[] files= file.listFiles();  
  105.           for (File tempFile : files) {  
  106.             if(tempFile.isFile()){  
  107.                 resultList.add(tempFile.getName());   
  108.             }  
  109.         }  
  110.           return resultList;  
  111.       }  
  112.         
  113.      //建立文件夹  
  114.       public void createFold(String path){  
  115.             try{  
  116.                 path=getRealyPath(path);  
  117.                 File folder = new File(path);   
  118.                 if(!folder.exists()){  
  119.                     folder.mkdirs();  
  120.                 }  
  121.                   
  122.             }catch (Exception e) {  
  123.                 e.printStackTrace();  
  124.             }  
  125.         }  
  126.         
  127.       /* 
  128.        *geter and seter  
  129.        * 
  130.        * */  
  131.     public File[] getUpload() {  
  132.         return upload;  
  133.     }  
  134.   
  135.     public void setUpload(File[] upload) {  
  136.         this.upload = upload;  
  137.     }  
  138.   
  139.     public String[] getUploadFileName() {  
  140.         return uploadFileName;  
  141.     }  
  142.   
  143.     public void setUploadFileName(String[] uploadFileName) {  
  144.         this.uploadFileName = uploadFileName;  
  145.     }  
  146.   
  147.     public String[] getUploadContentType() {  
  148.         return uploadContentType;  
  149.     }  
  150.   
  151.     public void setUploadContentType(String[] uploadContentType) {  
  152.         this.uploadContentType = uploadContentType;  
  153.     }  
  154.   
  155.     public String getSavePath() {  
  156.         return savePath;  
  157.     }  
  158.   
  159.     public void setSavePath(String savePath) {  
  160.         this.savePath = savePath;  
  161.     }  
  162.   
  163.     public static int getBUFFER_SIZE() {  
  164.         return BUFFER_SIZE;  
  165.     }     
  166. }    


上传时所用到的拦截配置 
Java代码  收藏代码
  1. <interceptors>  
  2.         <interceptor-stack name="albumUpload">  
  3.                  <interceptor-ref name="fileUpload">     
  4.                       <!-- 配置允许上传的文件类型,多个用","分隔 -->     
  5.                       <param name="allowedTypes">     
  6.                      image/bmp,image/png,image/gif,image/jpeg,image/jpg,image/x-png, image/pjpeg     
  7.                       </param>     
  8.                       <!-- 配置允许上传的文件大小,单位字节 -->     
  9.                       <param name="maximumSize">10240000</param>     
  10.               </interceptor-ref>     
  11.               <interceptor-ref name="defaultStack" />    
  12.         </interceptor-stack>  
  13.     </interceptors>  
  14.   
  15.     <action name="album!*" class="com.easyjob.cnhuike.actions.AlbumAction"   method="{1}">  
  16.         <interceptor-ref name="albumUpload" />    
  17.         <result>/album/album_{1}.jsp</result>  
  18.         <result name="list" type="redirect-action">album!{1}List</result>  
  19.         <result name="error">/album/album_error.jsp</result>  
  20.         <result name="input">/album/album_error.jsp</result>  
  21.         <result name="next" type="redirect-action">  
  22.             <param name="actionName">${nextAction}</param>  
  23.             <param name="namespace">/album</param>  
  24.         </result>  
  25.     </action>
0 0
原创粉丝点击