一.文件上传:
1.表单需要注意3 点
2.Struts2 的文件上传实际使用的是Commons FileUpload组件,所需导入
3.Struts2 进行文件上传所需使用的FileUpload拦截器
4. 基本文件上传:直接在Action 中定义如下3个属性,并提供get(),set()方法
//文件对应的File对象
private File [fileFileName];
//文件类型
private String [fileFileName]ContentType;
//文件名
private String [fileFileName]FileName;
5.使用IO流进行文件的上传即可。
6.若要传多个文件:
上述的三个属性可以改为List类型,多个name属性值要一致;
<!-- 多个描述时,若只用(pptDesc)不用下标形式如(ptDesc[0]),回显时就会有问题-->
7.能限制上传文件的类型,大小,格式等等吗。
可通过配置FileUploadInterceptor 拦截器来进行限制
(1)maximumSize (optional) - 默认最大值为2M
(2)allowedTypes (optional) - 允许的上传文件的类型,多个使用,分割
(3)allowedExtensions (optional) - 允许上传的文件扩展名,多个使用,分割
8.错误消息定制。可在国际资源文件中定义如下消息:
struts.messages.error.uploading - 文件上传类型错误消息
struts.messages.error.file.too.large - 文件大小超过消息
struts.messages.error.content.type.not.allowed - 文件内容类型不合法消息
struts.messages.error.file.extension.not.allowed - 文件扩展名不合法消息
放到国际资源配置文件(.properties)里
但上述消息不完善。
可以参考 struts2-core-2.3.16.3.jar 下的 org.apache.struts2 的struts-messages.properties文件:
struts.messages.error.uploading=Error uploading: {0}
struts.messages.error.file.too.large=The file is to large to be uploaded: {0} "{1}" "{2}" {3}
struts.messages.error.content.type.not.allowed=Content-Type not allowed: {0} "{1}" "{2}" {3}
struts.messages.error.file.extension.not.allowed=File extension not allowed: {0} "{1}" "{2}" {3}
放到国际资源配置文件(.properties)里
二、文件的下载
Struts2 提供了 type="stream" 的result 进行下载即可 。参考 文档 stream-result.html
1.表单需要注意3 点
2.Struts2 的文件上传实际使用的是Commons FileUpload组件,所需导入
3.Struts2 进行文件上传所需使用的FileUpload拦截器
4. 基本文件上传:直接在Action 中定义如下3个属性,并提供get(),set()方法
//文件对应的File对象
private File [fileFileName];
//文件类型
private String [fileFileName]ContentType;
//文件名
private String [fileFileName]FileName;
5.使用IO流进行文件的上传即可。
6.若要传多个文件:
上述的三个属性可以改为List类型,多个name属性值要一致;
<!-- 多个描述时,若只用(pptDesc)不用下标形式如(ptDesc[0]),回显时就会有问题-->
7.能限制上传文件的类型,大小,格式等等吗。
可通过配置FileUploadInterceptor 拦截器来进行限制
(1)maximumSize (optional) - 默认最大值为2M
(2)allowedTypes (optional) - 允许的上传文件的类型,多个使用,分割
(3)allowedExtensions (optional) - 允许上传的文件扩展名,多个使用,分割
8.错误消息定制。可在国际资源文件中定义如下消息:
struts.messages.error.uploading - 文件上传类型错误消息
struts.messages.error.file.too.large - 文件大小超过消息
struts.messages.error.content.type.not.allowed - 文件内容类型不合法消息
struts.messages.error.file.extension.not.allowed - 文件扩展名不合法消息
放到国际资源配置文件(.properties)里
但上述消息不完善。
可以参考 struts2-core-2.3.16.3.jar 下的 org.apache.struts2 的struts-messages.properties文件:
struts.messages.error.uploading=Error uploading: {0}
struts.messages.error.file.too.large=The file is to large to be uploaded: {0} "{1}" "{2}" {3}
struts.messages.error.content.type.not.allowed=Content-Type not allowed: {0} "{1}" "{2}" {3}
struts.messages.error.file.extension.not.allowed=File extension not allowed: {0} "{1}" "{2}" {3}
放到国际资源配置文件(.properties)里
二、文件的下载
Struts2 提供了 type="stream" 的result 进行下载即可 。参考 文档 stream-result.html