JSP implicit object response implementation file download two methods

  • 2020-11-26 18:57:36
  • OfStack

1.JSP implicit Object response implementation file download introduction

(1) The easiest way to realize file download in JSP is to define a hyperlink pointing to the target resource. Users click the hyperlink and download the resource directly, but URL directly exposes the resource

It will also bring some negative effects, such as easy to be downloaded by other websites hotlinking, causing the local server to load too much.

(2) Another way to download files is to use the file output stream to download. First, the client browser is informed via the response header to save the received information

Is a file, and then use the output stream object to transfer file data to the client. After the browser receives the data, the data will be saved as a file. The advantage of this download method is to serve

The privacy of the resource path on the server side is good, and the download flow and log registration can be controlled.

2. The following two ways to download files are introduced

(1) Download of base 2 files

The basic principle of downloading binary files with the JSP program is that the source file is first encapsulated as a byte input stream object, through which the file data is read and the response object is obtained

Byte output stream object, through which byte data in base 2 is passed to the client.

1. Encapsulate source files as byte input stream objects

2. Read base 2 byte data and transmit 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 a stream of characters, not a stream of bytes. First get the character input stream object of the source file, encapsulated in the class ES37en.io.FileReader,

The FileReader object is then encapsulated as ES43en.io.BufferedReader to facilitate reading 1 line at a time from a text file. The character output stream USES JSP's hidden directly

With object out, 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(); 
} 
%>

Related articles: