UTF8 Converts to GB2312 When we get data or send requests in an application based on the HTTP protocol, the JVM encodes the transported data into the UTF8 format. If we extract Chinese data directly from the HTTP stream, the extracted result is "????" (possibly more question marks), in order to convert the Chinese characters we can understand, we need to convert UTF8 into GB2312, with ISO-8859 -1 Standard coding can be easily implemented, the following code implements this feature:
Byte [] B;
String UTF8_VALUE;
UTF8_VALUE = Request.getParameter ("name"); // Take UTF8 data from "Name" from HTTP stream
B = UTF8_VALUE.GETBYTES ("8859_1"); // Transition with ISO-8859-1 in the middle
String name = new string (b, "gb2312"); // Convert to GB2312 characters
In the case of knowing the flow length, the input stream converted into byte array Java has intread (Byte [] B, int off, int LEN) method, parameter BYTE [] B is used to store From the data read from the InputStream, the int OFF specifies the offset address of the array B, which is the start subscript of the array B, and the int LEN specifies the length you need to read, the method returns the number of bytes actually read. Friends who have just learned Java may say: First define a length of byte arrays such as flow length, call the read method, specify the starting date of 0, specify the length of reading length and array length, not to read it again Yet? It's true that the author has also tried to read data, but later found that it is unsafe when reading network data, we think that the data can be not so smooth on the network, the data flow may be disconnected, Therefore, you can not guarantee that all data can be read once, especially when reading a large capacity data, so we must detect the length of the actual read when reading the data, and if you don't have a known length data You should read again to cycle it until the length of the actual read is equal to the known length, the following code implements this function:
ServletinputStream Instream = Request.GetinputStream (); // Take the HTTP request stream
INT size = request.getContentLength (); // Take the HTTP request flow length
Byte [] buffer = new byte [size]; // Used to cache data each read
BYTE [] in_b = new byte [size]; // A number of arrays for storage results
INT count = 0;
INT rbyte = 0;
While (count Rbyte = instream.read (buffer); / / Each actual reading length is stored in RBYTE For (int i = 0; i IN_B [count i] = buffer [i]; } Count = RBYTE; } In the case where the input stream conversion is introduced in the case where the input stream conversion is introduced in front of the flow length, then when we don't know how long it is, it is not possible to determine the conversion. How old is there in an array group? The author reviewed the JDK documentation and found that ByteArrayoutputStream has a Byte [] TobyTearRay () method, which automatically creates a byte array and then returns. So smartly use ByteArrayoutputStream to make an intermediate transition implementation conversion, and other processing is similar to those of the above. Suppose the stream that needs to be converted is already in the Instream, we can use the following code: ByteArrayoutputStream swapstream = new byteArrayoutputStream (); Byte [] buff = new byte [100]; // BUFF is used to store temporary data for loop read INT RC = 0; While ((rc = instream.read (buff, 0, 100))> 0) { SwapStream.write (buff, 0, rc); } Byte [] in_b = swapstream.tobyteArray (); // IN_B is the result after the conversion