JSP hidden Object Response implementation file Download _jsp programming

Source: Internet
Author: User
Tags zip

I. Brief introduction of JSP hidden Object response implementation file download

(1) in the JSP implementation of the file download the simplest way is to define a hyperlink to target resources, users click hyperlinks directly download resources, but the direct exposure of resources URL will also bring some negative effects, such as easy to be hotlinking by other sites, causing the local server download overload.
(2) Another way to download the file is to use the file output stream to download, first through the response header to inform the client browser, will receive the information saved as 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, The advantage of this download method is that 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. Two kinds of files download mode
(1) binary file download
The basic principle of downloading binary files with JSP program is: first, the source file is encapsulated into a byte input stream object, the object reads the file data, obtains the byte output stream object of the response object, and transmits the binary byte data to the client through the 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 encapsulation, then encapsulates the FileReader object as the Java.io.BufferedReader, in order to be easy to read one row from the text file. The character output stream can output character data directly with the implied object out,out of the JSP.

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 will help you learn the JSP hidden Object response implementation file download.

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.