国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JSP > 正文

jsp實現從服務器下載xls文件到客戶端的方法

2020-07-27 21:29:56
字體:
來源:轉載
供稿:網友

本文實例講述了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("http://")+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程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 天等县| 凤城市| 宜兰市| 曲阳县| 甘德县| 昌都县| 甘孜| 大余县| 宁河县| 姜堰市| 印江| 城口县| 石渠县| 九台市| 赤城县| 鄱阳县| 扶风县| 岑巩县| 微山县| 合水县| 青州市| 宾川县| 神池县| 黄石市| 陕西省| 金阳县| 保康县| 太和县| 金乡县| 温州市| 临安市| 丹东市| 丽江市| 靖西县| 莱芜市| 达州市| 湟中县| 通州区| 高邑县| 天水市| 科技|