jsp實(shí)現(xiàn)從服務(wù)器下載xls文件到客戶端的方法
本文實(shí)例講述了jsp實(shí)現(xiàn)從服務(wù)器下載xls文件到客戶端的方法。分享給大家供大家參考,具體如下:
參考網(wǎng)上的代碼寫了一個(gè)下載xls文件到客戶端的jsp頁(yè)面,只要將服務(wù)器的文件地址傳給這個(gè)jsp頁(yè)面就可以實(shí)現(xiàn)下載文件到客戶端了。
<%@ 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");//此項(xiàng)內(nèi)容隨文件類型而異 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("出錯(cuò)"); } 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");//此項(xiàng)內(nèi)容隨文件類型而異 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("出錯(cuò)"); } 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.另外一個(gè)修改后的版本(下載文件名可包含中文)
<%@ 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");//八進(jìn)制流 與文件類型無(wú)關(guān) 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("出錯(cuò)了"); } finally { if (os != null) os.close(); if (fis != null) fis.close(); } out.clear(); out = pageContext.pushBody(); } %> </html>
希望本文所述對(duì)大家JSP程序設(shè)計(jì)有所幫助。
相關(guān)文章
基于javaweb+mysql的jsp+servlet宿舍管理系統(tǒng)(超級(jí)管理員、宿舍管理員、學(xué)生)
基于javaweb+mysql的jsp+servlet宿舍管理系統(tǒng)(超級(jí)管理員、宿舍管理員、學(xué)生)(java+jsp+servlet+javabean+mysql+tomcat),需要的朋友可以參考下2023-08-08JavaServlet的文件上傳和下載實(shí)現(xiàn)方法
下面小編就為大家分享一篇JavaServlet的文件上傳和下載實(shí)現(xiàn)方法具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-02-02基于JSP的RSS閱讀器的設(shè)計(jì)與實(shí)現(xiàn)方法(推薦)
下面小編就為大家?guī)?lái)一篇基于JSP的RSS閱讀器的設(shè)計(jì)與實(shí)現(xiàn)方法(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-07-07