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("沒(méi)有文件");
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("沒(méi)有文件");
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("沒(méi)有文件");
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)文章
jsp實(shí)現(xiàn)頁(yè)面分頁(yè)功能代碼
經(jīng)??梢杂玫降膶?duì)頁(yè)面進(jìn)行分頁(yè),下面整理好的完整的頁(yè)面分頁(yè)代碼,各位朋友需要可以參考下2017-04-04
JavaWeb程序設(shè)計(jì)之JSP實(shí)現(xiàn)購(gòu)物車功能全過(guò)程
這篇文章主要介紹了如何使用Java和相關(guān)技術(shù)實(shí)現(xiàn)一個(gè)基本的購(gòu)物車功能,包括商品列表展示、用戶登錄、結(jié)賬和查看購(gòu)物車等功能,通過(guò)編寫代碼和設(shè)計(jì)JSP頁(yè)面,實(shí)現(xiàn)了用戶交互和數(shù)據(jù)管理,需要的朋友可以參考下2025-01-01
jsp基于XML實(shí)現(xiàn)用戶登錄與注冊(cè)的實(shí)例解析(附源碼)
這篇文章主要介紹了jsp基于XML實(shí)現(xiàn)用戶登錄與注冊(cè)的實(shí)例解析,xml做數(shù)據(jù)庫(kù)實(shí)現(xiàn)用戶登錄與注冊(cè),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2015-11-11
用連接池提高Servlet訪問(wèn)數(shù)據(jù)庫(kù)的效率(1)
用連接池提高Servlet訪問(wèn)數(shù)據(jù)庫(kù)的效率(1)...2006-10-10
JSP實(shí)現(xiàn)從數(shù)據(jù)庫(kù)導(dǎo)出數(shù)據(jù)到Excel下載的方法
這篇文章主要介紹了JSP實(shí)現(xiàn)從數(shù)據(jù)庫(kù)導(dǎo)出數(shù)據(jù)到Excel下載的方法,涉及JSP操作excel文件導(dǎo)出的相關(guān)技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2015-10-10
使用JavaBean創(chuàng)建您的網(wǎng)上日歷本(2)
使用JavaBean創(chuàng)建您的網(wǎng)上日歷本(2)...2006-10-10
jsp學(xué)習(xí)之scriptlet的使用方法詳解
這篇文章主要介紹了jsp學(xué)習(xí)之scriptlet的使用方法詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
動(dòng)態(tài)jsp頁(yè)面轉(zhuǎn)PDF輸出到頁(yè)面的實(shí)現(xiàn)方法
最近遇到了很多坑,今天小編抽點(diǎn)時(shí)間給大家介紹下動(dòng)態(tài)jsp頁(yè)面轉(zhuǎn)PDF輸出到頁(yè)面的實(shí)現(xiàn)方法,感興趣的朋友一起看看吧2016-10-10

