A. JSP implied object response implementation file Download introduction
(1) The simplest way to implement a file download in JSP is to define a hyperlink to a target resource, and the user clicks the hyperlink to download the resource directly, but exposes the URL of the resource directly.
can also have some negative effects, such as easy to be hotlinking by other websites, causing the local server download overload.
(2) Another way to download the file is to use the file output stream implementation download, first through the response header to inform the client browser, will receive the information to save
For a file, and then use the output stream object to the client to transfer file data, the browser received data after the data saved as a file, this download method is the advantage of the service
The security of the resource path of the server is good, and the traffic of downloading and log registration can be controlled.
Two. The following is a description of how the two files are downloaded
(1) binary file download
The basic principle of downloading binary files with JSP program is: first encapsulate the source file into byte input stream object, read the file data through the object, get the response object
Byte output stream object that transmits binary byte data to the client through an output stream object.
1. Encapsulate the source file into a byte input stream object
2. Read binary byte 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
Text files are downloaded with a character stream, not a byte flow. First obtains the source file the character input stream object, uses the Java.io.FileReader class to encapsulate,
The FileReader object is then encapsulated as a java.io.BufferedReader to facilitate reading one row at a time from a text file. The character output stream is directly implicit with JSP
Contains object Out,out can output character data.
The code is as follows:
<%@ page contenttype= "application/x-download" import= "java.io.*"%><% int sta
tus=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 (); }%>