JSP Server File Download Method, jsp Server File
This example describes how to download server files from JSP. We will share this with you for your reference. The details are as follows:
<% @ Page import = "java. io. FileInputStream" %> <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN "" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <% @ Page language =" java "contentType =" text/html; charset = UTF-8 "%> <% @ page import =" com. ecc. emp. core. context "%> <% @ page import =" java. io. file "%> <% @ include file =" http://www.xxxx.com/head.jsp "%> <% Boolean loadFlag = false; ServletOutputStream ou = null; FileInputStream FCM = null; try {Context context = (Context) request. getAttribute ("context"); String accountFilePath = (String) context. getDataValue ("accountFilePath"); // String accountFilePath = "E:/ComeBack/Acc_BOS_CM/WebContent/upload/product/snapshot"; // String accountFileName = (String) context. getDataValue ("ac CountFileName "); String displayFileName = (String) context. getDataValue ("displayFileName"); // String displayFileName = "custormerStatement.txt"; String fullPath = accountFilePath; response. reset (); response. setContentType ("charset = UTF-8"); response. setContentType ("text/HTML"); // response. setContentType ("application/vnd. ms-excel "); String filename = new String (displayFileName. getBytes ("gb2312"), "ISO8 859-1 "); response. setHeader ("Content-Disposition", "attachment; filename =" + filename); // solve the problem that HTTPS cannot be downloaded. response. setHeader ("Cache-Control", "public"); ou = response. getOutputStream (); File file = new File (fullPath); FD = new FileInputStream (file); try {if (FS! = Null) {int filelen = Fi. available (); byte a [] = new byte [filelen. read (a); ou. write (a); ou. flush ();} loadFlag = true;} catch (Exception e) {System. out. println ("Excel is not exist! ");} Out = pageContext. pushBody ();} catch (Exception e) {e. printStackTrace ();} finally {try {FCM. close (); ou. close (); ou = null; response. flushBuffer ();} catch (Exception e) {// TODO automatically generates catch Block e. printStackTrace () ;}%>
I hope this article will help you with JSP program design.