This article example describes the method of JSP to download xls files from the server to the client. Share it for your reference, as follows:
Refer to the code on the Internet to write a jsp page to download the xls file to the client. Just pass the server's file address to the jsp page to download the file to the client.
<%@ page language="java"import=".*"pageEncoding="utf-8"%> <%@ taglib prefix="c"uri="/jsp/jstl/core"%> <%@ page import=".*" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:///TR/xhtml1/DTD/"> <html xmlns="http:///1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link href="styles/" rel="stylesheet" type="text/css" /> <title>download</title> </head> <% ("gb2312"); ("gb2312"); if (("file") != null) { OutputStream os = null; FileInputStream fis = null; try { String file = ("file"); if (!(new File(file)).exists()) { ("No file"); return; } ("File name:"+file); os = (); ("content-disposition", "attachment;filename=" + file); ("application/-excel");//This content varies according to the file typebyte temp[] = new byte[1000]; fis = new FileInputStream(file); int n = 0; while ((n = (temp)) != -1) { (temp, 0, n); } } catch (Exception e) { ("Error"); } finally { if (os != null) (); if (fis != null) (); } (); out = (); } %> <form action="" method="post"> <select name="file"> <option value="D:\Program Files\apache-tomcat-6.0.18\webapps\StarAttendance\upload/"> Lengshansky_snow </option> </select> <input type="submit"/> </form> </html>
<%@ page language="java" import=".*" pageEncoding="utf-8"%> <%@ taglib prefix="c" uri="/jsp/jstl/core"%> <%@ page import=".*" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:///TR/xhtml1/DTD/"> <html xmlns="http:///1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link href="styles/" rel="stylesheet" type="text/css" /> <title>download</title> </head> <% ("gb2312"); ("gb2312"); if (("file") != null) { OutputStream os = null; FileInputStream fis = null; try { String file = ("file"); if (!(new File(file)).exists()) { ("No file"); return; } ("File name:"+file); os = (); ("content-disposition", "attachment;filename=" + file); ("application/-excel");//This content varies according to the file type byte temp[] = new byte[1000]; fis = new FileInputStream(file); int n = 0; while ((n = (temp)) != -1) { (temp, 0, n); } } catch (Exception e) { ("Error"); } finally { if (os != null) (); if (fis != null) (); } (); out = (); } %> <form action="" method="post"> <select name="file"> <option value="D:\Program Files\apache-tomcat-6.0.18\webapps\StarAttendance\upload/"> Lengshansky_snow </option> </select> <input type="submit"/> </form> </html>
2. Another modified version (the download file name can contain Chinese)
<%@ page language="java"import=".*,.*"pageEncoding="utf-8"%> <%@ taglib prefix="c"uri="/jsp/jstl/core"%> <%@ page import=".*" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:///TR/xhtml1/DTD/"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <html xmlns="http:///1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link href="styles/" rel="stylesheet" type="text/css" /> <title>download</title> </head> <% ("UTF-8"); ("UTF-8"); String filepath = new String(("file").getBytes("ISO-8859-1"),"UTF-8"); ("============================"+filepath); if (filepath != null) { OutputStream os = null; FileInputStream fis = null; try { String file = filepath; if (!(new File(file)).exists()) { ("No file"); return; } String filefilename = (("\\")+1); ("File name:"+filename); os = (); ("content-disposition", "attachment;filename=" + new String(("GBK"), "ISO-8859-1")); ("application/octet-stream");//Octal stream has nothing to do with file typebyte temp[] = new byte[1024]; fis = new FileInputStream(file); int n = 0; while ((n = (temp)) != -1) { (temp, 0, n); } } catch (Exception e) { ("What went wrong"); } finally { if (os != null) (); if (fis != null) (); } (); out = (); } %> </html>
I hope this article will be helpful to everyone's JSP programming.