日期:2014-05-20  浏览次数:20701 次

struts 2 如何动态生成一个html文件并下载?
是动态生成,并下载,不是先生成到一个目录再去下载,然后再删除生成的文件。
以下是我的下载文件action;
Java code

public class TrainingAttendHistoryExportRegisterAction extends DefaultActionSupport {

    private static final long serialVersionUID = 6929180904579405855L;
    private String path;
    
    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public InputStream getInputStream() throws Exception {
        // 根据配置文件inputPath属性值返回一个文件输入流
        OutputStreamWriter fw = new OutputStreamWriter(new FileOutputStream("trainingAttendHistoryExportRegister.html"),"big5");
        fw.write("<html></html>"+"\r\n");
        fw.close();
        InputStream is=new FileInputStream("pa");
//        File reDocFile = new File(ServletActionContext.getServletContext().getRealPath("/")+"test.html");
        
        return ServletActionContext.getServletContext().getResourceAsStream(path);
    }

    public String execute() throws Exception {
        return SUCCESS;
    }


}



------解决方案--------------------
可以参考这个地方试试:
http://jeromexie.iteye.com/blog/139491