java從伺服器下載xls檔案到用戶端

來源:互聯網
上載者:User

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

Code:
  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://test//test.xls">      
  51.              冷山sky_snow      
  52.          </option>      
  53.      </select>      
  54.      <input type="submit"/>      
  55. </form>   
  56. </html>  

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.