您好,欢迎访问一九零五行业门户网

用户管理中的文件上传功能

需求完成用户文件上传
  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.全路径格式(例如:upload\2017\7\8\用户管理)string filepath=basepath+datepath+model;//4.判断该文件夹是否存在,若不存在,创建file datefile = new file(filepath);if(!datefile.exists()){             datefile.mkdirs();         }//5.指定对应文件名//获取文件名后缀string suffix = filename.substring(filename.lastindexof(.));//设置文件名为uuidstring 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="width:450px;"></s:file> *</form>
3.vo对象中添加非持久化javabean属性    //上传文件private file[] uploads;//上传文件名,该变量的定义必须是上传表单file字段name属性值+filenameprivate string[] uploadsfilename;//上传文件类型,该变量的定义必须是上传表单file字段name属性值+contenttypeprivate 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);//将文件上传,同时返回路径pathstring 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>
以上就是用户管理中的文件上传功能的详细内容。
其它类似信息

推荐信息