佳木斯湛栽影视文化发展公司

主頁(yè) > 知識(shí)庫(kù) > jsp實(shí)現(xiàn)從服務(wù)器下載xls文件到客戶端的方法

jsp實(shí)現(xiàn)從服務(wù)器下載xls文件到客戶端的方法

熱門標(biāo)簽:Mysql連接數(shù)設(shè)置 銀行業(yè)務(wù) Linux服務(wù)器 科大訊飛語(yǔ)音識(shí)別系統(tǒng) 阿里云 服務(wù)器配置 電子圍欄 團(tuán)購(gòu)網(wǎng)站

本文實(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ì)有所幫助。

您可能感興趣的文章:
  • JSP如何獲取客戶端真實(shí)IP地址
  • JSP自定義標(biāo)簽獲取用戶IP地址的方法
  • JSP 獲取真實(shí)IP地址的代碼
  • JSP入門教程之客戶端驗(yàn)證、常用輸出方式及JSTL基本用法
  • JSP中實(shí)現(xiàn)判斷客戶端手機(jī)類型并跳轉(zhuǎn)到app下載頁(yè)面
  • jsp 獲取客戶端的瀏覽器和操作系統(tǒng)信息
  • 使用JSP讀取客戶端信息
  • jsp獲取客戶端IP地址的方法

標(biāo)簽:衡水 江蘇 棗莊 衢州 萍鄉(xiāng) 蚌埠 廣元 大理

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《jsp實(shí)現(xiàn)從服務(wù)器下載xls文件到客戶端的方法》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    阜平县| 平果县| 诸城市| 宿迁市| 城步| 乌鲁木齐市| 黎川县| 芦山县| 洛扎县| 东乡族自治县| 海林市| 财经| 常熟市| 祁门县| 崇信县| 株洲县| 江孜县| 郓城县| 黎城县| 金堂县| 峨边| 涟水县| 寿光市| 东宁县| 旬邑县| 高尔夫| 株洲市| 平阳县| 乐陵市| 新平| 垫江县| 沂南县| 甘洛县| 永德县| 塔城市| 邵东县| 平湖市| 普洱| 安庆市| 普兰店市| 莫力|