用户管理的设计--6.文件上传

需求

完成用户文件上传

  1:将上传的文件统一放置到upload的文件夹下

  2:将每天上传的文件,使用日期格式的文件夹分开,将每个业务的模块放置统一文件夹下

  3:上传的文件名要指定唯一,可以使用UUID的方式,也可以使用日期作为文件名

  4:封装一个文件上传的方法,该方法可以支持多文件的上传,即支持各种格式文件的上传

  5:保存路径path的时候,使用相对路径进行保存,这样便于项目的可移植性


实现步骤

1.在util包下封装一个方法

public class FileUtils {
    
    /**  
    * @Name: fileUploadReturnPath
    * @Description: 文件上传,返回panth路径
    * @Parameters: file:上传文件
    *                fileName:上传文件名
    *                model:模块名称
    * @Return: String:返回上传路径
    * 1:完成文件上传的要求
          1:将上传的文件统一放置到upload的文件夹下
          2:将每天上传的文件,使用日期格式的文件夹分开,将每个业务的模块放置统一文件夹下
          3:上传的文件名要指定唯一,可以使用UUID的方式,也可以使用日期作为文件名
          4:封装一个文件上传的方法,该方法可以支持多文件的上传,即支持各种格式文件的上传
          5:保存路径path的时候,使用相对路径进行保存,这样便于项目的可移植性
    */
    public static String fileUploadReturnPath(File file, String fileName, String model) {
        //1.获取upload文件夹路径
        String basePath = ServletActionContext.getServletContext().getRealPath("/upload");
        //2.获取日期格式的文件夹(格式yyyy/MM/dd/)
        String datePath=DateUtils.dateToStringByFile(new Date());
        //3.全路径格式(例如:upload201778用户管理)
        String filePath=basePath+datePath+model;
        //4.判断该文件夹是否存在,若不存在,创建
        File dateFile = new File(filePath);
        if(!dateFile.exists()){
            dateFile.mkdirs();
        }
        //5.指定对应文件名
        //获取文件名后缀
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        //设置文件名为uuid
        String uuidFileName = UUID.randomUUID().toString()+suffix;
        //目标文件
        File destFile = new File(dateFile,uuidFileName);
        //上传文件
        file.renameTo(destFile);
        return "/upload"+datePath+model+"/"+uuidFileName;
    }

}

 

2.jsp页面的表单要求

  当导入struts2的jar包时,struts2会默认支持使用fileupload工具上传文件

设置表单属性: enctype=multipart/form-data          表单类型

        method=post           提交方式

       name=uploads                   文件域名称

<form name="Form1"
action="${pageContext.request.contextPath }/system/elecUserAction_save.do"
method="post" enctype="multipart/form-data">
    <s:file name="uploads" id="uploads" cssStyle="450px;"></s:file> *
</form>

 

3.VO对象中添加非持久化javabean属性

    //上传文件
    private File[] uploads;
    //上传文件名,该变量的定义必须是上传表单file字段name属性值+FileName
    private String[] uploadsFileName;
    //上传文件类型,该变量的定义必须是上传表单file字段name属性值+ContentType
    private String[] uploadsContentType;
  public File[] getUploads() {
        return uploads;
    }
    public void setUploads(File[] uploads) {
        this.uploads = uploads;
    }
    public String[] getUploadsFileName() {
        return uploadsFileName;
    }
    public void setUploadsFileName(String[] uploadFileName) {
        this.uploadsFileName = uploadFileName;
    }
    public String[] getUploadsContentType() {
        return uploadsContentType;
    }
    public void setUploadsContentType(String[] uploadContentType) {
        this.uploadsContentType = uploadContentType;
    }

4.Service中添加方法

  在用户service实现类中添加附件保存的方法saveUserFile(),并在上篇讲到的saveUser()方法中进行调用

/**
     * 遍历多个附件,组织附件的PO对象,完成文件上传,保存用户的附件(多条数据),建立附件表和用户表的关联关系
     * @param elecUser
     */
    private void saveUserFiles(ElecUser elecUser) {
        Date date = new Date();
        //获取上传的文件
        File[] files = elecUser.getUploads();
        //获取文件名
        String[] fileNames = elecUser.getUploadsFileName();
        //获取文件类型
        String[] contentType = elecUser.getUploadsContentType();
        //遍历
        if(files!=null&&files.length>0){
            for(int i=0;i<files.length;i++){
                //组织PO对象
                ElecUserFile elecUserFile = new ElecUserFile();
                elecUserFile.setFileName(fileNames[i]);
                elecUserFile.setProgressTime(date);
                //将文件上传,同时返回路径path
                String fileURL=FileUtils.fileUploadReturnPath(files[i],fileNames[i],"用户管理");
                elecUserFile.setFileURL(fileURL);
                elecUserFile.setElecUser(elecUser);//重要:与用户建立关联关系,如果不建立,外键为null
                //保存附件
                elecUserFileDao.save(elecUserFile);
            }
        }
    }

5.struts2上传文件大小限制

  struts2默认可以上传的文件最大限制是2M,如果上传文件大小超过2M,控制台错误如下:

org.apache.commons.fileupload.FileUploadBase$SizeLimitExceededException: the request was rejected because its size (77817949) exceeds the configured maximum (2097152)

  该异常信息是common-fileupload组件输出的,而非是Struts2框架

解决办法:在struts.xml中设置上传组件的文件大小限制

<!-- 设置最大上传的大小是80M -->
<constant name="struts.multipart.maxSize" value="83886080"></constant>
原文地址:https://www.cnblogs.com/zhstudy/p/7159413.html