Struts2文件下载

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <form action="fileDownload.action" method="post">
        <input type="submit" value="Download"></input>
    </form>
</body>
</html>
package com.oa.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import com.opensymphony.xwork2.ActionSupport;

/**
 * 
 * @author: zhao
 * @time: 2016年5月5日
 * 
 * @description:Struts2文件下载
 */
@SuppressWarnings("all")
public class FileDownloadAction extends ActionSupport{
    private static final long serialVersionUID = 1L;
    private InputStream inputStream;
    private String filename;
    
    @Override
    public String execute() throws Exception {
        File file=new File("E:\\imooc.txt");
        filename=file.getName();
        try {
            inputStream=new FileInputStream(file);
            System.out.println(inputStream.available());
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return SUCCESS;
    }
    
    public InputStream getInputStream() {
        return inputStream;
    }
    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }
    public String getFilename() {
        return filename;
    }
    public void setFilename(String filename) {
        this.filename = filename;
    }
    

}
<?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 name="default" namespace="/" extends="struts-default">
        <action name="fileDownload" class="com.oa.action.FileDownloadAction">
            <result type="stream">
                <param name="inputName">inputStream</param>
                <param name="contentDisposition">attachment;filename="${filename}"</param>
                <param name="contentType">application/octet-stream</param>
                <param name="bufferSize">1024</param>
            </result>
        </action>

    </package>

</struts>

 

posted @ 2016-05-05 21:13  假寐的我  阅读(167)  评论(0编辑  收藏  举报