Struts2实现文件的上传功能

需求来源于生活:一个业余程序员的突发奇想

以前做班委的时候,经常要向班里收集各种各样的资料。既要对已交同学的文件作出统一规范化的命名,又要及时提醒那些迟交的同学该交资料了。这是一项重复性的工作。
老师以前教育我们机器是最适合做重复性工作的,于是便有了这样一个小小的需求:为自己班级实现一个统一的提交作业的平台。
说干就干。
这个小项目的核心就在于上传下载。同学们上传,班委(管理员)统一打包下载资料,并且提供邮件提醒服务。那么这里,我们先来探讨一下文件的上传功能。

Struts2的常规配置:Intellij开发工具

完成了struts2的常规配置之后,开始实现对应的上传action(实现action接口)。
action部分字段如下,并实现set,get方法用于获取前端传给后台的值。

    private File upload;//前端页面file字段设置的name名称
    private String uploadFileName;//FileName固定写法,文件名称
    private String uploadContentType;//ContentType固定写法,文件类型

多文件上传方案则将这里的字段变为list集合:

    private List<File> upload;
    private List<String> uploadFileName;
    private List<String> uploadContentType;

单文件上传execute方法:本质还是采用了io的方式重新对文件进行读写

   String path = ServletActionContext.getServletContext().getRealPath("/WEB-INF/upload")+File.separator+username;//获取储存路径
        File serverFileDir = new File(path);
        if(!serverFileDir.exists()){
            serverFileDir.mkdir(); //创建该目录
        }
        File serverFile = new File(path,uploadFileName);
        FileInputStream in = new FileInputStream(upload);
        FileOutputStream out = new FileOutputStream(serverFile);
        byte[]b = new byte[1024];
        int len = 0;
        while((len=in.read(b))>0){
            out.write(b,0,len);
        }
        out.close();
        return SUCCESS;

多文件上传方案则是在单文件上传的基础上对list进行遍历:

 String path = ServletActionContext.getServletContext().getRealPath("/WEB-INF/upload")+File.separator+username;
        File serverFileDir = new File(path);
        if(!serverFileDir.exists()){
            serverFileDir.mkdir(); //创建该目录
        }
        for(int i = 0 ; i < upload.size() ; i++ ){
            File serverFile = new File(path,uploadFileName.get(i));
            FileInputStream in = new FileInputStream(upload.get(i));
            FileOutputStream out = new FileOutputStream(serverFile);
            byte[]b = new byte[1024];
            int len = 0;
            while((len=in.read(b))>0){
                out.write(b,0,len);
            }
            out.close();
        }
        return SUCCESS;

完成了action的编码同时也不要忘记对action进行路径配置:

        <action name="fileUpload" class="action.FileUploadAction" method="execute">
            <result type="redirect" name="success">success.jsp</result>
        </action>

        <action name="mulFileUpload" class="action.MulFileUploadAction" method="execute">
            <result type="redirect" name="success">success.jsp</result>
        </action>

有时候我们需要对文件上传大小做出限制,默认为2M,这里我们在Struts的根元素下设置为100M

  <constant name="struts.multipart.maxSize" value="104857600"/><!--设置文件上传最大值100m-->

前端界面:进行多文件同时上传时要保证file类型的name属性名称要相同,否则后台将不会接收数据为list集合!

  <div>使用struts2实现文件上传下载</div>
    文件上传最大值为100M
   <form action="mulFileUpload.action" method="post" enctype="multipart/form-data">
        
      username: <input type="text" name="username"><br>
      file1: <input type="file" name="upload"><br>
      file2: <input type="file" name="upload"><br>

      <input type="submit" value="submit">
  </form>

上传的文件在本地调试的时候回出现在out文件夹中,如下图:
这里写图片描述

如何在有限的生命里,活的更快乐

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值