struts上传文件
2016-01-06 21:23
435 查看
struts2中上传文件功能实现:
1、jsp页面配置
<s:form action="studentSubmitTitleAction" method="post" enctype="multipart/form-data">
<s:textfield name="title" label="课程设计题目"/></br>
<s:file name="document" label="上传课程设计文档" /></br>
<s:submit value="上传" />
</s:form>2、struts.xml文件对应的配置
<action name="studentSubmitTitleAction" class="com.wxb.action.studentSubmitTitleAction">
<!-- 配置fileUpload的拦截器 -->
<interceptor-ref name="fileUpload">
<span style="white-space:pre"> </span><!-- 配置允许上传的文件类型 -->
<span style="white-space:pre"> </span><param name="allowedTypes">image/png,image/gif,image/jpeg</param>
<span style="white-space:pre"> </span><!-- 配置允许上传的文件大小 -->
<span style="white-space:pre"> </span><param name="maximumSize">20000</param>
</interceptor-ref>
<!-- 配置系统默认的拦截器 -->
<interceptor-ref name="defaultStack" />
<result name="success">student/SubmitSubject.jsp</result>
package com.CourseProject.action;
import java.io.File;
/**
* 学生提交课程设计题目和课程设计文档对应的处理类
*
* @author 王校兵
* @version 1.0, 2016-1-5
* */
public class StudentSubmitTitleAction extends BaseAction {
private File <span style="color:#ff0000;">document</span>;
private String <span style="color:#ff0000;">documentFileName</span>;
private String <span style="color:#ff0000;">documentContentType</span>;
private String title;
/**
* 用户提交课程设计题目和文档跳转
*
* @param null
* @return 跳转页面对应的代码
* */
public String execute() {
System.out.println("123123123123");
System.out.println(title);
System.out.println(document);
System.out.println(documentFileName);
System.out.println(documentContentType);
return "success";
}
public String getDocumentFileName() {
return documentFileName;
}
public void setDocumentFileName(String documentFileName) {
this.documentFileName = documentFileName;
}
public String getDocumentContentType() {
return documentContentType;
}
public void setDocumentContentType(String documentContentType) {
this.documentContentType = documentContentType;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public File getDocument() {
return document;
}
public void setDocument(File document) {
this.document = document;
}
}
4、导入commons-io-1.4.jar和commons-fileupload-1.2.1
两个jar包
注意:在action中标红的属性,如果要想取到上传文件的类型或者是文件名需要在action中指定他的属性名的前缀为前台jsp页面中输入的名字。
例如上述例子jsp表单中的名称为document则它对应的文件类型名或文件名应为documentContentType或者documentFileName。
1、jsp页面配置
<s:form action="studentSubmitTitleAction" method="post" enctype="multipart/form-data">
<s:textfield name="title" label="课程设计题目"/></br>
<s:file name="document" label="上传课程设计文档" /></br>
<s:submit value="上传" />
</s:form>2、struts.xml文件对应的配置
<action name="studentSubmitTitleAction" class="com.wxb.action.studentSubmitTitleAction">
<!-- 配置fileUpload的拦截器 -->
<interceptor-ref name="fileUpload">
<span style="white-space:pre"> </span><!-- 配置允许上传的文件类型 -->
<span style="white-space:pre"> </span><param name="allowedTypes">image/png,image/gif,image/jpeg</param>
<span style="white-space:pre"> </span><!-- 配置允许上传的文件大小 -->
<span style="white-space:pre"> </span><param name="maximumSize">20000</param>
</interceptor-ref>
<!-- 配置系统默认的拦截器 -->
<interceptor-ref name="defaultStack" />
<result name="success">student/SubmitSubject.jsp</result>
</action>3、action文件编写
package com.CourseProject.action;
import java.io.File;
/**
* 学生提交课程设计题目和课程设计文档对应的处理类
*
* @author 王校兵
* @version 1.0, 2016-1-5
* */
public class StudentSubmitTitleAction extends BaseAction {
private File <span style="color:#ff0000;">document</span>;
private String <span style="color:#ff0000;">documentFileName</span>;
private String <span style="color:#ff0000;">documentContentType</span>;
private String title;
/**
* 用户提交课程设计题目和文档跳转
*
* @param null
* @return 跳转页面对应的代码
* */
public String execute() {
System.out.println("123123123123");
System.out.println(title);
System.out.println(document);
System.out.println(documentFileName);
System.out.println(documentContentType);
return "success";
}
public String getDocumentFileName() {
return documentFileName;
}
public void setDocumentFileName(String documentFileName) {
this.documentFileName = documentFileName;
}
public String getDocumentContentType() {
return documentContentType;
}
public void setDocumentContentType(String documentContentType) {
this.documentContentType = documentContentType;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public File getDocument() {
return document;
}
public void setDocument(File document) {
this.document = document;
}
}
4、导入commons-io-1.4.jar和commons-fileupload-1.2.1
两个jar包
注意:在action中标红的属性,如果要想取到上传文件的类型或者是文件名需要在action中指定他的属性名的前缀为前台jsp页面中输入的名字。
例如上述例子jsp表单中的名称为document则它对应的文件类型名或文件名应为documentContentType或者documentFileName。
相关文章推荐
- JSP/PHP基于Ajax的分页功能实现
- JQuery+Strusts1.x无刷新登录
- 开发阶段Jetty运行Jsp报错且响应空白
- jsp简单实现页面之间共享信息的方法
- Apache Web让JSP“动”起来
- ASP实现文件上传的方法
- PHP文件上传问题汇总(文件大小检测、大文件上传处理)
- .net文件上传时实现通过文件头确认文件类型的方法
- asp.net 文件上传与刷新与asp.net页面与iframe之间的数据传输
- ASP的chr(0)文件上传漏洞原理和解决方法介绍
- asp.net上传文件到数据库的解决方案
- javascript实现校验文件上传控件实例
- ajax 异步上传带进度条视频并提取缩略图
- jQuery实现文件上传进度条特效
- 分享20多个很棒的jQuery 文件上传插件或教程
- jquery插件ajaxupload实现文件上传操作
- JSP学习经验小结分享
- JSP 多条SQL语句同时执行的方法