jsp实现从服务器下载xls文件到客户端的方法
本文实例讲述了jsp实现从服务器下载xls文件到客户端的方法。分享给大家供大家参考,具体如下:
参考网上的代码写了一个下载xls文件到客户端的jsp页面,只要将服务器的文件地址传给这个jsp页面就可以实现下载文件到客户端了。
<%@pagelanguage="java"import="java.util.*"pageEncoding="utf-8"%> <%@taglibprefix="c"uri="http://java.sun.com/jsp/jstl/core"%> <%@pageimport="java.io.*"%> <!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <htmlxmlns="http://www.w3.org/1999/xhtml"> <head> <metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/> <linkhref="styles/basic.css"rel="stylesheet"type="text/css"/> <title>download</title> </head> <% response.setCharacterEncoding("gb2312"); request.setCharacterEncoding("gb2312"); if(request.getParameter("file")!=null){ OutputStreamos=null; FileInputStreamfis=null; try{ Stringfile=request.getParameter("file"); if(!(newFile(file)).exists()){ System.out.println("没有文件"); return; } System.out.println("文件名为:"+file); os=response.getOutputStream(); response.setHeader("content-disposition","attachment;filename="+file); response.setContentType("application/vnd.ms-excel");//此项内容随文件类型而异 bytetemp[]=newbyte[1000]; fis=newFileInputStream(file); intn=0; while((n=fis.read(temp))!=-1){ os.write(temp,0,n); } }catch(Exceptione){ out.print("出错"); }finally{ if(os!=null) os.close(); if(fis!=null) fis.close(); } out.clear(); out=pageContext.pushBody(); } %> <formaction=""method="post"> <selectname="file"> <optionvalue="D:\ProgramFiles\apache-tomcat-6.0.18\webapps\StarAttendance\upload/temp.xls"> 冷山sky_snow </option> </select> <inputtype="submit"/> </form> </html>
<%@pagelanguage="java"import="java.util.*"pageEncoding="utf-8"%> <%@taglibprefix="c"uri="http://java.sun.com/jsp/jstl/core"%> <%@pageimport="java.io.*"%> <!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <htmlxmlns="http://www.w3.org/1999/xhtml"> <head> <metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/> <linkhref="styles/basic.css"rel="stylesheet"type="text/css"/> <title>download</title> </head> <% response.setCharacterEncoding("gb2312"); request.setCharacterEncoding("gb2312"); if(request.getParameter("file")!=null){ OutputStreamos=null; FileInputStreamfis=null; try{ Stringfile=request.getParameter("file"); if(!(newFile(file)).exists()){ System.out.println("没有文件"); return; } System.out.println("文件名为:"+file); os=response.getOutputStream(); response.setHeader("content-disposition","attachment;filename="+file); response.setContentType("application/vnd.ms-excel");//此项内容随文件类型而异 bytetemp[]=newbyte[1000]; fis=newFileInputStream(file); intn=0; while((n=fis.read(temp))!=-1){ os.write(temp,0,n); } }catch(Exceptione){ out.print("出错"); }finally{ if(os!=null) os.close(); if(fis!=null) fis.close(); } out.clear(); out=pageContext.pushBody(); } %> <formaction=""method="post"> <selectname="file"> <optionvalue="D:\ProgramFiles\apache-tomcat-6.0.18\webapps\StarAttendance\upload/temp.xls"> 冷山sky_snow </option> </select> <inputtype="submit"/> </form> </html>
2.另外一个修改后的版本(下载文件名可包含中文)
<%@pagelanguage="java"import="java.util.*,java.net.*"pageEncoding="utf-8"%> <%@taglibprefix="c"uri="http://java.sun.com/jsp/jstl/core"%> <%@pageimport="java.io.*"%> <!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <metahttp-equiv="Content-Type"content="text/html;charset=utf-8"> <htmlxmlns="http://www.w3.org/1999/xhtml"> <head> <metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/> <linkhref="styles/basic.css"rel="stylesheet"type="text/css"/> <title>download</title> </head> <% response.setCharacterEncoding("UTF-8"); request.setCharacterEncoding("UTF-8"); Stringfilepath=newString(request.getParameter("file").getBytes("ISO-8859-1"),"UTF-8"); System.out.println("============================"+filepath); if(filepath!=null){ OutputStreamos=null; FileInputStreamfis=null; try{ Stringfile=filepath; if(!(newFile(file)).exists()){ System.out.println("没有文件"); return; } Stringfilefilename=file.substring(file.lastIndexOf("\\")+1); System.out.println("文件名为:"+filename); os=response.getOutputStream(); response.setHeader("content-disposition","attachment;filename="+newString(filename.getBytes("GBK"),"ISO-8859-1")); response.setContentType("application/octet-stream");//八进制流与文件类型无关 bytetemp[]=newbyte[1024]; fis=newFileInputStream(file); intn=0; while((n=fis.read(temp))!=-1){ os.write(temp,0,n); } }catch(Exceptione){ out.print("出错了"); }finally{ if(os!=null) os.close(); if(fis!=null) fis.close(); } out.clear(); out=pageContext.pushBody(); } %> </html>
希望本文所述对大家JSP程序设计有所帮助。