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>
<%@ 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.另外一个修改后的版本(下载文件名可包含中文)

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>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值