Two Methods for downloading files using JSP implicit object response
I. Introduction to implementing File Download Using JSP implicit object response
(1) The simplest way to download a file in JSP is to define a hyperlink pointing to the target resource. You can click the hyperlink to download the resource directly, but directly expose the resource URL.
It 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 to save the received information
Is a file, and then uses the output stream object to transmit file data to the client. After receiving the data, the browser 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.
II. The following two download methods are introduced: (1) download a binary file
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 response object
To send binary bytes to the client.
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) When downloading text files, text files are downloaded using the refer stream instead of the byte stream. First, obtain the character input stream object of the source file, which is encapsulated by the java. io. FileReader class,
Encapsulate the FileReader object as java. io. BufferedReader to read a row from a text file. The character output stream is directly hidden from JSP
Including the 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();}%>