jsp实现从服务器下载xls文件到客户端的方法
网络编程 2021-07-05 11:23www.168986.cn编程入门
这篇文章主要介绍了jsp实现从服务器下载xls文件到客户端的方法,以完整实例形式较为详细的分析了jsp文件下载的相关实现技巧,具有一定参考借鉴价值,需要的朋友可以参考下
本文实例讲述了jsp实现从服务器下载xls文件到客户端的方法。分享给大家供大家参考,具体如下
参考网上的代码写了一个下载xls文件到客户端的jsp页面,只要将服务器的文件地址传给这个jsp页面就可以实现下载文件到客户端了。
<%@ page language="java"import="java.util."pageEncoding="utf-8"%> <%@ taglib prefix="c"uri="http://java.sun./jsp/jstl/core"%> <%@ page import="java.io." %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://.w3./TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://.w3./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./jsp/jstl/core"%> <%@ page import="java.io." %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://.w3./TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://.w3./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.."pageEncoding="utf-8"%> <%@ taglib prefix="c"uri="http://java.sun./jsp/jstl/core"%> <%@ page import="java.io." %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://.w3./TR/xhtml1/DTD/xhtml1-transitional.dtd"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <html xmlns="http://.w3./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程序设计有所帮助。
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程