现在的位置: 首页 > 综合 > 正文

struts2实现上传下载(单文件上传与多文件上传的比较)

2014年03月14日 ⁄ 综合 ⁄ 共 3533字 ⁄ 字号 评论关闭

 struts2没有提供自己的请求解析器,也就是说,struts2不会自己去处理multipart/form-data的请求,它需要调用其他请求解析器,将HTTP请求中的表单域解析出来,但struts2在原有的上传解析器上作了进一步封装,更进一步简化了文件上传,Struts2的struts.properties配置文件中,配置struts2的上传文件解析器struts.multipart.parser=jakarta(srtuts2默认),也可以设置为常用的cos,pell等。

       struts2实现上传下载所必须的2个jar包:commons-fileupload-xxx.jarcommons-io-xxx.jar

一、文件上传

  1.  单文件上传

(1)单文件上传表单视图:

  1. <body>  
  2.  <form action="/test/upload.action" enctype="multipart/form-data" method="post">  
  3.      <input name="uploadfile" type="file">  
  4.      <input type="submit" value="上传">  
  5.  </form>  
  6. </body>  

注意:表单必须设置enctype="multipart/form-data"method="post"属性,否则上传会出错。

2. 单文件上传Action类

  1. /** 
  2.  * 文件上传类 
  3.  * @author Ye 
  4.  * 
  5.  */    
  6.  public class UploadAction extends ActionSupport{  
  7.   
  8.         <!--获取上传文件,名称必须和表单file控件名相同-->       
  9.          private File uploadfile;  
  10.   
  11.          <!--获取上传文件名,命名格式:表单file控件名+FileName(固定)-->      
  12.          private String uploadfileFileName;  
  13.   
  14.          //获取上传文件类型,命名格式:表单file控件名+ContentType(固定)   
  15.          private String uploadfileContentType;       
  16.   
  17.          ...//省略属性的getter、setter方法  
  18.         
  19.          //方法一:使用FileUtils的copyFile来实现文件上传  
  20.          public String upload() throws IOException  
  21.          {  
  22.                  //设置上传文件目录    
  23.                  String realpath = ServletActionContext.getServletContext().getRealPath("/image");  
  24.   
  25.                  //判断上传文件是否为空      
  26.                  if(uploadfile!=null)  
  27.                  {  
  28.                          //设置目标文件(根据 parent 路径名字符串和 child 路径名字符串创建一个新 File 实例)  
  29.                          File savefile = new File(realpath,uploadfileFileName);  
  30.   
  31.                          // 判断上传目录是否存在            
  32.                          if(!savefile.getParentFile().exists())  
  33.                                savefile.getParentFile().mkdirs();  
  34.   
  35.                          //把文件uploadfile 拷贝到 savefile 里,FileUtils类需要commons-io-x.x.x.jar包支持  
  36.                          FileUtils.copyFile(uploadfile,savefile);  
  37.   
  38.                          //设置request对象值       
  39.                          ActionContext.getContext().put("message""上传成功!");  
  40.                  }  
  41.                  return "success";  
  42.           }  
  43.   
  44.           //方法二:使用文件流来实现文件上传  
  45.           public String upload() throws IOException  
  46.           {  
  47.                 FileOutputStream fos = new FileOutputStream("D:\\"+uploadfileFileName);  
  48.           
  49.                 FileInputStream fis = new FileInputStream(uploadfile);  
  50.           
  51.                 byte[] buffer = new byte[1024];  
  52.           
  53.                 int len = 0;  
  54.           
  55.                 while((len=fis.read(buffer))>0)  
  56.                 {  
  57.                    fos.write(buffer,0,len);  
  58.                 }  
  59.                 return "success";  
  60.            }  
  61.   
  62.  }  

注意:记得Action类要继承ActionSupport

3.  配置struts.xml文件

  1. <package name="hello" namespace="/test" extends="struts-default">  
  2.     <action name="upload" class="action.UploadAction" method="upload">    
  3.      
  4.         <!-- 在struts.xml文件中使用constant元素指定在全局的struts.properties文件中自定义出错信息,value值为*.properties类型的文件名 -->  
  5.         <constant name="struts.custom.i18n.resources" value="struts"></constant>   
  6.   
  7.         <!-- 配置fileUpload的拦截器 -->  
  8.         <interceptor-ref name="fileUpload">  
  9.    
  10.            <!-- 配置允许上传的文件类型 -->  
  11.            <param name="allowedTypes">image/bmp,image/gif,image/jpg</param>      
  12.         
  13.            <!--配置允许上传文件的扩展名,如果有多个用","隔开 -->      
  14.            <param name="allowedExtensions">txt,excel,ppt</param>    
  15.                 
  16.            <!-- 配置允许上传的文件大小,最大为20k -->  
  17.            <param name="maximumSize">20480</param>  
  18.   

抱歉!评论已关闭.