• Struts2实现文件上传和下载,多文件上传,限制文件大小,限制文件类型


    文件上传使用的包:commons-upload-xx.jar

                                    commons-io-xx.jar

    一.实现文件上传:

    1.在表单空间中添加enctype=”multipart/form-data”

    <form action="upload.action" method="post" enctype="multipart/form-data">
               <input type="file" name="upload" />
               <input type="submit" value="上传" />
          </form>

    2.创建Action:

               ①在action中添加属性

                    private File xnamex;    //指定文件名,文件名必须与文件域的name属性名一样

                    private String xnamexUploadContentType;  //文件类型

                    private String xnamexFileName;   //文件名称

                ②重写execute或自定义方法:

                    获取远程站点真正路径,指定文件保存路径:

                      String savePath= ServletActionContext.getServletContext.get(“/upload”);

                    通过IO操作将文件写入保存文件夹

      1: package com.action;
    
      2: import java.io.File;
    
      3: import org.apache.commons.io.IOUtils;
    
      4: import org.apache.struts2.ServletActionContext;
    
      5: import com.opensymphony.xwork2.ActionSupport;
    
      6: 
    
      7: public class UploadAction extends ActionSupport {
    
      8:   private File upload;//上传的文件
    
      9:   private String uploadContentType;//文件类型
    
     10:   private String uploadFileName;//文件名
    
     11:   
    
     12:   public File getUpload() {
    
     13:     return upload;
    
     14:   }
    
     15:   public void setUpload(File upload) {
    
     16:     this.upload = upload;
    
     17:   }
    
     18:   public String getUploadContentType() {
    
     19:     return uploadContentType;
    
     20:   }
    
     21:   public void setUploadContentType(String uploadContentType) {
    
     22:     this.uploadContentType = uploadContentType;
    
     23:   }
    
     24:   public String getUploadFileName() {
    
     25:     return uploadFileName;
    
     26:   }
    
     27:   public void setUploadFileName(String uploadFileName) {
    
     28:     this.uploadFileName = uploadFileName;
    
     29:   }
    
     30:   @Override
    
     31:   public String execute() throws Exception {
    
     32:     String savaPath = ServletActionContext.getServletContext()
    
     33:         .getRealPath("/upload");
    
     34:     FileOutputStream fos=null;
    
     35:     FileInputStream fis=null;
    
     38:       fis=new FileInputStream(upload[i]);
    
     39:       fos=new FileOutputStream(savaPath+"\"+uploadFileName[i]);
    
     44:       IOUtils.copy(fis, fos);
    
     45:       fis.close();
    
     46:       fos.flush();
    
     47:       fos.close();
    
     49:     return "success";
    
     50:   }
    
     52: }
    

    3.在struts2.xml文件中配置:

    <action name="upload" class="com.action.UploadAction">
    
          <result name="success">/uploadfile.jsp</result>
    
    </action>

     

     

    二.限制文件大小:

    方法一:在struts2.xml文件中配置常量参数:

    <constant name=”struts.multipart.maxSize” value=”50000” />

    value值  为  byte

    方法二:在action中引用系统定义的fileUpload拦截器,

                  配置拦截器参数:<param name=”maximumSize”>50000</param>

    三.限制文件类型:

     

    在action中引用系统定义的fileUpload拦截器,

                  配置拦截器参数:

    <param name=”allowedTypes”>application/msword,·····</param>

    tomcat中,在conf/web.xml中查找type

    image

     

     

    四.实现多个文件同时上传

    ①,将Action中属性改为数组:

                   private File[]  xnamex;    //指定文件名

                    private String[]  xnamexUploadContentType;  //文件类型

                    private String[]  xnamexFileName;   //文件名称

    ②,在方法中添加数组遍历

     

    五.在struts.xml中配置中配置保存路径

    ①.在action标签中添加参数配置

    <param name=”path”>upload</param>   //指定文件夹名

    ②.修改文件上传Action:

    添加属性:private String path;  //与配置文件中参数的name属性一致

    更改保存路径:savaPath=ServletActionContext.getActionContext.get(“/”+path);

     

     

     

     

    实现文件下载 (点击)

    ============================================================

    OVER

  • 相关阅读:
    JChartFree创建饼形图
    JFreeChart设置点的颜色
    JChartFree使用散点图
    JChartFree常用数据集
    博客园安家了
    在Android中什么是异步执行;
    XmlPullParserException
    构造器的执行顺序
    Sqlite之contentProvider
    使用java获取歌曲的属性
  • 原文地址:https://www.cnblogs.com/next-open/p/3461949.html
Copyright © 2020-2023  润新知