jsp从服务器下载xls文件到客户端
2012-10-15 22:24
459 查看
参考网上的代码写了一个下载xls文件到客户端的jsp页面,只要将服务器的文件地址传给这个jsp页面就可以实现下载文件到客户端了。
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.另外一个修改后的版本(下载文件名可包含中文)
Html代码
<%@ 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>
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>
<%@ 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.另外一个修改后的版本(下载文件名可包含中文)
Html代码
<%@ 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从服务器下载xls文件到客户端
- jsp实现从服务器下载xls文件到客户端的方法
- jsp实现从服务器下载xls文件到客户端的方法
- java从服务器下载xls文件到客户端
- 下载服务器文件至客户端/WebClient上传文件至服务器
- 直接客户端浏览器下载文件,不必通过回传服务器再下载(例如将table内容导出excel)
- jsp页面点击下载文件 java方式(服务器上带路径文件 以文件流输出)
- JSP实现远程文件下载保存到服务器指定目录中的方法
- jsp下载服务器文件
- Jsp 下载远程文件至服务器
- Android TCP 文件客户端文件下载与服务器
- [java]客户端从服务器下载文件
- C#从服务器下载文件到客户端源码
- web服务器如何判断客户端文件已下载结束?(php实现)
- 服务器 判断 客户端 文件下载
- web项目-文件下载(文件服务器-应用服务器-客户端)
- Delphi7 中利用Indy的TidFtp客户端控件实现在服务器指定目录下下载需要的文件
- 【转载】使用jsp实现文件上传到服务器或者从服务器上下载文件到本地计算机完整说明版
- 用压缩流jsp实现从Tomcat服务器下载多个文件
- 下载(二)客户端通过C# 代码直接从服务器下载文件到本地