程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> JSP編程 >> 關於JSP >> jsp生成 文件下載 比如txt

jsp生成 文件下載 比如txt

編輯:關於JSP

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 respons

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved