26.struts2_文件的上传
来源:互联网 发布:如何将相同的数据合并 编辑:程序博客网 时间:2024/05/22 03:14
一、准备工作
表单准备:将 HTML表单 enctype属性设置为 multipart/form-data
表单需要使用 method为post的方式
添加<input type=“file”>字段
二、struts2对文件上传的支持
框架中,FileUpload拦截器和Jakarta Commons FileUpload组件可以完成文件的上传。
步骤:
1.在对应Action建立三个属性
private File [fileFieldName]; //这个fileFieldName对应页面上<s:file name=""/> 的name属性
private String [fileFieldName]ContentType;
private String [fileFieldName]FileName;
UploadAction.java
public class UploadAction extends ActionSupport {/** * */private static final long serialVersionUID = 1L;private File ppt;private String pptContentType; private String pptFileName;private String pptDesc;public String getPptDesc() {return pptDesc;}public void setPptDesc(String pptDesc) {this.pptDesc = pptDesc;}public File getPpt() {return ppt;}public void setPpt(File ppt) {this.ppt = ppt;}public String getPptContentType() {return pptContentType;}public void setPptContentType(String pptContentType) {this.pptContentType = pptContentType;}public String getPptFileName() {return pptFileName;}public void setPptFileName(String pptFileName) {this.pptFileName = pptFileName;}@Overridepublic String execute() throws Exception {System.out.println(ppt);System.out.println(pptContentType);System.out.println(pptFileName);System.out.println(pptDesc);//使用IO流进行上传ServletContext sac=ServletActionContext.getServletContext();String dir=sac.getRealPath("/files/"+pptFileName);System.out.println(dir);FileOutputStream out =new FileOutputStream(dir);FileInputStream in = new FileInputStream(ppt);byte[] buffer=new byte[1024];int len=0;while((len=in.read(buffer))!=-1){out.write(buffer,0,len);}out.close();in.close();return SUCCESS;}}
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><s:form action="testUpload" method="post" enctype="multipart/form-data"><s:file name="ppt" label="PPTFile"></s:file><s:textfield name="pptDesc" label="PPTDesc" ></s:textfield><s:submit></s:submit></s:form></body></html>
三、配置FileUploadInterceptor拦截器参数,限制上传,并给出提示
- maximumSize (optional) - the maximum size (in bytes) that the interceptor will allow a file reference to be set on the action. Note, this isnot related to the various properties found in struts.properties. Default to approximately 2MB.//单个文件上传最大值
- allowedTypes (optional) - a comma separated list of content types (ie: text/html) that the interceptor will allow a file reference to be set on the action. If none is specified allow all types to be uploaded.//允许的上传文件类型,多个使用【,】分隔
- allowedExtensions (optional) - a comma separated list of file extensions (ie: .html) that the interceptor will allow a file reference to be set on the action. If none is specified allow all extensions to be uploaded.//允许上传文件的扩展名,逗号分隔
可以在国际化资源中定义如下消息
- struts.messages.error.uploading - 文件上传出错
- struts.messages.error.file.too.large - 超过大小
- struts.messages.error.content.type.not.allowed - 文件内容类型不合法
- struts.messages.error.file.extension.not.allowed - 文件扩展名不合法
添加错误提示后的struts.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"><struts><!-- 配置国际化资源文件 --><constant name="struts.custom.i18n.resources" value="i18n"></constant><package name="default" namespace="/" extends="struts-default"><!-- 配置拦截器栈 --><interceptors><interceptor-stack name="hcx"><interceptor-ref name="defaultStack"><param name="fileUpload.maximumSize">2000</param><param name="fileUpload.allowedTypes">text/plain,text/xml</param><param name="fileUpload.allowedExtensions">txt,html</param></interceptor-ref></interceptor-stack></interceptors><!-- 使用拦截器栈 --><default-interceptor-ref name="hcx"/> <action name="testUpload" class="com.hcx.app.UploadAction"><result name="success">/success.jsp</result><result name="input">/upload.jsp</result></action></package></struts>
但是目前提示消息不完善,具体可以参考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}
注意: 在struts2的defaul.properties中有对上传文件总的大小限制,为2M。struts.multipart.maxSize=2097152。如果修改可以按照上述方式。
阅读全文
0 0
- 26.struts2_文件的上传
- 42. Struts2_文件的上传
- Struts2_文件上传_Demo
- Struts2_单文件上传
- Struts2_多文件上传
- struts2_文件上传
- Struts2_文件上传
- Struts2_事物管理&文件上传
- Struts2_事物管理&文件上传
- 27.struts2_文件的下载
- 11级_Java_曹建波 03.02 Struts2_事物管理&文件上传
- JavaWeb开发知识总结(七)-(struts2_文件上传_Ajax)
- Struts2_文件上传关于限制文件名、类型及大小
- struts2_文件上传下载
- Struts2_文件下载
- struts2_文件下载
- Struts2_文件下载
- 26.Struts2_类型转换错误消息的显示和定制
- Python scikit-learn机器学习工具包学习笔记:cross_validation模块
- css浏览器兼容问题
- buntu15.04设置自动锁屏时间
- POJ 1325 Machine Schedule 笔记
- struts2入门实例
- 26.struts2_文件的上传
- Git 命令
- chrome主页被篡改毒霸网址大全
- git版本控制常用指令
- php中0、null和""
- cat /proc/meminfo
- 软件开发系统分析规格说明
- 正则背诵部分
- Java学习之线程中一些方法,生命周期,线程间通信