基于struts框架的文件上传

来源:互联网 发布:淘宝北卡大学是正品吗 编辑:程序博客网 时间:2024/04/29 23:17

struts的基本配置这里就不再说

一、jsp文件中的form表单写法

<form action="FileUpLoad_file.action" method="post"enctype="multipart/form-data" >
    <input type="file" name="fileUpLoad">
    <input type="submit" value="提交"/>
    </form>

二、相应的jar包及struts.xml文件配置要点

1.commons-fileupload-1.2.2.jar

2.commons-io-2.0.1.jar

3.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.action.extension" value="action,html,do,,"></constant>
<constant name="struts.configuration.xml.reload" value="true"></constant>
<constant name="struts.multipart.maxSize" value="1024000"></constant>//设置文件上传的大小
<package name="default" namespace="/" extends="struts-default">
<action name="*_*" class="cn.struts.Action.{1}Action"
method="{2}">
<result>/WEB-INF/jsp/{2}.jsp</result>
</action>
</package>
</struts>

三、在你的Action类中写上相应的接收数据(二进制数据)和相应的get、set方法(重要)

package cn.struts.Action;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
public class FileUpLoadAction {
private File fileUpLoad;
private String fileUpLoadFileName;
private String fileUpLoadContentType;
//开始显示方法
public String fileUpLoad(){
return "success";
}
//操作文件
public String file(){
try {
FileUtils.copyFile(fileUpLoad, new File("D://test/"+fileUpLoadFileName));
} catch (IOException e) {
e.printStackTrace();
}
return "success";
}
public File getFileUpLoad() {
return fileUpLoad;
}
public void setFileUpLoad(File fileUpLoad) {
this.fileUpLoad = fileUpLoad;
}
public String getFileUpLoadFileName() {
return fileUpLoadFileName;
}
public void setFileUpLoadFileName(String fileUpLoadFileName) {
this.fileUpLoadFileName = fileUpLoadFileName;
}
public String getFileUpLoadContentType() {
return fileUpLoadContentType;
}
public void setFileUpLoadContentType(String fileUpLoadContentType) {
this.fileUpLoadContentType = fileUpLoadContentType;
}

}

0 0
原创粉丝点击