Struts2下使用Common-FileUpload实现文件上传
来源:互联网 发布:java与android的区别 编辑:程序博客网 时间:2024/05/21 12:43
web应用下上传文件需要为表单设置enctype="multipart/form-data"属性,表单将以二进制编码的方式提交请求,然后由解析器进行解析,struts2不提供解析器,但可以和common-fileupload、COS很好的结合。struts2默认使用Jakarta的common-fileupload文件上传框架(在struts2-core.jar中default.properties中可见struts.multipart.parser=jakarta)。
下面展示一个使用common-fileupload实现文件上传的示例。
1、从http://commons.apache.org/上下载commons-fileupload-1.2.1.jar和commons-io-1.3.2.jar,然后放到WEB-INF/lib文件夹下。
2、编写代码
upload-input.jsp
upload.jsp
UploadAction.java
struts.xml
效果如下:
上传页面:
上传成功:
项目结构图:
可见,成功在webapp/test/下上传了upload.png文件。
在struts.xml中配置fileUpload拦截器来限制上传文件的类型和大小时需要注意:
1、通过allowedTypes参数限制文件类型时允许类型定义要全,比如png类型的图片就有image/png和image/x-png两种类型,我开始只定义了image/png,发现png类型的图片上传不了,后来看了http://exceljava.javaeye.com/blog/210249发现我上传的图片类型是image/x-png,添加以后上传成功,所以遇到上传类型错误最好在本机输入看看是什么类型,然后在allowedTypes参数中添加。
2、通过maximunSize参数限制上传的文件大小时,如果没有改变常量“struts.multipart.maxSize”的值,那么maximunSize就不要设置超过2M,否则程序直接抛出异常,拦截器不会拦截,因为fileUpload拦截器默认使用“struts.multipart.maxSize”的值作为上传文件的最大值。当然,可以在struts.xml或者struts.properties中更改“struts.multipart.maxSize”的值,然后确保maximunSize参数的值不大于“struts.multipart.maxSize”的值。
3、对于拦截器的报错信息可以在国际化资源文件中增加
struts.messages.error.file.too.large=your error message
struts.messages.error.content.type.not.allowed=your error message
- Struts2下使用Common-FileUpload实现文件上传
- Struts2下使用Common-FileUpload实现文件上传
- Struts2中使用Common-FileUpload实现文件上传
- Struts2中使用Common-FileUpload实现文件上传(二)
- Struts2中使用Common-FileUpload实现文件上传(二)
- Struts2文件上传 Common-FileUpload
- 使用 common-fileupload 实现文件上传
- 使用common-fileupload实现文件的上传
- 使用common-fileupload框架实现文件上传
- 使用common-fileupload实现文件上传功能
- 使用common-fileupload实现文件上传
- 使用common-fileupload实现文件的上传
- 使用common-fileUpload实现文件上传
- 使用common-fileupload实现文件的上传
- 使用common-fileupload实现文件的上传
- Struts2框架的文件上传--common-fileupload
- Struts2文件上传(一):Common-FileUpload
- struts2上传文件(Common-FileUpload)
- jquery 验证框架的一点实战总结
- Java中的堆内存与栈内存分配浅析
- 玻璃的革命
- Kerberos support in PostgreSQL on Windows
- 编写高效的java线程安全类
- Struts2下使用Common-FileUpload实现文件上传
- spring security
- 有利于版本管理的代码格式
- 借光转载mac的特殊按键
- MySQL中varchar最大长度是多少?
- oarcle lag()分析函数
- 做山寨,更要有原则和自己的特色
- eclipse跳出“password required”
- struts2中外键字段的迭代方式