jsp从服务器下载xls文件到客户端

参考网上的代码写了一个下载xls文件到客户端的jsp页面,只要将服务器的文件地址传给这个jsp页面就可以实现下载文件到客户端了。

Html代码  复制代码  收藏代码
  1. <%@ page language="java"import="java.util.*"pageEncoding="utf-8"%>
  2. <%@ taglib prefix="c"uri="http://java.sun.com/jsp/jstl/core"%>
  3. <%@ page import="java.io.*" %>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  5. <html xmlns="http://www.w3.org/1999/xhtml">
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  8. <link href="styles/basic.css" rel="stylesheet" type="text/css" />
  9. <title>download</title>
  10. </head>
  11. <%
  12. response.setCharacterEncoding("gb2312");
  13. request.setCharacterEncoding("gb2312");
  14. if (request.getParameter("file") != null) {
  15. OutputStream os = null;
  16. FileInputStream fis = null;
  17. try {
  18. String file = request.getParameter("file");
  19. if (!(new File(file)).exists()) {
  20. System.out.println("没有文件");
  21. return;
  22. }
  23. System.out.println("文件名为:"+file);
  24. os = response.getOutputStream();
  25. response.setHeader("content-disposition", "attachment;filename=" + file);
  26. response.setContentType("application/vnd.ms-excel");//此项内容随文件类型而异
  27. byte temp[] = new byte[1000];
  28. fis = new FileInputStream(file);
  29. int n = 0;
  30. while ((n = fis.read(temp)) != -1) {
  31. os.write(temp, 0, n);
  32. }
  33. } catch (Exception e) {
  34. out.print("出错");
  35. } finally {
  36. if (os != null)
  37. os.close();
  38. if (fis != null)
  39. fis.close();
  40. }
  41. out.clear();
  42. out = pageContext.pushBody();
  43. }
  44. %>
  45. <form action="" method="post">
  46. <select name="file">
  47. <option value="D:\Program Files\apache-tomcat-6.0.18\webapps\StarAttendance\upload/temp.xls">
  48. 冷山sky_snow
  49. </option>
  50. </select>
  51. <input type="submit"/>
  52. </form>
  53. </html>
[html]  view plain copy
  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>  
  2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>  
  3. <%@ page import="java.io.*" %>  
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head>  
  7.         <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  8.         <link href="styles/basic.css" rel="stylesheet" type="text/css" />  
  9.         <title>download</title>  
  10. </head>  
  11. <%      
  12.      response.setCharacterEncoding("gb2312");      
  13.      request.setCharacterEncoding("gb2312");      
  14.      
  15.     if (request.getParameter("file") != null) {      
  16.          OutputStream os = null;      
  17.          FileInputStream fis = null;      
  18.         try {      
  19.              String file = request.getParameter("file");      
  20.             if (!(new File(file)).exists()) {      
  21.                  System.out.println("没有文件");      
  22.                 return;      
  23.              }      
  24.              System.out.println("文件名为:"+file);      
  25.              os = response.getOutputStream();      
  26.              response.setHeader("content-disposition", "attachment;filename=" + file);      
  27.              response.setContentType("application/vnd.ms-excel");//此项内容随文件类型而异      
  28.             byte temp[] = new byte[1000];      
  29.              fis = new FileInputStream(file);      
  30.             int n = 0;      
  31.             while ((n = fis.read(temp)) != -1) {      
  32.                  os.write(temp, 0, n);      
  33.              }      
  34.          } catch (Exception e) {      
  35.              out.print("出错");      
  36.          } finally {      
  37.             if (os != null)      
  38.                  os.close();      
  39.             if (fis != null)      
  40.                  fis.close();      
  41.          }      
  42.          out.clear();      
  43.          out = pageContext.pushBody();      
  44.      
  45.      }      
  46. %>      
  47.      
  48. <form action="" method="post">      
  49.      <select name="file">      
  50.          <option value="D:\Program Files\apache-tomcat-6.0.18\webapps\StarAttendance\upload/temp.xls">      
  51.              冷山sky_snow      
  52.          </option>      
  53.      </select>      
  54.      <input type="submit"/>      
  55. </form>   
  56. </html>  

2.另外一个修改后的版本(下载文件名可包含中文)

Html代码  复制代码  收藏代码
  1. <%@ page language="java"import="java.util.*,java.net.*"pageEncoding="utf-8"%>
  2. <%@ taglib prefix="c"uri="http://java.sun.com/jsp/jstl/core"%>
  3. <%@ page import="java.io.*" %>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  6. <html xmlns="http://www.w3.org/1999/xhtml">
  7. <head>
  8. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  9. <link href="styles/basic.css" rel="stylesheet" type="text/css" />
  10. <title>download</title>
  11. </head>
  12. <%
  13. response.setCharacterEncoding("UTF-8");
  14. request.setCharacterEncoding("UTF-8");
  15. String filepath = new String(request.getParameter("file").getBytes("ISO-8859-1"),"UTF-8");
  16. System.out.println("============================"+filepath);
  17. if (filepath != null) {
  18. OutputStream os = null;
  19. FileInputStream fis = null;
  20. try {
  21. String file = filepath;
  22. if (!(new File(file)).exists()) {
  23. System.out.println("没有文件");
  24. return;
  25. }
  26. String filefilename = file.substring(file.lastIndexOf("\\")+1);
  27. System.out.println("文件名为:"+filename);
  28. os = response.getOutputStream();
  29. response.setHeader("content-disposition", "attachment;filename=" + new String(filename.getBytes("GBK"), "ISO-8859-1"));
  30. response.setContentType("application/octet-stream");//八进制流 与文件类型无关
  31. byte temp[] = new byte[1024];
  32. fis = new FileInputStream(file);
  33. int n = 0;
  34. while ((n = fis.read(temp)) != -1) {
  35. os.write(temp, 0, n);
  36. }
  37. } catch (Exception e) {
  38. out.print("出错了");
  39. } finally {
  40. if (os != null)
  41. os.close();
  42. if (fis != null)
  43. fis.close();
  44. }
  45. out.clear();
  46. out = pageContext.pushBody();
  47. }
  48. %>
  49. </html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值