JSP implicit object response realizes file download

I This paper briefly introduces the file download of JSP implicit object response

(1) The simplest way to realize file download in JSP is to define the hyperlink to point to the target resource. Users directly download the resource after clicking the hyperlink, but directly exposing the URL of the resource will also bring some negative effects. For example, it is easy to be stolen by other websites, resulting in excessive download load on the local server. (2) Another way to download a file is to use the file output stream to realize the download. First, inform the client browser through the response header to save the received information as a file, and then use the output stream object to transmit the file data to the client. After receiving the data, the browser saves the data as a file. The advantage of this download method is that the server-side resource path Good confidentiality, and can control the download flow and log registration. II Two file download methods (1) The basic principle of downloading binary files with JSP program is: first, encapsulate the source file into a byte input stream object, read the file data through the object, obtain the byte output stream object of the response object, and transmit 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 is generated and transmitted to the client. The code is as follows:

(2) Text file download when downloading a text file, the character stream is used instead of the byte stream. First, get the character input stream object of the source file, encapsulate it with java.io.filereader class, and then encapsulate the FileReader object as java.io.bufferedreader, so as to read one line at a time from the text file. The character output stream directly uses the implicit object out of JSP, which can output character data 。

The code is as follows:

I hope this article will be helpful for you to learn the file download of JSP implicit object response.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>