1、设置 HTML 页面编码格式为 UTF-8
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
2、设置 HTML 页面编码格式为中文
<meta http-equiv="content-language" content="zh-CN" />
3、设置 JSP 页面编码格式为 UTF-8
<%@ page contentType="text/html;charset=UTF-8" %>
/**
* 生成HTMl文件
*/
BufferedReader in = null;
URL url1 = null;
URLConnection urlConn = null;
try {
//http://localhost:8080/pertest/admin.go?method=showReportToCustomer&license=
String strURL = "http://localhost:8080/t/admin.go?method=showReportToCustomer&license="
+ user.getT_license()
+ "&title="
+ URLEncoder.encode(user.getName(), "utf-8");
url1 = new URL(strURL);
urlConn = url1.openConnection();// 建立http连接
in = new BufferedReader(new InputStreamReader(urlConn
.getInputStream(), "UTF-8"));// 设置Encoding,必须设置,否则显示中文会有问题
} catch (IOException e) {
log.error(e.getMessage(), e);
} finally {
urlConn = null;
url1 = null;
}
BufferedWriter out = null;
try {
//服务器
File htmlFile = new File("E:/perTest/UserMsg/"+user.getName()+user.getT_license()+".html");
//本机 "E:/temp/perTest/UserMsg/"+user.getName()+user.getT_license()+".html");
htmlFile.createNewFile();
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(htmlFile), "UTF-8"));// 设置Encoding
// 写HTMl文件
while (in.ready()) {
out.write(in.readLine());
}
} catch (IOException e) {
log.error(e.getMessage(), e);
} finally {
try {
in.close();
out.close();
} catch (IOException e) {
log.error(e.getMessage(), e);
}
}
解决乱码,原因是没有设置html编码格式
1、设置 HTML 页面编码格式为 UTF-8
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
2、设置 HTML 页面编码格式为中文
<meta http-equiv="content-language" content="zh-CN" />
3、设置 JSP 页面编码格式为 UTF-8
<%@ page contentType="text/html;charset=UTF-8" %>