欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

使用jsp下载excel文件

程序员文章站 2022-05-28 20:49:17
<%@ page contenttype="application/x-xls;charset=gbk"%>  <%@ page import=&q...

<%@ page contenttype="application/x-xls;charset=gbk"%> 
<%@ page import="java.io.file,java.io.fileinputstream"%> 
<%@ page import="javax.servlet.servletoutputstream"%> 
<% 
out.clear(); 
out = pagecontext.pushbody(); 
string filename = request.getparameter("filename"); 
response.setheader("content-disposition","inline;filename="+filename); 
file fileload = new file(request.getrealpath("")+"/shipment/template", filename); 
long filelength = fileload.length(); 
string length = string.valueof(filelength); 
response.setheader("content_length", length); 
fileinputstream input = null; 
servletoutputstream output = null; 
try { 
   input = new fileinputstream(fileload); 
   output = response.getoutputstream(); 
   byte[] block = new byte[1024]; 
   int len = 0; 
   while ((len = input.read(block)) != -1) { 
    output.write(block, 0, len); 
   } 
} catch (exception e) { 
   e.getmessage(); 
} finally { 
   try { 
    input.close(); 
    output.flush(); 
    output.close(); 
   } catch (exception ex) { 
    ex.getmessage(); 
   } 

%> 
  
 
作者“erdp技术架构”