JSP Implicit Object response Implementation File Download

  • 2021-10-15 11:11:09
  • OfStack

1. Briefly introduce the JSP implicit object response implementation file download

(1) The simplest way to realize file download in JSP is to define hyperlinks to target resources, and users can download resources directly after clicking hyperlinks. However, URL, which directly exposes resources, will also bring some negative effects, such as being easily stolen by other websites, resulting in overload of local server download.
(2) Another method of downloading files is to use file output stream to realize downloading. Firstly, the client browser is informed by response header, and the received information is saved as a file. Then the output stream object is used to transmit file data to the client, and the browser saves the data as a file after receiving the data. This downloading method has the advantages of serving
The server-side resource path has good confidentiality, and can control the download flow and log registration.
2. Two ways to download files
(1) Download of binary files
The basic principle of downloading binary files with JSP program is: firstly, encapsulate the source file into byte input stream object, read the file data through this object, obtain the byte output stream object of response object, and transmit the binary byte data to the client through the output stream object.
1. Encapsulate the source file as a byte input stream object
2. Read binary byte data and transfer 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
Text files are downloaded using character streams instead of byte streams. Firstly, the character input stream object of the source file is obtained and encapsulated with java. io. FileReader class, and then the FileReader object is encapsulated as java. io. BufferedReader, so that it is convenient to read one line at a time from the text file. Character output stream directly uses the implicit object out of JSP, and out 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 is helpful for everyone to learn JSP implicit object response implementation file download.


Related articles: