JSP Response object implements two ways of file downloading _jsp programming

Source: Internet
Author: User
Tags tomcat

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 (); }%> 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.