jsp實現從伺服器下載xls檔案到用戶端的方法_JSP編程

來源:互聯網
上載者:User

本文執行個體講述了jsp實現從伺服器下載xls檔案到用戶端的方法。分享給大家供大家參考,具體如下:

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

<%@ 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>
<%@ 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.另外一個修改後的版本(下載檔案名稱可包含中文)

<%@ 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");//八進位流 與檔案類型無關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程式設計有所協助。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.