---
title: "007 - 文件上传"
author: "backcrazy"
date: "2009-12-30T18:07:18Z"
tags: []
categories: ["企业架构"]
slug: "007"
draft: false
lastmod: "2009-12-30T20:57:03Z"
id: 559979
url: "https://ray-lin.iteye.com/blog/559979"
---

# 1.文件上传

 //FileUpLoadAction .java

public class FileUpLoadAction {

 private File image;                //取得文件  
 private String imageFileName;      //取得文件名  
 private String imageContentType;   //取得文件类型  
   
   
 private File\[] images;                //取得多个文件  
 private String\[] imagesFileName;      //取得多个文件名  
 private String\[] imagesContentType;   //取得多个文件类型  
   
 public String executeSimple() throws Exception{  //上传单个文件  
  

   String realpath = ServletActionContext.getServletContext().getRealPath("/images");  
   System.out.println(realpath);  
   if(image!=null){

     File savefile = new File(new File(realpath), imageFileName);  
     if(!savefile.getParentFile().exists()) savefile.getParentFile().mkdirs();  
     FileUtils.copyFile(image, savefile);  
     ActionContext.getContext().put("message", "上传成功");

   }  
   return "success";

 }  
   
 public String executeMutiple() throws Exception{        //上传多个文件  
  

   String realpath = ServletActionContext.getServletContext().getRealPath("/images");  
   System.out.println(realpath);  
   if(image!=null){  
         File savedir = new File(realpath);  
         if(!savedir.exists()){  
              savedir.mkdirs();  
         }  
       
     for(int i = 0 ; i&lt;image.length ; i++){      
            File savefile = new File(savedir, imageFileName\[i]);  
            FileUtils.copyFile(image\[i], savefile);  
     }  
     ActionContext.getContext().put("message", "上传成功");  
   }  
   return "success";

 }  
   
 .....//getters and setters  
 

}

//上传单个文件表单  
    &lt;form action="upload\_executeSimple" enctype="multipart/form-data" method="post"&gt;  
            文件:&lt;input type="file" name="image"&gt;&lt;br/&gt;  
           &lt;input type="submit" value="上传"/&gt;  
    &lt;/form&gt;  
     
     
//上传多个文件表单,   注意enctype="multipart/form-data"  
    &lt;form action="upload\_executeMutiple" enctype="multipart/form-data" method="post"&gt;  
           文件1:&lt;input type="file" name="images"&gt;&lt;br/&gt;  
           文件2:&lt;input type="file" name="images"&gt;&lt;br/&gt;  
           文件3:&lt;input type="file" name="images"&gt;&lt;br/&gt;  
           &lt;input type="submit" value="上传"/&gt;  
    &lt;/form&gt;

 

//struts.xml  
&lt;struts&gt;  
     &lt;constant name="struts.enable.DynamicMethodInvocation" value="true"/&gt;  
     &lt;constant name="struts.multipart.maxSize" value="10701096"/&gt;  
       
     &lt;package name="upload" namespace="/" extends="struts-default"&gt;  
            &lt;action name="upload\_\*" class="FileUpLoadAction" method="{1}"&gt;  
                   &lt;result name="success"&gt;/WEB-INF/page/message.jsp&lt;/result&gt;  
            &lt;/action&gt;  
      &lt;/package&gt;  
&lt;/struts&gt;

 

# 2. 文件下载

 

 

public class DownloadAction extends ActionSupport {

        private String fileName;   
       
        public void setFileName() {         

               String tempFileName = ServletActionContext.getRequest().getParameter("name");  
               try {   
                      // 对tempFileName进行UTF-8解码

                      // 这里使用request.setCharacterEncoding解码无效.    
                      // 只有解码了getDownloadFile()方法才能在下载目录下正确找到请求的文件    
                     tempFileName = new String(tempFileName .getBytes("ISO-8859-1"), "UTF-8");   
                } catch (Exception e) {   
                      e.printStackTrace();  
                }  
                 this.fileName = tempFileName ;   
         }

 

 

     //此方法对应的是struts.xml文件中的：    
     // &lt;param name="contentDisposition"&gt;attachment;filename="${fileName}"&lt;/param&gt;    
     // 这个属性设置的是下载工具下载文件时显示的文件名，    
     // 要想正确的显示中文文件名，我们需要对fileName再次编码    
     //否则中文名文件将出现乱码，或无法下载的情况     

     public String getFileName() throws UnsupportedEncodingException {

            fileName=new String(fileName.getBytes(),"ISO-8859-1"); 

            return fileName;  
     }

    

 

     //此方法对应的是struts.xml文件中的：    
     // &lt;param name="inputName"&gt;downloadFile&lt;/param&gt;    
     //返回下载文件的流，可以参看struts2的源码    
      public InputStream getDownloadFile() {

             this.setFileName();   
             return ServletActionContext.getServletContext().getResourceAsStream("/upload/" + fileName);  
    }

   

    public String execute() throws Exception {  
            return SUCCESS;  
    }  
}

 

 

 

struts.xml  其中的EL表达式，是要注意的

        &lt;action name="download" class="com.test.action.DownloadAction"&gt;  
            &lt;result name="success" type="stream"&gt;  
                &lt;param name="contentDisposition"&gt;attachment;filename="${fileName}"&lt;/param&gt;   
                &lt;param name="inputName"&gt;downloadFile&lt;/param&gt; 

                &lt;!--对应DownloadAction.getDownloadFile()--&gt;  
            &lt;/result&gt;  
        &lt;/action&gt;