极乐门资源网 Design By www.ioogu.com
本文实例讲述了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程序设计有所帮助。
极乐门资源网 Design By www.ioogu.com
极乐门资源网
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
极乐门资源网 Design By www.ioogu.com
暂无jsp实现从服务器下载xls文件到客户端的方法的评论...
更新日志
2024年12月23日
2024年12月23日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]