Struts2实现文件的上传功能
来源:互联网 发布:高频交易软件 编辑:程序博客网 时间:2024/05/17 02:29
需求来源于生活:一个业余程序员的突发奇想
以前做班委的时候,经常要向班里收集各种各样的资料。既要对已交同学的文件作出统一规范化的命名,又要及时提醒那些迟交的同学该交资料了。这是一项重复性的工作。
老师以前教育我们机器是最适合做重复性工作的,于是便有了这样一个小小的需求:为自己班级实现一个统一的提交作业的平台。
说干就干。
这个小项目的核心就在于上传下载。同学们上传,班委(管理员)统一打包下载资料,并且提供邮件提醒服务。那么这里,我们先来探讨一下文件的上传功能。
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文件夹中,如下图:
如何在有限的生命里,活的更快乐
阅读全文
0 0
- struts2文件上传功能的实现
- Struts2实现文件的上传功能
- struts2实现文件上传功能
- 使用Extjs Struts2 实现文件的上传功能
- 关于使用struts2实现基本的文件上传功能
- struts2 实现简单的文件上传、下载功能
- 基于struts2实现文件上传功能
- Struts2+FileFlashUpload实现多文件上传功能
- struts2中实现文件上传功能
- Struts2中实现多文件上传功能
- Struts2-上传/下载文件功能实现
- struts2实现文件上传与下载功能
- Struts2.x中实现文件上传功能
- struts2文件上传功能
- struts2实现文件的上传
- struts2实现文件的上传
- struts2文件上传的实现
- struts2实现文件的上传
- TensorFlow使用的一个小案例
- Linux SPI框架(中)
- canvas.save() canvas.restore() 作用
- bootstrap-datepicker.css
- Learning Python Part II 之 表达式语句(Expression)
- Struts2实现文件的上传功能
- Java鼠标监听器
- Java IO 用缓冲区 复制文本
- angularjs的内置服务
- leetcode 455. Assign Cookies(C语言,堆排序)28
- java 删除文件夹下所有文件(java delete folder)
- Eclipse 中 Git 各分支间代码同步
- 移动端click时间、touch事件、tap事件详解
- python3.x与python2.x的区别汇总