struts2-上传下载文件

版权声明: https://blog.csdn.net/qq_34866380/article/details/79796152

struts2的文件上传下载很简单,因为有拦截器的帮助,可以省略大部分代码,只需要通过写入路径就可实现。

上传文件
1、表单的文件上传中,method必须为post,还要加个enctype=”multipart/form-data”

<form action="test/upload" method="post" enctype="multipart/form-data">
  img:<input type="file" name="img">
  img:<input type="file" name="img">
  <input type="submit" value="upload">
</form>

2、uploadAction.java

public class uploadAction {
    private File[] img;
    private String[] imgFileName;//文件名,必须这样命名,否则无法识别

    public File[] getImg() {
        return img;
    }

    public void setImg(File[] img) {
        this.img = img;
    }

    public String[] getImgFileName() {
        return imgFileName;
    }

    public void setImgFileName(String[] imgFileName) {
        this.imgFileName = imgFileName;
    }

    public String execute(){
        if(img!=null){//防止img为空时直接在浏览器抛出异常
           for(int i = 0;i<img.length;i++){
               try {
//                String path = "f:/images";
                  String path = ServletActionContext.getServletContext().getRealPath("/images");//获取服务器下应用根目录
                  File destFile = new File(path  ,imgFileName[i]);
                  System.out.println(path);
                  FileUtils.copyFile(img[i], destFile );//向目路径写入文件

                   } catch (IOException e) {
                                     e.printStackTrace();
                                           }
                   }
                return "success";
             }
        return "fail";
    }
}

3、注册action

<action name="upload" class="com.test3.uploadAction">
            <result>/welcome.jsp</result>
            <result name="fail">/fail.jsp</result>
           </action>

修改上传文件最大值:
默认常量为struts.multipart.maxSize,修改值:(一次上传的所以文件总共的大小)

<constant name="struts.multipart.maxSize" value="20971520" /><!--20M-->

限制上传文件扩展名
action下添加:

<interceptor-ref name="defaultStack">
    <param name="fileUpload.allowedExtensions">jpg,img</param>
</interceptor-ref>

文件下载
1、 下载请求:

<a href="test/download">image</a>

2、download.java

public class download {

    private InputStream is;//文件输入流,用于指定服务器向客户端所提供下载的文件资源
    private String fileName;

    public InputStream getIs() {
        return is;
    }

    public void setIs(InputStream is) {
        this.is = is;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String execute(){
        fileName = "2.ico";
        is = ServletActionContext.getServletContext().getResourceAsStream("/images/"+fileName);//从应用根目录读取文件
        //取出后修改成自定义的名字,多从数据库读取
        fileName = "image.ico";

        return "success";
    }
}

3、注册action:

<action name="download" class="com.test3.download">
             <result type="stream">
                  <param name="contentDisposition">attachment;fileName=${fileName}</param>
                  <param name="inputName">is</param><!-- inputName可省略,其后面的默认值为inputname-->
              </result>
           </action>

猜你喜欢

转载自blog.csdn.net/qq_34866380/article/details/79796152