需求
完成用户文件上传
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 文件域名称
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></files.length>
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></constant>
The above is the detailed content of File upload function in user management. For more information, please follow other related articles on the PHP Chinese website!

Using POI library in Java to add borders to Excel files Many Java developers are using Apache...

Efficient processing of batch interface requests: Using CompletableFuture to ensure that concurrent calls to third-party interfaces can significantly improve efficiency when processing large amounts of data. �...

In JavaWeb applications, the feasibility of implementing entity-class caching in Dao layer When developing JavaWeb applications, performance optimization has always been the focus of developers. Either...

The current status of motorcycle and motorcycle systems and ecological development of motorcycle systems, as an important bridge connecting knights and vehicles, has developed rapidly in recent years. Many car friends...

When using MyBatis-Plus or tk.mybatis...

How to query personnel data through natural language processing? In modern data processing, how to efficiently query personnel data is a common and important requirement. ...

In processing next-auth generated JWT...

In IntelliJ...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version
God-level code editing software (SublimeText3)