Solve java Chinese issues
Introduction ... 1
Environment ... 1
Solve it ... 1
I. JSP Chinese Show ... 1
1. Enter Chinese name directly to the address bar ... 1
2. Transfer through super connection ... 2
3. Transport by ActionForm ... 2
4. Web Server Uriencoding. 2
5. JS River Parameters ... 2
Two. Database Chinese ... 3
III. Mail Chinese ... 3
Send Chinese name attachment ... 3
2. Receive Chinese name attachment ... 3
Four. SOAP message Chinese ... 5
1. Base64 encoding ... 5
2. Unicode encoding ... 5
5. Browser displays Chinese name files ... 5
6. Upload Chinese name files ... 6
Seven. Multi-language mixed problems ... 6
Reference ... 7
preface
One of the difficulties in writing Java applications is how Chinese solves. This article has made some discussions on this issue.
surroundings
Solve the way
I.Jsp Chinese display
1. Enter the Chinese name directly to the address bar
Such as: Enter http: // localhost: 8888 / uum / tree.do? Type = ou & DN = OU = test company, DC = CMHK, DC = COM
String DN = Request.getParameter ("DN");
DN = new string (DN.GetBytes ("ISO-8859-1")); // Display Chinese
2. Transfer by super connection
Such as: test
String DN = Request.getParameter ("DN");
DN = New String (DN.GetBytes ("ISO-8859-1"), "UTF-8"));
3. Transport by ActionForm
html: form>
Select 1: Convert to Chinese with encoded filters
Select 2: Code Conversion
String DN = form.getdn ();
DN = New String (DN.GetBytes ("ISO-8859-1"), "UTF-8"));
4. Web server uriencoding
The default is ISO-8859-1, it can be modified if necessary. See the browser to display Chinese name files
5. JS illegal parameters
such as:
Var str = ""
IF (Document.ws.dns.value! = "") {
Str = Document.ws.dns.value "@" Document.ws.names.value;
}
Var RT = WINDOW.SHOWMODALDIALOG
"cmhk / portal / ldapframe.jsp? type = dep & dn = ou = test company, DC = cmHK, DC = COM",
Str,
"Dialogwidth = 800px; Dialogheight = 600px");
IF (rt! = null && = ") {
Var ss = rt.split ("@"); Document.ws.dns.value = ss [0];
Document.ws.names.value = ss [1];
Document.ws.to.value = ss [1];
}
In ldapframe.jsp, you must make the following conversion to get the right Chinese (similar to one .1)
String DN = New String (Request.getParameter ("DN"). GetBytes ("ISO-8859-1");
Database in Chinese
Database character encoding is consistent with incoming data encoding.
Three. Mail in Chinese
1. Send Chinese Name Accessories
@ MessageBodyPart.SetFileName (MimeUtility.EncodeWord (filename);
@ Mimebodypart.setFileName (file.getname (). GetBytes (), "ISO8859-1");
@Javamail can support Chinese well, but when the test process is found to use JavaMail to send an attachment with the Chinese file name, the file name is garbled. Test and view the source file discovery, it is a write message in the JavaMail package. The method does not consider international factors, and there is no consideration for bilingual or multi-byte characters. That is, com.sun.mail.util.LineoutputStream calls com.sun.mail.util.asciiutility.getbytes (String S); this method directly converts the string into characters and converts into bytes. In fact, the getBytes () method in the String class can directly convert into bytecode according to the default encoding of the system. Workaround: Tune the com.sun.mail.util.LineoutputStream; part of the method is changed to S.GetBytes () to solve this problem. Replace the COM / Sun / Mail / Util / LineoutPutStream.class file in javamail with recompiled LINEOUTSTREAM.CLASS files
2. Receive Chinese Name Accessories
Nowadays, there are more and more people in javamail. Now I will talk about an attachment Chinese issue that everyone will encounter in javamail. Due to the Chinese issue of Sun's Java, it will appear as garbled when the attachment is Chinese, resulting in abnormal procedures, how to Avoid? ? I used the following method to analyze the mail body when I learned Javamail, first determined whether the mail body has an attachment: object out_content = message [i] .GetContent (); if (! (! (Out_content instanceof multipart) {// is not a composite message Body, both without accessories.
Partname = new string [0]; Return Partname;} If it is a composite mail body, you can call the following method to get a Chinese file name: public static string getisofilename (part body) {// Set a flag, determine the file name from Content -Disposition is obtained from Content-Type to get boolean flag = true; if (body == null) {return null;} String [] cdis; try {cdis = body.getHeader ("Content-Disposition");} catch (Exception E) {Return NULL;} if (cdis == null) {flag = false;} if (! Flag) {Try {CDIS = body.getheader ("content-type");} catch (Exception E) { Return null;}}} f (cdis == null) { Return null;} = {RETURN NULL;} // Get filename IF (FLAG) {INT POS = CDIS [0] .indexof from Content-Disposition; "FileName ="); IF (POS <0) {return null;} // If file name with quotation marks IF (CDIS [0] .Charat (CDIS [0] .length () - 1) == '") {Return CDIS [0] . Substring (POS 10, CDIS [0] .length () - 1);
Return CDIS [0] .substring (POS 9, CDIS [0] .length ());} else {INT POS = CDIS [0] .indexof ("Name ="); if (POS <0) {Return NULL;} // If the file name is quoted IF (CDIS [0] .Charat (CDIS [0] .length () - 1) == '"') {Return CDIS [0] .substring (POS 6, CDIS [0] .length () - 1);} Return CDIS [0] .substring (POS 5, CDIS [0] .length ());}}, but there is a case, the obtained Chinese file name is also garbled, The reason why the mail server is specially encoded for the attachment name, solving the method of the above problem is as follows: First: 1) Use the above method to get a Chinese file name: string filename = new string (GetisofileName (Part). GetBytes ("ISO-8859-1"), "GB2312"); then create a file, if filenotfoundexception is abnormal, indicating that the file name is specially encoded - we have achieved unresolved, is it chaos, then Just use the function decodeText provided by Javamail. The specific method is as follows .Try {myfileoutputstream = new fileoutputstream (filename);} catch (filenotfoundexception fe) {TRY {IF (filepath.exis TS ()) {filename = new file (filepath, mimeutility.decodetext);} catch (exception e) {system.out.println ("GetPart (INT i) Regeneration file:" E. TOSTRING ());}}}} catch (exception e) {system.out.println ("GetPart (INT i) Regenerated file:" E.toTRING ());}} After the above steps, you can completely solve the JavaMail attachment Chinese name garbled problem. Four. SoAP Message Chinese
Base64 encoding
See [CMHK-OA-SD-Web Service.doc]
2. Unicode encoding
See [CMHK-OA-SD-Web Service.doc]
5. Browser displays Chinese name files
Under Tomcat, modify the configuration file server.xml
Enablelookups = "false" redirectport = "8443" acceptcount = "100" Debug = "0" ConnectionTimeout = "20000" DisableUploadTimeout = "true" uriencoding = "UTF-8" /> Uriencoding = "UTF-8" Properties Support Chinese Document Browse For example: Enter http: // localhost: 82 / ws / one .doc, there is no finding of the file (this file in the directory). 6. Upload Chinese name Use JavaZoom's UPLOADBEAN upload components to upload Chinese name files MultipartformDataRequest MREQUEST = New MultipartFormDataRequest REQUEST, NULL, MultipartformDataRquest.maxContentLengthallowed, MultipartFormDataRequest.cosparser, "UTF-8"); // Specify UTF-8 encoding Seven. Multilingual mixed problems Because Java will have a bug that can't be mixed, it will not be able to display the multi-language BUG. This weekend studied the problem of servlet, JSP's multi-language display, which is the multi-character set problem of servlet, because I am not very good to the concept of character set. Clearly written, it is not necessarily accurate, I understand the character set in Java: When running, all string objects are stored in Unicode internal code (I think all languages There is correspondingly encoded because the internal strings are always represented by the internal code, but only the string coding time platform in the normal computer language is related, while Java uses the platform-independent Unicode). Java When reading a string from a BYTE stream, the platform-related BYTE transitions to the platform-independent Unscore string. Java will turn the Unicode string to the platform-related BYTE stream when the output is output. If a UN iCode character does not exist on a platform, one '?' Will be output. For example: In Chinese Windows, Jav A reads a "GB2312" encoded file (which can be any stream) to construct a string object in the memory, which will transform the GB2 312 encoded to the Unicode encoded string, If the output of this string will convert the Unicode string to the BYTE stream or array of GB2312: "Chinese test" -----> "/ u4E2D / u6587 / u6d4b / u8bd5" - ---> " test". The following routines: byte [] bytes = new byte [] {(byte) 0xD6, (byte) 0xD0, (byte) 0xce, (byte) 0xc4, (byte) 0xB2, (Byte) 0xE2, (Byte) 0xca, byte) 0xd4}; // GBK coded "test Chinese" java.io.ByteArrayInputStream bin = new java.io.ByteArrayInputStream (bytes); java.io.BufferedReader reader = new java.io.BufferedReader (new java.io. INPU TSTREAMREADER (BIN, "GBK")); string msg = reader.readline (); Resources u http://www.w3.org/tr/Unicode-xml/