Data flow to the client through the JSP

Source: Internet
Author: User
Tags rar save file client root directory
js| Client | Data flow to the client via data JSP

/**
@ Author: Ci Qin Qiang
@Email: cqq1978@Gmail.com
*/



One often asks the question: "How does a server-side program output a file to the client and then let the browser

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

Connection, point to the file to be exported, such as <a href= "A.rar" > Download a.rar</a&gt, so that when the user clicks

When you connect, you will pop the IE Save file dialog box and download it.



However, there are two drawbacks to this:



One is that browsers sometimes get smart, for some common file types will invoke the relevant application to open,

For example, download a Word document. doc file, and sometimes the browser will open it by default with a Word program.



Second, it is to prevent the problem of hotlinking. A user can refer to the file directly through the URL address, causing

The embezzlement of the file.



It is now possible to write data directly to the client in a JSP, similar to what we use in ASP

The ADODB.stream object. In this way, we can put the files we need to protect outside the Web root directory,

For example, the Web root directory is F:\tomcat\webapps\ROOT, and we can put the file in the F:\software\ directory

, so that there is no way for others to download the file by calling the URL.



OK, let's take a look at how the code is written.



First, set the ContentType of response object as Application/octet-stream;



Next, set the HTTP header property of the Response object content-disposition to a filename,

For example: attachment; Filename= "A.rar", where the filename is "Save as" dialog box appears

Filename.



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

Specific examples are:

<%
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 particularly noteworthy that this approach is to read data directly from a file and then output to the client. So in the entire file, no more



Output any information to the client, because the information that is exported in other ways is also saved in the file, which destroys the structure of the file. Like what

<% symbol before the occurrence of even a space, a carriage return is not allowed, so be sure to be careful.




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.