国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JSP > 正文

jsp實現從服務器下載xls文件到客戶端的方法

2024-09-05 00:22:24
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了jsp實現從服務器下載xls文件到客戶端的方法,以完整實例形式較為詳細的分析了jsp文件下載的相關實現技巧,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了jsp實現從服務器下載xls文件到客戶端的方法。分享給大家供大家參考,具體如下:

參考網上的代碼寫了一個下載xls文件到客戶端的jsp頁面,只要將服務器的文件地址傳給這個jsp頁面就可以實現下載文件到客戶端了。

 

  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" 
  5. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
  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("gb2312"); 
  14. request.setCharacterEncoding("gb2312"); 
  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. 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. catch (Exception e) { 
  33. out.print("出錯"); 
  34. finally { 
  35. if (os != null
  36. os.close(); 
  37. if (fis != null
  38. fis.close(); 
  39. out.clear(); 
  40. out = pageContext.pushBody(); 
  41. %> 
  42. <form action="" method="post"
  43. <select name="file"
  44. <option value="D:/Program Files/apache-tomcat-6.0.18/webapps/StarAttendance/upload/temp.xls"
  45. 冷山sky_snow 
  46. </option> 
  47. </select> 
  48. <input type="submit"/> 
  49. </form> 
  50. </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" 
  5. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
  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("gb2312"); 
  14. request.setCharacterEncoding("gb2312"); 
  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. 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. catch (Exception e) { 
  33. out.print("出錯"); 
  34. finally { 
  35. if (os != null
  36. os.close(); 
  37. if (fis != null
  38. fis.close(); 
  39. out.clear(); 
  40. out = pageContext.pushBody(); 
  41. %> 
  42. <form action="" method="post"
  43. <select name="file"
  44. <option value="D:/Program Files/apache-tomcat-6.0.18/webapps/StarAttendance/upload/temp.xls"
  45. 冷山sky_snow 
  46. </option> 
  47. </select> 
  48. <input type="submit"/> 
  49. </form>  
  50. </html> 

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

 

 
  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. String filefilename = file.substring(file.lastIndexOf("//")+1); 
  26. System.out.println("文件名為:"+filename); 
  27. os = response.getOutputStream(); 
  28. response.setHeader("content-disposition""attachment;filename=" + new String(filename.getBytes("GBK"), "ISO-8859-1")); 
  29. response.setContentType("application/octet-stream");//八進制流 與文件類型無關 
  30. byte temp[] = new byte[1024]; 
  31. fis = new FileInputStream(file); 
  32. int n = 0; 
  33. while ((n = fis.read(temp)) != -1) { 
  34. os.write(temp, 0, n); 
  35. catch (Exception e) { 
  36. out.print("出錯了"); 
  37. finally { 
  38. if (os != null
  39. os.close(); 
  40. if (fis != null
  41. fis.close(); 
  42. out.clear(); 
  43. out = pageContext.pushBody(); 
  44. %> 
  45. </html> 

希望本文所述對大家JSP程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 嘉兴市| 清徐县| 米脂县| 岳阳县| 文山县| 资兴市| 龙川县| 容城县| 祥云县| 托里县| 慈利县| 德钦县| 博乐市| 永胜县| 安平县| 安乡县| 台江县| 宁城县| 庆元县| 西林县| 昌吉市| 兴隆县| 额济纳旗| 鱼台县| 永善县| 留坝县| 河北省| 泾源县| 理塘县| 东源县| 故城县| 渝北区| 上高县| 温泉县| 惠东县| 乃东县| 和静县| 辉南县| 南昌县| 泉州市| 宿迁市|