您的位置:首页 > 编程语言 > Java开发

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>
</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。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  struts 文件上传 jsp