struts2文件上传

来源:互联网 发布:python turtle安装 编辑:程序博客网 时间:2024/06/03 21:24

文件上传主要三步:
1、struts.xml文件配置

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"><struts><constant name="struts.enable.DynamicMethodInvocation" value="true" /><constant name="struts.devMode" value="true" /><!-- 设置视图主题 --><constant name="struts.ui.theme" value="simple" /><!-- 解决乱码 --><constant name="struts.i18n.encoding" value="utf-8" /><!-- 设置允许上传文件最大字节数 默认值 2097152(2M)--><constant name="struts.multipart.maxSize" value="85265425412" /><!-- 设置上传文件的临时文件夹 --><constant name="struts.multipart.saveDir" value="d:/temp" /><package name="default" extends="struts-default"><interceptors><interceptor name="fileUpload" class="org.apache.struts2.interceptor.FileUploadInterceptor"/></interceptors>                       <action name="UploadAction" class="com.zrgk.action.UploadAction"><param name="savePath">/upload</param><!-- 定义处理结果字符串和资源之间的映射关系 --><result name="success">success.jsp</result><result name="error">error.jsp</result><interceptor-ref name="fileUpload"><!--  这里设置允许上传的类型--><param name="allowedTypes">image/bmp,text/plain,image/jpeg,application/vnd.ms-excel</param><param name="maximumSize">829392929</param></interceptor-ref><interceptor-ref name = "defaultStack" ></interceptor-ref></action></package></struts> 

2、jsp页面
注意:设置form表单 enctype="multipart/form-data",另外要注意文件file的name属性,因为接下来第三部要用到!!!

<form action="UploadAction" method="post" enctype="multipart/form-data">    USERNAME:<input type="text" name= "username" /><br/>    文件:<input type="file" name= "file1" /><br/>    <input type="submit" value="上传"></form>


3、UploadAction.java控制类
记得提前在WebRoot下创建upload文件夹
注意点:类中的private成员变量与上面jsp中的文件的name值相关!!!
其中固定格式写法(注意他们的名字):
private File file1 ; //具体上传文件的 引用 , 指向临时目录中的临时文件  
        private String file1FileName ;  // 上传文件的名字 ,FileName 固定的写法  
                private String file1ContentType ; //上传文件的类型, ContentType 固定的写法 

package com.zrgk.action;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import java.io.OutputStream;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;public class UploadAction extends ActionSupport {private String usename ;          private File file1 ; //具体上传文件的 引用 , 指向临时目录中的临时文件          private String file1FileName ;  // 上传文件的名字 ,FileName 固定的写法          private String file1ContentType ; //上传文件的类型, ContentType 固定的写法 private String savePath;  //文件的上传后存储的地址@Overridepublic String execute() throws Exception {//获得要下载到的路径String path = ServletActionContext.getServletContext().getRealPath("/upload");//输入流InputStream is = new FileInputStream(getFile1());OutputStream os = new FileOutputStream(new File(path+"//"+getFile1FileName()));int len = 0;byte[] b = new byte[1024];while((len=is.read(b))!=-1){os.write(b, 0, len);}os.close();is.close();return SUCCESS;}public String getUsename() {return usename;}public void setUsename(String usename) {this.usename = usename;}public File getFile1() {return file1;}public void setFile1(File file1) {this.file1 = file1;}public String getFile1FileName() {return file1FileName;}public void setFile1FileName(String file1FileName) {this.file1FileName = file1FileName;}public String getFile1ContentType() {return file1ContentType;}public void setFile1ContentType(String file1ContentType) {this.file1ContentType = file1ContentType;}public String getSavePath() {return savePath;}public void setSavePath(String savePath) {this.savePath = savePath;}}



原创粉丝点击