struts导出txt文件

1、struts.xml 

<action name="downLoad" class="org.wll.prj.action.HelloAction" method="exportFile">
        </action>

2、export.jsp

 <script type="text/javascript">
    function daochu()
    {
   window.document.forms[0].submit();
    }
  </script>
  <body>
  <form action="<%=path%>/downLoad.action?method=exportFile" method="post">
      <input id="wll" name="wll" value="helloworld!"></input>
  </form>
    This is my JSP page. <br>
   
    <button onclick="daochu();">点击</button>
  </body>

3、导出文件action

public void exportFile() throws Exception {
  request = ServletActionContext.getRequest();
  response = ServletActionContext.getResponse();
  String[] ss = request.getParameterValues("wll");
  String filepath = "D://test.txt";
  response.setContentType("application/octet-stream; charset=gbk");
  response.setHeader("Content-disposition", "attachment; filename=\"" + filepath + "\"");
  PrintWriter pw = null;
  try {
   pw = response.getWriter();
   pw.print(ss[0]);            
   pw.println("");
  } catch (Exception e) {
   // TODO: handle exception
  }finally{
  }

posted @ 2012-12-30 15:37  Brook Legend  阅读(359)  评论(0编辑  收藏  举报