struts2实现文件的上传下载

在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>
<!-- 
package 分组管理action
name:package名称  不可以重复
namespace:与action  name结合确立访问路径
        默认值:""
extends:继承自某个package   沿用java中的概念  
abstract:用来表示这个package是否是被继承的
 -->
<!-- 
 action:与处理的action类建立映射关系
 name:action名称与action  name结合确立访问路径,在同一个package下不能重复
 class :配置要访问的action的全路径名  默认ActionSupport
 method:指定访问指定action的方法  默认访问的就是execute
 -->

 <!-- 

    result:结果集处理标签
    name:访问的方法的返回值 默认:success
    type:指定跳转方式   5 

    跳转的路径:/index.jsp
   -->
    <package name="default" namespace="/" extends="struts-default"  >
        <action name="myAction2_*" class="com.baidu.action.MyAction2" method="{1}" >
            <result name="upload" >/index.jsp</result>
            <result name="down" type="stream">
                <param name="contentType">${contentType}</param> <!-- 调用当前action中的getContentType()方法 -->
                <param name="contentDisposition">attachment;filename=${downloadFileName}</param>
                <param name="inputStream">${inputStream}</param><!-- 调用当前action中的getInputStream()方法 -->
            </result>
        </action>                       
    </package>    
</struts>

创建一个action类,实现上传下载功能

public class MyAction2 extends ActionSupport{

    private File ff;//获取到默认的上传文件路径
    private String ffFileName;//获取到上传的文件名
    private String ffContentType;//获取到上传的文件类型
    private String df;//定义df变量表示要下载文件名
    public File getFf() {
        return ff;
    }
    public void setFf(File ff) {
        this.ff = ff;
    }
    public String getFfFileName() {
        return ffFileName;
    }
    public void setFfFileName(String ffFileName) {
        this.ffFileName = ffFileName;
    }
    public String getFfContentType() {
        return ffContentType;
    }
    public void setFfContentType(String ffContentType) {
        this.ffContentType = ffContentType;
    }

    public String getDf() {
        return df;
    }
    public void setDf(String df) {
        this.df = df;
    }
    //上传文件
    public String upload() throws IOException {
        //String realPath = ServletActionContext.getServletContext().getRealPath("upload");
        FileUtils.copyFile(ff, new File("f:"+File.separator+"upload"+File.separator+ffFileName));
        return "upload";
    }
    //下面是下载文件的方法
    public String down() {      
        return "down";
    }
    public String getContentType() {
        return ServletActionContext.getServletContext().getMimeType(df);
    }
    public String getDownloadFileName() {
        return df;
    }
    public InputStream getInputStream() throws FileNotFoundException {
        //String realPath = ServletActionContext.getServletContext().getRealPath("upload");
        FileInputStream fis = new FileInputStream(new File("f:"+File.separator+"upload"+File.separator+df));
        return fis;
    }
}

猜你喜欢

转载自blog.csdn.net/chenbingbing111/article/details/81060425