JSP implicit object response implements file download,
1. Introduction to JSP implicit object response for object download
(1) The simplest way to download a file in JSP is to define a hyperlink pointing to the target resource, and the user clicks the hyperlink to download the resource directly, however, directly exposing the resource URL also has some negative effects, such as being easily leeched by other websites, resulting in excessive download load on the local server.
(2) another method for downloading files is to use the file output stream for downloading. First, inform the client browser through the response header and save the received information as a file, then, the output stream object is used to transmit the file data to the client. After the browser receives the data, it saves the data as a file. The advantage of this download method is that
The resource path on the server is highly confidential, and the download traffic and log registration can be controlled.
2. Two File Download Methods
(1) download binary files
The basic principle of downloading a binary file using a JSP program is: first encapsulate the source file into a byte input stream object, read the file data through this object, and obtain the byte output stream object of the response object, transmits binary bytes to the client through the output stream object.
1. encapsulate the source file into a byte input stream object
2. Read Binary data and transmit it to the client
The Code is as follows:
<%@ page contentType="application/x-download" import="java.io.*" %> <% int status=0; byte b[]=new byte[1024]; FileInputStream in=null; ServletOutputStream out2=null; try { response.setHeader("content-disposition","attachment; filename=d.zip"); in=new FileInputStream("c:\\tomcat\\webapps\\ROOT\\d.zip"); out2=response.getOutputStream(); while(status != -1 ) { status=in.read(b); out2.write(b); } out2.flush(); } catch(Exception e) { System.out.println(e); response.sendRedirect("downError.jsp"); } finally { if(in!=null) in.close(); if(out2 !=null) out2.close(); } %>
(2) Text File Download
The text file is downloaded using the byte stream instead of the byte stream. First, get the character input stream object of the source file, encapsulate it with the java. io. FileReader class, and then encapsulate the FileReader object as java. io. BufferedReader to read one row from the text file at a time. The character output stream directly uses the hidden JSP object out, which can output character data.
The Code is as follows:
<%@ page contentType="application/x-download" import="java.io.*" %><% int status=0; String temp=null; FileReader in=null; BufferedReader in2=null; try { response.setHeader("content-disposition","attachment; filename=ee.txt"); response.setCharacterEncoding("gb2312"); in=new FileReader("c:\\tomcat\\webapps\\ROOT\\ee.txt"); in2=new BufferedReader(in); while((temp=in2.readLine()) != null ) { out.println(temp); } out.close(); } catch(Exception e) { System.out.println(e); response.sendRedirect("downError.jsp"); } finally { if(in2!=null) in2.close(); } %>
I hope this article will help you learn about the JSP implicit object response to download files.
Articles you may be interested in:
- Sample Code for jsp File Download
- Four Methods for JSP File Download
- How to download Servlet files using jsp
- JSP implements Remote File Download and save to the specified directory on the server