关闭

export 页面

355人阅读 评论(0) 收藏 举报
分类:
<%@page language="java" contentType="application/x-msdownload" pageEncoding="UTF-8" %>    
<%@page language="java" import="java.io.*" %>
<%@page language="java" import="java.net.URLEncoder" %>
<%@page language="java" import="java.net.URLDecoder" %>
<%@page language="java" import="com.ejpt.util.charconvert.CharConvert" %>
<%
out.clear();    
out = pageContext.pushBody();
	request.setCharacterEncoding("GBK");
	String filename = request.getParameter("filename");
	System.out.print("文件名称:"+filename+"");
	String path = request.getParameter("path");
	String df = request.getSession().getServletContext().getRealPath("/");
    //关于文件下载时采用文件流输出的方式处理:    
    //加上response.reset(),并且所有的%>后面不要换行,包括最后一个;    
  	response.reset();//可以加也可以不加
  	
  	response.setContentType("application/x-download");    
   
   
	String filedisplay =  filename;
 	String filedownload = df+path;
	//System.out.println("下载路径:"+filedownload+" end");
	File fileLen = new File(filedownload);
	long len = fileLen.length();
	response.setContentLength((int)len);
  	response.addHeader("Content-Disposition","attachment;filename=" + new String(filedisplay.getBytes("gb2312"),"iso8859-1"));    
   
  	java.io.OutputStream outp = null;    
  	java.io.FileInputStream in = null;    
  	try    
  	{    
  		outp = response.getOutputStream();
  		File file = new File(filedownload);
  		in = new FileInputStream(file);    
   
  		byte[] b = new byte[1024];    
  		int i = 0;    
   
  		while((i = in.read(b)) > 0)    
  		{    
  			outp.write(b, 0, i);    
  		}
  		
		//      
		outp.flush();    
		//要加以下两句话,否则会报错    
		//java.lang.IllegalStateException: getOutputStream() has already been called for //this response      
		out.clear();    
		out = pageContext.pushBody();    
	}
	catch(Exception e)    
	{    
	  System.out.println("Error!");    
	  e.printStackTrace();    
	}    
	finally    
	{    
	  if(in != null)    
	  {    
	  in.close();    
	  in = null;    
	  }    
	}  
%>

<html>
<head>
<title>导出Excel</title>	
</head>
	
</html>
  <a href="/swbz/pages/dmbzbzb/export.jsp?filename=sjy.doc&path=/preModel/sjy.doc" >下载</a>

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:113276次
    • 积分:1399
    • 等级:
    • 排名:千里之外
    • 原创:39篇
    • 转载:20篇
    • 译文:0篇
    • 评论:18条
    文章分类
    最新评论