Let's start by talking about the causes of things:
Made a Java Web project where there is a Java file with a function:
/** * @param s * @return get data MD5 */public static string MD5 (string s) {char hexdigits[]={' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ' , ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' D ', ' E ', ' F '}; try {byte[] btinput = S.getbytes (); Get the MessageDigest object messagedigest mdinst = messagedigest.getinstance ("MD5") of the MD5 Digest algorithm; Updates the digest mdinst.update (btinput) using the specified byte; Obtained ciphertext byte[] MD = Mdinst.digest (); Convert ciphertext to 16 binary string form int j = md.length; Char str[] = new CHAR[J * 2]; int k = 0; for (int i = 0; i < J; i++) {byte byte0 = md[i]; str[k++] = hexdigits[byte0 >>> 4 & 0xf]; str[k++] = hexdigits[byte0 & 0xf]; } return new String (str); } catch (Exception e) {e.printstacktrace (); return null; } }
The function is naturally the MD5 code that gets the string.
But I was wondering.
As long as the input string has Chinese, such as "China"
In Java Unit Self-test, the output is: c13dceabcb143acd6c9298265d618a9f
When running as a Tomcat Web site, the output is: cf0832dedf7457bbcbfa00bbd87b300a
After investigation, it is found that the problem is in this sentence:
byte[] Btinput = S.getbytes ();
GetBytes This function is provided by the JDK,
If you do not specify an encoding for the input string, the JRE is automatically read from dfile.encoding.
The dfile.encoding of the JRE defaults to GBK.
However, my MyEclipse project was all set by me in order to UTF-8, and the input string was naturally UTF-8 format.
The JRE used in the MyEclipse is not the JRE that we installed on our system, but the one we own, this JRE you can not see in the cmd under the java-version.
The self-brought JRE automatically loads the code of the project itself at run time, and then runs it with that code, so when you self-test in a Java file, run as application out the right.
And I run the site with my own configuration of Tomcat, as a Java program run, do not specify Dfile.encoding, the default is GBK, so the above error occurs.
Solution:
(1) Change the parameters of Tomcat as a Java program runtime. This only needs to set the JRE's startup parameters.
(2) Develop a good habit and specify the encoding in the code. Recommended ) such as: GetBytes ("UTF-8")
Note: A JRE instance, under Windows process name Java.exe can only have one dfile.encoding, and a Tomcat will run in a Java program. So multiple sites under Tomcat can only share a single dfile.encoding, in other words, this encoding is not Tomcat for Java VMS.
MyEclipse Configuration
1. JDK code: window-->preferences-->java-->installed jres-->edit jre-->default VM Arguments- Dfile.encoding=utf-8
2. File code: Window-->preferences-->general-->content types-->text UTF-8
3. Work Area Workspace code: Window-->preferences-->general-->workspace
4. Project Project code set project name right-click Properties Resource
5. TOMCA Settings: Window-->preferences-->myeclipse-->servers-->tomcat-->tomcat 5.x-->jdk--> Optional Java VM Arguments-dfile.encoding=utf-8
"Java+eclipse encoding Settings" MD5 code-throwing murders