Send data to the client through data flow in jsp

Source: Internet
Author: User


Send data to the client in jsp (preferred for SUN Enterprise applications)

/**
@ Author: ciqinqiang
@ Email: cqq1978@Gmail.com
*/



Someone often asks the following question: "How does a server program output a file to the client and then let the browser

Open a "save file" dialog box to save the file ." Of course, a simple approach is to do

Connection, pointing to the file to be output, such as <a href = "a.rar"> download a.rar </a>.

The Save file dialog box for IE will pop up during connection, and then download the file.


However, this has two drawbacks:


First, browsers are sometimes smart, and some common file types can be opened by calling related applications,

For example, download a worddocument. DOC file. Sometimes the browser opens it with a word program by default.


Second, it is to prevent leeching. A user can reference this file through the url address at will, resulting in

File theft.


Now, in jsp (the preferred choice for SUN Enterprise applications), you can directly write data to the client through file streams, similar to the one we use in asp

The adodb. stream object. In this way, we can put the files we need to protect out of the Web root directory,

For example, the web root directory is in the F: omcat (a very useful JSP running platform) webdeskroot, and we can put the file in the f: software Directory.

In this way, no one else can download the file by calling the url.


Now let's take a look at how to write the code.


First, set the contenttype of the response object to APPLICATION/OCTET-STREAM;


Next, set the http header attribute Content-Disposition of the response object to a file name,

For example: attachment; filename = "a.rar", where filename appears in the "Save as" dialog box

File Name.


Finally, you need to use the FileInputStream object to read data from the file and then output the data.

Example:

<%
String filename = "JavaFlash.rar ";
String filepath = "f :\";
Response. setContentType ("APPLICATION/OCTET-STREAM ");
Response. setHeader ("Content-Disposition", "attachment; filename =" a.rar "");
Java. io. FileInputStream fileInputStream = new java. io. FileInputStream (filepath + filename );
Int I;
While (I = fileInputStream. read ())! =-1 ){
Out. write (I );}
FileInputStream. close ();
Out. close ();
%>


It is worth noting that this method reads data directly from the file and then outputs the data to the client. Therefore, you cannot


Output any information to the client, because the information output in other ways will also be saved in the file, which destroys the file structure. For example,

Even a space before the symbol <% cannot be returned by a carriage return, so be careful.

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.