<code class= "Hljs java" >package gacl.response.study;
2
3 Import java.io.IOException;
4 Import Java.io.OutputStream;
5 Import javax.servlet.ServletException;
6 Import Javax.servlet.http.HttpServlet;
7 Import Javax.servlet.http.HttpServletRequest;
8 Import Javax.servlet.http.HttpServletResponse;
9
public class ResponseDemo01 extends HttpServlet {
11
The private static final long serialversionuid = 4312868947607181532L;
13
public void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
Outputchinesebyoutputstream (response);//output Chinese with OutputStream stream
17}
18
19/**
20 * Output Chinese using OutputStream stream
* @param request
* @param response
IOException * @throws
24 */
public void Outputchinesebyoutputstream (HttpServletResponse response) throws ioexception{
26/** use OutputStream to output Chinese note the problem:
27 * On the server side, the data is the Code table output, then control the client browser to open the corresponding code table,
28 * For example: Outputstream.write ("China". GetBytes ("UTF-8"));//use OutputStream to 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, or display will appear when the Chinese garbled, then on the server side how to 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");//Display data with UTF-8 encoding by setting the response header control browser
32 */
The String data = "China";
OutputStream outputstream = Response.getoutputstream ();//get OutputStream output stream
Response.setheader ("Content-type", "text/html;charset=utf-8");//By setting the response header control browser to display data in UTF-8 encoding, if you do not add this sentence, Then the browser will display a garbled
36/**
The PNS * Data.getbytes () is a process of converting characters into byte arrays, which is sure to check the Code table,
38 * If it is a Chinese operating system environment, the default is to look for the GB2312 code table,
39 * The process of converting a character to a byte array is to convert the Chinese characters to the corresponding numbers on the GB2312 's Code table.
40 * For example: "Medium" on the GB2312 of the Code table corresponding to the number is 98
41 * "Country" the corresponding number on the GB2312 's Code table is 99
42 */
43/**
* 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
45 */
byte[] Databytearr = data.getbytes ("UTF-8");//convert character to byte array, specify to convert by UTF-8 encoding
Outputstream.write (Databytearr);//use OutputStream to flow to the client output byte array
48}
49
public void DoPost (HttpServletRequest request, httpservletresponse response)
Wuyi throws Servletexception, IOException {
Doget (request, response);
53}
54
}</code>
2.2. File download
File download function is often used in web development, the use of HttpServletResponse object can be used to achieve file download
The implementation of the file download function idea:
1. Get the absolute path to the file you want to download
2. Get the file name to download
3. Set the Content-disposition response header to control the browser to download the form of open file
4. Get the file input stream to download
5. Create a data buffer
6. Get the OutputStream stream from the response object
7. Writing the FileInputStream stream to buffer buffers
8. Using OutputStream to output buffer data to the client browser
Example: Using response for file download
<code class= "Hljs java" > Package gacl.response.study;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileReader;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.OutputStream;
Import Java.io.PrintWriter;
Import Java.net.URLEncoder;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
/**
* @author GaCl
* File Download
*/
public class ResponseDemo02 extends HttpServlet {
public void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
Downloadfilebyoutputstream (response);//download file via OutputStream stream
}
/**
* Download files via OutputStream stream
* @param response
* @throws FileNotFoundException
* @throws IOException
*/
private void Downloadfilebyoutputstream (HttpServletResponse response)
Throws FileNotFoundException, IOException {
1. Get the absolute path to the file you want to download
String Realpath = This.getservletcontext (). Getrealpath ("/download/1.jpg");
2. Get the file name to download
String fileName = realpath.substring (realpath.lastindexof ("\ \") +1);
3. Set the Content-disposition response header to control the browser to download the form of open file
Response.setheader ("Content-disposition", "attachment;filename=" +filename);
4. Get the file input stream to download
InputStream in = new FileInputStream (Realpath);
int len = 0;
5. Create a data buffer
byte[] buffer = new byte[1024];
6. Get the OutputStream stream from the response object
OutputStream out = Response.getoutputstream ();
7. Writing the FileInputStream stream to buffer buffers
while (len = in.read (buffer)) > 0) {
8. Using OutputStream to output buffer data to the client browser
Out.write (Buffer,0,len);
}
In.close ();
}
public void DoPost (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
Doget (request, response);
}
}</code>
Note: Calling the WebService interface during upload and download can convert the file to a byte array to write Response.getoutputstream (). Write (), The request is sent to the other end to read the information in the response. The upload is written in the client via response and read in the server request. The download is written in the server via response and read in the client request.
WebService transferring files through response and request objects