当前位置: 代码迷 >> JavaScript >> jsp生成 文件上载 比如txt
  详细解决方案

jsp生成 文件上载 比如txt

热度:134   发布时间:2012-11-09 10:18:48.0
jsp生成 文件下载 比如txt

download.jsp

<%@page import="java.io.*"%>

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
String filename = request.getParameter("filename");//"tzhsymd.txt";    
String filepath = request.getParameter("filepath");//"路径";    
response.setContentType("application/octet-stream");    
response.setHeader("Content-Disposition","attachment;filename = "+filename);  
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filepath+filename),"utf-8"));
BufferedWriter fos =new BufferedWriter(new OutputStreamWriter(response.getOutputStream(),"utf-8"));
String line;
while((line=br.readLine())!=null){
    fos.write(line+"\n\r");
}
fos.flush();
fos.close();
br.close();
out.clear();
out = pageContext.pushBody();
%>   

//out.clear();
//out = pageContext.pushBody();

加上这两个是防止  getOutputStream() has already been called for this response


在点击链接上添加 filename filepath 参数

<a href="./download.jsp?filename=fwscl.txt&filepath=<%=lujing %>" >下载</a>

  相关解决方案