<action name="test" class="com.TestAction" method="importToFile"> <result name="success" type="stream"> <param name="contentType">unknown</param> <param name="inputName">downloadFile</param> <!--下载文件的文件流名称,在action中需要给它一个get方法返回下载文件的流--> <param name="contentDisposition">attachment;filename="${downloadFileName}"</param> <param name="bufferSize">4096</param> </result> </action>
public InputStream getDownloadFile() throws InvalidResultSetAccessException, IOException, SQLException { String path = request.getSession().getServletContext().getRealPath("/temp"); File dir = new File(path); if (!dir.exists()) dir.mkdir(); parent_code = request.getParameter("parent_code"); String fileName = "shopbarcode_"+parent_code+".txt"; File file = new File(dir,fileName); FileWriter writer = new FileWriter(file); DB db = new DB(); writeToFile(db, writer, parent_code); writer.close(); return ServletActionContext.getServletContext().getResourceAsStream("/temp/"+fileName); } public String importToFile() { setDownloadFileName("test.txt"); return SUCCESS; }