检查前端页面编码 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
修改Eclipse字符集 Window > Preferences > General > Workspace
,修改Text file encoding
项。
选择 Other
,下拉框选择UTF-8
原先项目乱码解决 然后你就会发现你的项目里的汉字全是乱码,这时候随便在某个地方创建一个Java文件,命名为Main.java
import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.io.PrintWriter;public class Main { public static void main (String[] args) { File f1 = new File ("C:/eclipse-workspace" ); File f2 = new File ("C:/eclipse-workspace2" ); try { new Main ().copyFile(f1, f2); } catch (IOException e) { e.printStackTrace(); } } private void copyFile (File f1, File f2) throws FileNotFoundException, IOException { if (f1.isDirectory()) { f2.mkdir(); File[] fs = f1 .listFiles(); for (File subF : fs) { copyFile(subF, new File (f2, subF.getName())); } } else if (f1.isFile() && f1.getName().endsWith(".java" )) { parse2UTF_8(f1, f2); } else { copyData(f1, f2); } } private void copyData (File f1, File f2) throws FileNotFoundException, IOException{ FileInputStream fis = new FileInputStream (f1); FileOutputStream fos = new FileOutputStream (f2, false ); byte [] bytes = new byte [1024 ]; int temp = 0 ; while (-1 != (temp = fis.read(bytes))) { fos.write(bytes, 0 , temp); } fos.flush(); if (null != fis) { fis.close(); } if (null != fos) { fos.close(); } } private void parse2UTF_8 (File file, File destFile) throws IOException { StringBuffer msg = new StringBuffer (); PrintWriter ps = new PrintWriter (new OutputStreamWriter (new FileOutputStream (destFile.getAbsolutePath(), false ), "utf8" )); BufferedReader br = new BufferedReader (new InputStreamReader (new FileInputStream (file.getAbsolutePath()), "gbk" )); String line = br.readLine(); while (line != null ) { msg.append(line).append("\r\n" ); line = br.readLine(); } ps.write(msg.toString()); br.close(); ps.flush(); ps.close(); } }
关闭Eclipse,然后 javac,java编译运行这段代码。
然后删掉旧的workspace文件夹,eclipse打开新的workspace文件夹。
修改后端代码 到Servlet的get、post方法添加这几行代码
response.setContentType("text/html;charset=utf-8" ); request.setCharacterEncoding("utf-8" ); response.setCharacterEncoding("utf-8" );
【可选项】优化效率,字符串的获取方式可以变为:
String name = new String (request.getParameter("name" ).getBytes("utf-8" ), "utf-8" ))
修改MySQL的url:
String url="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8"
修改数据库字符集 到你的MySQL去,执行以下代码(通过Navicat的话,直接New Query就行)
SET character_set_client = "utf8";SET character_set_connection = "utf8";SET character_set_database = "utf8";SET character_set_results = "utf8";SET character_set_server = "utf8";SET character_set_system = "utf8";SHOW VARIABLES LIKE "%chara%";
如果表格大概长这样,就行了:
Variable_name
Value
character_set_client
utf8
character_set_connection
utf8
character_set_database
utf8
character_set_filesystem
binary
character_set_results
utf8
character_set_server
utf8
character_set_system
utf8