检查前端页面编码

<%@ 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

// A code block
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");//需要转码的GBK格式文件夹
File f2 = new File("C:/eclipse-workspace2");//转码成UTF-8的目标文件夹
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")) {
//对java文件进行转码
parse2UTF_8(f1, f2);
} else {
//对非java文件直接复制
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();
}
}

/*
* 该方法引用 杰克思勒(Jacksile)的文章:Eclipse修改编码后乱码解决
* 之后作了小修改
* 原码地址 http://www.cnblogs.com/tufujie/p/5137564.html
*/
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:

//useUnicode表示允许使用自定义的Unicode,
//characterEncoding是给定自定义的Unicode是什么
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