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{
  }

每一天都要行动,在前进中寻求卓越。
原文地址:https://www.cnblogs.com/wshsdlau/p/2839790.html