HttpServletResponse Object Implementation File download

Source: Internet
Author: User
<span id="Label3"></p><p><p>The Web server receives an HTTP request from the client, creating a Request object for each request, and a response object representing the response, respectively.<br>The request and response objects represent requests and responses, so we need to get the data submitted by the client, just find the request Object. To output data to a client, you just need to find the response Object.</p></p><p><p>first, HttpServletResponse Object Introduction<br><br>The HttpServletResponse object represents the response of the Server. This object encapsulates the method of sending data to the client, sending a response header, and sending a response status Code. Look at the HttpServletResponse API to see these related methods.</p></p><p><p>1.1, responsible for sending data to the client (browser) related methods<br><br>  </p></p><p><p>1.2, responsible for sending the response header to the client (browser) related methods</p></p><p><p>  </p></p><p><p>1.3, responsible for sending the response status code to the client (browser) related methods</p></p><p><p>  </p></p><p><p>1.4, The constant response status code<br>  <br>HttpServletResponse defines a number of constants for the status code (see the Servlet's api), which can be used when a response status code needs to be sent to the client, avoiding direct write numbers, and constants that correspond to common status codes:</p></p><p><p>Constants corresponding to status code 404<br><br> </p></p><p><p>Constants corresponding to status code 200<br><br>  </p></p><p><p>Constants corresponding to status code 500<br><br>  </p></p><p><p>Ii. common applications of HttpServletResponse objects</p></p><p><p>2.1, using OutputStream flow to the client browser output Chinese data</p></p><p><p>Use OutputStream stream to output Chinese note the Problem:</p></p><p><p>On the server side, the data is the Code table output, then control the client browser with the corresponding code table open, such as: outputstream.write ("china". getBytes ("UTF-8")); Use OutputStream flow to the client browser output chinese, to UTF-8 encoding for output, at this time to control the client browser to UTF-8 encoding open, otherwise display will appear in Chinese garbled, So how does the server side control the client browser to display the data in UTF-8 encoding? You can control the behavior of the browser by setting the response header, for example: Response.setheader ("content-type", "text/html;charset=utf-8"), and by setting the response header to control the browser to display the data in UTF-8 encoding.</p></p><p><p>Example: using the OutputStream to flow to the client browser output "china" two characters</p></p><pre class="prettyprint"><code class=" hljs java"><span class="hljs-keyword"><span class="hljs-keyword"></span> package</span>gacl.response.study;<span class="hljs-number"><span class="hljs-number">2</span></span> <span class="hljs-number"><span class="hljs-number">3</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>java.io.IOException;<span class="hljs-number"><span class="hljs-number">4</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>java.io.OutputStream;<span class="hljs-number"><span class="hljs-number">5</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>javax.servlet.ServletException;<span class="hljs-number"><span class="hljs-number">6</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>javax.servlet.http.HttpServlet;<span class="hljs-number"><span class="hljs-number">7</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>javax.servlet.http.HttpServletRequest;<span class="hljs-number"><span class="hljs-number">8</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>javax.servlet.http.HttpServletResponse;<span class="hljs-number"><span class="hljs-number">9</span></span> <span class="hljs-number"><span class="hljs-number">Ten</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-class"><span class="hljs-class"> <span class="hljs-keyword">class</span> <span class="hljs-title">ResponseDemo01</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">httpservlet</span> {</span></span><span class="hljs-number"><span class="hljs-number"></span> one</span> <span class="hljs-number"><span class="hljs-number"></span> a</span> <span class="hljs-keyword"><span class="hljs-keyword">Private</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Static</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Final</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Long</span></span>Serialversionuid =<span class="hljs-number"><span class="hljs-number">4312868947607181532</span></span>L<span class="hljs-number"><span class="hljs-number"></span> -</span> <span class="hljs-number"><span class="hljs-number"></span> -</span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">Doget</span></span>(httpservletrequest request, HttpServletResponse Response) 15<span class="hljs-keyword"><span class="hljs-keyword">throws</span></span>servletexception, IOException {<span class="hljs-number"><span class="hljs-number"></span> -</span>Outputchinesebyoutputstream (response);<span class="hljs-comment"><span class="hljs-comment">//use outputstream stream to output Chinese</span></span><span class="hljs-number"><span class="hljs-number"></span> -</span>}<span class="hljs-number"><span class="hljs-number"></span> -</span> <span class="hljs-number"><span class="hljs-number"></span> +</span> <span class="hljs-javadoc"><span class="hljs-javadoc">/**20 * Use outputstream stream output Chinese *<span class="hljs-javadoctag"> @param</span> request22 *<span class="hljs-javadoctag"> @param</span> response23 *<span class="hljs-javadoctag"> @thro WS</span> IOException *</span> /</span><span class="hljs-number"><span class="hljs-number"></span> -</span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">Outputchinesebyoutputstream</span></span>(httpservletresponse Response)<span class="hljs-keyword"><span class="hljs-keyword">throws</span></span>ioexception{<span class="hljs-number"><span class="hljs-number"></span> -</span> <span class="hljs-javadoc"><span class="hljs-javadoc">/** using OutputStream output Chinese note: 27 * On the server side, the data is the Code table output, then control the client browser with the corresponding code table open, 28 * for Example: Outputstream.write ("china ". getBytes (" UTF-8 "));//use outputstream flow to the client browser output chinese, output in UTF-8 encoding 29 * At this time to control the client browser to UTF-8 encoding open, otherwise display will appear when the Chinese garbled, then in the service How does the server side control the client browser to display data in UTF-8 encoding? 30 * You can control the behavior of the browser by setting the response header, for example: * response.setheader ("content-type", "text/html;charset=utf-8");//by Setting the response header control The browser displays the data in UTF-8 Code.</span></span><span class="hljs-number"><span class="hljs-number"></span> -</span>String data =<span class="hljs-string"><span class="hljs-string">"china"</span></span>;<span class="hljs-number"><span class="hljs-number"></span> the</span>OutputStream outputstream = Response.getoutputstream ();<span class="hljs-comment"><span class="hljs-comment">//get outputstream output stream</span></span><span class="hljs-number"><span class="hljs-number"></span> *</span>Response.setheader (<span class="hljs-string"><span class="hljs-string">"content-type"</span></span>,<span class="hljs-string"><span class="hljs-string">"text/html;charset=utf-8"</span></span>);<span class="hljs-comment"><span class="hljs-comment">//by Setting the response header control browser to display data in UTF-8 encoding, if not add this sentence, then the browser will display is garbled</span></span><span class="hljs-number"><span class="hljs-number"></span> $</span> <span class="hljs-javadoc"><span class="hljs-javadoc">/**37 * Data.getbytes () is a process of converting characters into Byte arrays, and this process will certainly go to the code table, 38 * If it is a Chinese operating system environment, The default is to find the GB2312 code table, 39 * The process of converting a character to a byte array is to convert the Chinese characters to the corresponding number on the GB2312 's Code table 40 * for Example: "medium" on GB2312 's code table corresponds to the number 9841 * "country" on the GB2312 code table corresponds to the number is 9942 * *</span></span><span class="hljs-number"><span class="hljs-number"></span> +</span> <span class="hljs-javadoc"><span class="hljs-javadoc">/**44 * GetBytes () method if no parameters, then according to the operating System's language environment to choose the conversion code table, if it is a Chinese operating system, then use the GB2312 code table *</span> /</span><span class="hljs-number"><span class="hljs-number"></span> $</span> <span class="hljs-keyword"><span class="hljs-keyword">byte</span></span>[] Databytearr = Data.getbytes (<span class="hljs-string"><span class="hljs-string">"UTF-8"</span></span>);<span class="hljs-comment"><span class="hljs-comment">//convert character to byte array, specify UTF-8 encoding for conversion</span></span><span class="hljs-number"><span class="hljs-number"></span> -</span>Outputstream.write (databytearr);<span class="hljs-comment"><span class="hljs-comment">//using OutputStream to flow to the client output byte array</span></span><span class="hljs-number"><span class="hljs-number"></span> -</span>}<span class="hljs-number"><span class="hljs-number"></span> the</span> <span class="hljs-number"><span class="hljs-number"></span> -</span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">DoPost</span></span>(httpservletrequest request, HttpServletResponse Response) 51<span class="hljs-keyword"><span class="hljs-keyword">throws</span></span>servletexception, IOException {<span class="hljs-number"><span class="hljs-number"></span> the</span>Doget (request, response);<span class="hljs-number"><span class="hljs-number"></span> -</span>}<span class="hljs-number"><span class="hljs-number"></span> wu</span> <span class="hljs-number"><span class="hljs-number"></span> -</span>}</code></pre><p><p>The results of the operation are as Follows:<br><br>After the client browser receives the data, it parses the data according to the character encoding set on the response header, as Follows:<br></p></p><p><p>2.2. File Download</p></p><p><p>File download function is often used in web development, the use of HttpServletResponse object can be used to achieve file download</p></p><p><p>The implementation of the file download function idea:</p></p><p><p>1. Get the absolute path to the file you want to download</p></p><p><p>2. Get the file name to download</p></p><p><p>3. Set the Content-disposition response header to control the browser to download the form of open file</p></p><p><p>4. Get the file input stream to download</p></p><p><p>5. Create a data buffer</p></p><p><p>6. Get the OutputStream stream from the response object</p></p><p><p>7. Writing the FileInputStream stream to buffer buffers</p></p><p><p>8. Using OutputStream to output buffer data to the client browser</p></p><p><p>Example: using response for file download</p></p><pre class="prettyprint"><code class=" hljs java"> <span class="hljs-keyword"><span class="hljs-keyword"></span> package</span>gacl.response.study;<span class="hljs-number"><span class="hljs-number">2</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>java.io.FileInputStream;<span class="hljs-number"><span class="hljs-number">3</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>java.io.FileNotFoundException;<span class="hljs-number"><span class="hljs-number">4</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>java.io.FileReader;<span class="hljs-number"><span class="hljs-number">5</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>java.io.IOException;<span class="hljs-number"><span class="hljs-number">6</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>java.io.InputStream;<span class="hljs-number"><span class="hljs-number">7</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>java.io.OutputStream;<span class="hljs-number"><span class="hljs-number">8</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>java.io.PrintWriter;<span class="hljs-number"><span class="hljs-number">9</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>java.net.URLEncoder;<span class="hljs-number"><span class="hljs-number">Ten</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>javax.servlet.ServletException;<span class="hljs-number"><span class="hljs-number"></span> one</span> <span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>javax.servlet.http.HttpServlet;<span class="hljs-number"><span class="hljs-number"></span> a</span> <span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>javax.servlet.http.HttpServletRequest;<span class="hljs-number"><span class="hljs-number"></span> -</span> <span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>javax.servlet.http.HttpServletResponse;<span class="hljs-number"><span class="hljs-number"></span> -</span> <span class="hljs-javadoc"><span class="hljs-javadoc">/**15 *<span class="hljs-javadoctag"> @author</span> gacl16 * File Download *</span> *</span><span class="hljs-number"><span class="hljs-number"></span> -</span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-class"><span class="hljs-class"> <span class="hljs-keyword">class</span> <span class="hljs-title">ResponseDemo02</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">httpservlet</span> {</span></span><span class="hljs-number"><span class="hljs-number"></span> +</span> <span class="hljs-number"><span class="hljs-number"></span> -</span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">Doget</span></span>(httpservletrequest request, HttpServletResponse Response) 21<span class="hljs-keyword"><span class="hljs-keyword">throws</span></span>servletexception, IOException {<span class="hljs-number"><span class="hljs-number"></span> a</span>Downloadfilebyoutputstream (response);<span class="hljs-comment"><span class="hljs-comment">//download files via OutputStream stream</span></span><span class="hljs-number"><span class="hljs-number"></span> at</span>}<span class="hljs-number"><span class="hljs-number"></span> -</span> <span class="hljs-number"><span class="hljs-number"></span> -</span> <span class="hljs-javadoc"><span class="hljs-javadoc">/**26 * Download the file via outputstream stream *<span class="hljs-javadoctag"> @param</span> response28 *<span class="hljs-javadoctag"> @throws</span> FileNotFoundException29 *<span class="hljs-javadoctag"> @throws</span> IOException30 * *</span></span><span class="hljs-number"><span class="hljs-number"></span> to</span> <span class="hljs-keyword"><span class="hljs-keyword">Private</span></span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">Downloadfilebyoutputstream</span></span>(httpservletresponse Response) 32<span class="hljs-keyword"><span class="hljs-keyword">throws</span></span>filenotfoundexception, IOException {<span class="hljs-number"><span class="hljs-number"></span> -</span> <span class="hljs-comment"><span class="hljs-comment">//1. Get the absolute path to the file you want to download</span></span><span class="hljs-number"><span class="hljs-number"></span> the</span>String Realpath =<span class="hljs-keyword"><span class="hljs-keyword"></span> this</span>. Getservletcontext (). Getrealpath (<span class="hljs-string"><span class="hljs-string">"/download/1.jpg"</span></span>);<span class="hljs-number"><span class="hljs-number"></span> *</span> <span class="hljs-comment"><span class="hljs-comment">//2. Get the file name to download</span></span><span class="hljs-number"><span class="hljs-number"></span> $</span>String fileName = realpath.substring (realpath.lastindexof (<span class="hljs-string"><span class="hljs-string">"\\"</span></span>)+<span class="hljs-number"><span class="hljs-number">1</span></span>);<span class="hljs-number"><span class="hljs-number">Panax Notoginseng</span></span> <span class="hljs-comment"><span class="hljs-comment">//3. Setting the Content-disposition response header to control the browser to open the file as downloaded</span></span><span class="hljs-number"><span class="hljs-number"></span> -</span>Response.setheader (<span class="hljs-string"><span class="hljs-string">"content-disposition"</span></span>,<span class="hljs-string"><span class="hljs-string">"attachment;filename="</span></span>+filename);<span class="hljs-number"><span class="hljs-number"></span> the</span> <span class="hljs-comment"><span class="hljs-comment">//4. getting the file input stream to download</span></span><span class="hljs-number"><span class="hljs-number"></span> +</span>InputStream in =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>FileInputStream (realpath);<span class="hljs-number"><span class="hljs-number"></span> a</span> <span class="hljs-keyword"><span class="hljs-keyword">int</span></span>Len =<span class="hljs-number"><span class="hljs-number">0</span></span>;<span class="hljs-number"><span class="hljs-number"></span> the</span> <span class="hljs-comment"><span class="hljs-comment">//5. Creating a data buffer</span></span><span class="hljs-number"><span class="hljs-number"></span> +</span> <span class="hljs-keyword"><span class="hljs-keyword">byte</span></span>[] buffer =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span> <span class="hljs-keyword"><span class="hljs-keyword">byte</span></span>[<span class="hljs-number"><span class="hljs-number">1024x768</span></span>];<span class="hljs-number"><span class="hljs-number"></span> -</span> <span class="hljs-comment"><span class="hljs-comment">//6. getting the OutputStream stream from a response object</span></span><span class="hljs-number"><span class="hljs-number"></span> $</span>OutputStream out = Response.getoutputstream ();<span class="hljs-number"><span class="hljs-number"></span> $</span> <span class="hljs-comment"><span class="hljs-comment">//7. Writing FileInputStream streams to buffer buffers</span></span><span class="hljs-number"><span class="hljs-number"></span> -</span> <span class="hljs-keyword"><span class="hljs-keyword"></span> while</span>(len = In.read (buffer)) ><span class="hljs-number"><span class="hljs-number">0</span></span>) {<span class="hljs-number"><span class="hljs-number"></span> -</span> <span class="hljs-comment"><span class="hljs-comment">//8. using OutputStream to output buffer data to the client browser</span></span><span class="hljs-number"><span class="hljs-number"></span> the</span>Out.write (buffer,<span class="hljs-number"><span class="hljs-number">0</span></span>, len);<span class="hljs-number"><span class="hljs-number"></span> -</span>}<span class="hljs-number"><span class="hljs-number">Wuyi</span></span>In.close ();<span class="hljs-number"><span class="hljs-number"></span> the</span>}<span class="hljs-number"><span class="hljs-number"></span> -</span> <span class="hljs-number"><span class="hljs-number"></span> wu</span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">DoPost</span></span>(httpservletrequest request, HttpServletResponse Response) 55<span class="hljs-keyword"><span class="hljs-keyword">throws</span></span>servletexception, IOException {<span class="hljs-number"><span class="hljs-number"></span> about</span>Doget (request, response);<span class="hljs-number"><span class="hljs-number"></span> $</span>}<span class="hljs-number"><span class="hljs-number"></span> -</span>}</code></pre><p><p>The results of the operation are as Follows:<br><br>Example: using response to implement Chinese file download</p></p><p><p>When downloading Chinese files, it is important to note that the Chinese file name is encoded using the Urlencoder.encode method (urlencoder.encode (filename, "character encoding")), Otherwise the file name will be Garbled.<br>  </p></p><pre class="prettyprint"><code class=" hljs java"><span class="hljs-keyword"><span class="hljs-keyword"></span> package</span>gacl.response.study;<span class="hljs-number"><span class="hljs-number">2</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>java.io.FileInputStream;<span class="hljs-number"><span class="hljs-number">3</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>java.io.FileNotFoundException;<span class="hljs-number"><span class="hljs-number">4</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>java.io.FileReader;<span class="hljs-number"><span class="hljs-number">5</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>java.io.IOException;<span class="hljs-number"><span class="hljs-number">6</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>java.io.InputStream;<span class="hljs-number"><span class="hljs-number">7</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>java.io.OutputStream;<span class="hljs-number"><span class="hljs-number">8</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>java.io.PrintWriter;<span class="hljs-number"><span class="hljs-number">9</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>java.net.URLEncoder;<span class="hljs-number"><span class="hljs-number">Ten</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>javax.servlet.ServletException;<span class="hljs-number"><span class="hljs-number"></span> one</span> <span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>javax.servlet.http.HttpServlet;<span class="hljs-number"><span class="hljs-number"></span> a</span> <span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>javax.servlet.http.HttpServletRequest;<span class="hljs-number"><span class="hljs-number"></span> -</span> <span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>javax.servlet.http.HttpServletResponse;<span class="hljs-number"><span class="hljs-number"></span> -</span> <span class="hljs-javadoc"><span class="hljs-javadoc">/**15 *<span class="hljs-javadoctag"> @author</span> gacl16 * File Download *</span> *</span><span class="hljs-number"><span class="hljs-number"></span> -</span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-class"><span class="hljs-class"> <span class="hljs-keyword">class</span> <span class="hljs-title">ResponseDemo02</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">httpservlet</span> {</span></span><span class="hljs-number"><span class="hljs-number"></span> +</span> <span class="hljs-number"><span class="hljs-number"></span> -</span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">Doget</span></span>(httpservletrequest request, HttpServletResponse Response) 21<span class="hljs-keyword"><span class="hljs-keyword">throws</span></span>servletexception, IOException {<span class="hljs-number"><span class="hljs-number"></span> a</span>Downloadchinesefilebyoutputstream (response);<span class="hljs-comment"><span class="hljs-comment">//download Chinese file</span></span><span class="hljs-number"><span class="hljs-number"></span> at</span>}<span class="hljs-number"><span class="hljs-number"></span> -</span> <span class="hljs-number"><span class="hljs-number"></span> -</span> <span class="hljs-javadoc"><span class="hljs-javadoc">/**26 * Download the Chinese file, the Chinese file download, the file name is url-encoded, otherwise the filename will appear garbled *<span class="hljs-javadoctag"> @param</span> response28 *<span class="hljs-javadoctag"> @throws</span> Filenot FOUNDEXCEPTION29 *<span class="hljs-javadoctag"> @throws</span> IOException30 *</span> *</span><span class="hljs-number"><span class="hljs-number"></span> to</span> <span class="hljs-keyword"><span class="hljs-keyword">Private</span></span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">Downloadchinesefilebyoutputstream</span></span>(httpservletresponse Response) 32<span class="hljs-keyword"><span class="hljs-keyword">throws</span></span>filenotfoundexception, IOException {<span class="hljs-number"><span class="hljs-number"></span> -</span>String Realpath =<span class="hljs-keyword"><span class="hljs-keyword"></span> this</span>. Getservletcontext (). Getrealpath (<span class="hljs-string"><span class="hljs-string">"/download/zhangjiajie National Forest Park. JPG "</span></span>);<span class="hljs-comment"><span class="hljs-comment">//get the absolute path to the file to be downloaded</span></span><span class="hljs-number"><span class="hljs-number"></span> the</span>String fileName = realpath.substring (realpath.lastindexof (<span class="hljs-string"><span class="hljs-string">"\\"</span></span>)+<span class="hljs-number"><span class="hljs-number">1</span></span>);<span class="hljs-comment"><span class="hljs-comment">//get the file name to download</span></span><span class="hljs-number"><span class="hljs-number"></span> *</span> <span class="hljs-comment"><span class="hljs-comment">//set content-disposition Response header control browser to download the form of open file, Chinese file name to use the Urlencoder.encode method to encode, otherwise the file name garbled</span></span><span class="hljs-number"><span class="hljs-number"></span> $</span>Response.setheader (<span class="hljs-string"><span class="hljs-string">"content-disposition"</span></span>,<span class="hljs-string"><span class="hljs-string">"attachment;filename="</span></span>+urlencoder.encode (fileName,<span class="hljs-string"><span class="hljs-string">"UTF-8"</span></span>));<span class="hljs-number"><span class="hljs-number">Panax Notoginseng</span></span>InputStream in =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>FileInputStream (realpath);<span class="hljs-comment"><span class="hljs-comment">//get file input stream</span></span><span class="hljs-number"><span class="hljs-number"></span> -</span> <span class="hljs-keyword"><span class="hljs-keyword">int</span></span>Len =<span class="hljs-number"><span class="hljs-number">0</span></span>;<span class="hljs-number"><span class="hljs-number"></span> the</span> <span class="hljs-keyword"><span class="hljs-keyword">byte</span></span>[] buffer =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span> <span class="hljs-keyword"><span class="hljs-keyword">byte</span></span>[<span class="hljs-number"><span class="hljs-number">1024x768</span></span>];<span class="hljs-number"><span class="hljs-number"></span> +</span>OutputStream out = Response.getoutputstream ();<span class="hljs-number"><span class="hljs-number"></span> a</span> <span class="hljs-keyword"><span class="hljs-keyword"></span> while</span>(len = In.read (buffer)) ><span class="hljs-number"><span class="hljs-number">0</span></span>) {<span class="hljs-number"><span class="hljs-number"></span> the</span>Out.write (buffer,<span class="hljs-number"><span class="hljs-number">0</span></span>, len);<span class="hljs-comment"><span class="hljs-comment">//output buffer data to client browser</span></span><span class="hljs-number"><span class="hljs-number"></span> +</span>}<span class="hljs-number"><span class="hljs-number"></span> -</span>In.close ();<span class="hljs-number"><span class="hljs-number"></span> $</span>}<span class="hljs-number"><span class="hljs-number"></span> $</span> <span class="hljs-number"><span class="hljs-number"></span> -</span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">DoPost</span></span>(httpservletrequest request, HttpServletResponse Response) 48<span class="hljs-keyword"><span class="hljs-keyword">throws</span></span>servletexception, IOException {<span class="hljs-number"><span class="hljs-number"></span> the</span>Doget (request, response);<span class="hljs-number"><span class="hljs-number"></span> -</span>}<span class="hljs-number"><span class="hljs-number">Wuyi</span></span>}</code></pre><p><p>The results of the operation are as Follows:<br></p></p><p><p>Exporting Chinese data to the client browser can use the OutputStream stream<br>You can also use the PrintWriter stream<br>PrintWriter Flow detailed description see the following blog post</p></p><p><p>Http://www.cnblogs.com/xdp-gacl/p/3789624.html</p></p> <p><p>HttpServletResponse Object Implementation File download</p></p></span>

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.