jsp实现从服务器下载xls文件到客户端的方法
程序员文章站
2022-04-15 09:19:18
本文实例讲述了jsp实现从服务器下载xls文件到客户端的方法。分享给大家供大家参考,具体如下:
参考网上的代码写了一个下载xls文件到客户端的jsp页面,只要将服务器的文...
本文实例讲述了jsp实现从服务器下载xls文件到客户端的方法。分享给大家供大家参考,具体如下:
参考网上的代码写了一个下载xls文件到客户端的jsp页面,只要将服务器的文件地址传给这个jsp页面就可以实现下载文件到客户端了。
<%@ page language="java"import="java.util.*"pageencoding="utf-8"%> <%@ taglib prefix="c"uri="http://java.sun.com/jsp/jstl/core"%> <%@ page import="java.io.*" %> <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <link href="styles/basic.css" rel="stylesheet" type="text/css" /> <title>download</title> </head> <% response.setcharacterencoding("gb2312"); request.setcharacterencoding("gb2312"); if (request.getparameter("file") != null) { outputstream os = null; fileinputstream fis = null; try { string file = request.getparameter("file"); if (!(new file(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");//此项内容随文件类型而异 byte temp[] = new byte[1000]; fis = new fileinputstream(file); int n = 0; while ((n = fis.read(temp)) != -1) { os.write(temp, 0, n); } } catch (exception e) { out.print("出错"); } finally { if (os != null) os.close(); if (fis != null) fis.close(); } out.clear(); out = pagecontext.pushbody(); } %> <form action="" method="post"> <select name="file"> <option value="d:\program files\apache-tomcat-6.0.18\webapps\starattendance\upload/temp.xls"> 冷山sky_snow </option> </select> <input type="submit"/> </form> </html>
<%@ page language="java" import="java.util.*" pageencoding="utf-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ page import="java.io.*" %> <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <link href="styles/basic.css" rel="stylesheet" type="text/css" /> <title>download</title> </head> <% response.setcharacterencoding("gb2312"); request.setcharacterencoding("gb2312"); if (request.getparameter("file") != null) { outputstream os = null; fileinputstream fis = null; try { string file = request.getparameter("file"); if (!(new file(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");//此项内容随文件类型而异 byte temp[] = new byte[1000]; fis = new fileinputstream(file); int n = 0; while ((n = fis.read(temp)) != -1) { os.write(temp, 0, n); } } catch (exception e) { out.print("出错"); } finally { if (os != null) os.close(); if (fis != null) fis.close(); } out.clear(); out = pagecontext.pushbody(); } %> <form action="" method="post"> <select name="file"> <option value="d:\program files\apache-tomcat-6.0.18\webapps\starattendance\upload/temp.xls"> 冷山sky_snow </option> </select> <input type="submit"/> </form> </html>
2.另外一个修改后的版本(下载文件名可包含中文)
<%@ page language="java"import="java.util.*,java.net.*"pageencoding="utf-8"%> <%@ taglib prefix="c"uri="http://java.sun.com/jsp/jstl/core"%> <%@ page import="java.io.*" %> <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <link href="styles/basic.css" rel="stylesheet" type="text/css" /> <title>download</title> </head> <% response.setcharacterencoding("utf-8"); request.setcharacterencoding("utf-8"); string filepath = new string(request.getparameter("file").getbytes("iso-8859-1"),"utf-8"); system.out.println("============================"+filepath); if (filepath != null) { outputstream os = null; fileinputstream fis = null; try { string file = filepath; if (!(new file(file)).exists()) { system.out.println("没有文件"); return; } string filefilename = file.substring(file.lastindexof("\\")+1); system.out.println("文件名为:"+filename); os = response.getoutputstream(); response.setheader("content-disposition", "attachment;filename=" + new string(filename.getbytes("gbk"), "iso-8859-1")); response.setcontenttype("application/octet-stream");//八进制流 与文件类型无关 byte temp[] = new byte[1024]; fis = new fileinputstream(file); int n = 0; while ((n = fis.read(temp)) != -1) { os.write(temp, 0, n); } } catch (exception e) { out.print("出错了"); } finally { if (os != null) os.close(); if (fis != null) fis.close(); } out.clear(); out = pagecontext.pushbody(); } %> </html>
希望本文所述对大家jsp程序设计有所帮助。
上一篇: 如何制作K线图?