connectionTimeout="20000" URIEncoding="UTF-8"/>
需要通过filter过滤器设置:response.setContentType("text/html;charset=UTF-8");
不能这样设置:
response.setCharacterEncoding(“UTF-8”);
读取.txt文本文件时也使用UTF-8编码:BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(fileName),"UTF-8")); FileOutputStream writerStream = new FileOutputStream(filePath, true); BufferedWriter oWriter = new BufferedWriter(new OutputStreamWriter(writerStream, "UTF-8")); .txt文件也要是以UTF-8编码格式保存的才行,以其他编码格式保存也会是乱码。
BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(fileName),"UTF-8"));
- FileOutputStream writerStream = new FileOutputStream(filePath, true);
- BufferedWriter oWriter = new BufferedWriter(new OutputStreamWriter(writerStream, "UTF-8"));
在Tomcat下conf文件夹下server.xml
找
这个各个机子不一样~
主要是在后面加disableUploadTimeout="true" useBodyEncodingForURI="true" URIEncoding="gbk"
- <Connector port="8080" protocol="HTTP/1.1"
- connectionTimeout="20000"
- redirectPort="8443"
- useBodyEncodingForURI="true"
- IEncoding="utf-8"
- URIEncoding="utf-8"/>
在apache设置
AddDefaultCharset off
AddDefaultCharset utf-8
在apache均出现乱码,
因此,Apache和Tomcat集成后,要使服务器支持UTF-8的中文地址,应该设置8009端口的标签
最后的方法在tomcat的server.conf里面设置apache和tomcat的连接为utf-8
设置如下:
useBodyEncodingForURI参数表示是否用request.setCharacterEncoding 参数对URL提交的数据和表单中GET方式提交的数据进行重新编码,在默认情况下,该参数为false。
URIEncoding参数指定对所有GET方式请求进行统一的重新编码(解码)的编码。
URIEncoding和useBodyEncodingForURI区别是,
URIEncoding是对所有GET方式的请求的数据进行统一的重新编码,
而useBodyEncodingForURI则是根据响应该请求的页面的request.setCharacterEncoding参数对数据进行的重新编码,不同的页面可以有不同的重新编码的编码
(1)HttpServletRequest.setCharacterEncoding()方法 仅仅只适用于设置post提交的requestboda的编码而不是设置get方法提交的queryString的编码。该方法告诉应用服务器应该采用什么编码解析post传过来的内容。
(2) HttpServletRequest.getPathInfo()返回的结果是由Servlet服务器解码(decode)过的。
(3) HttpServletRequest.getRequestURI()返回的字符串没有被Servlet服务器decoded过。
(1)HttpServletRequest.setCharacterEncoding()方法 仅仅只适用于设置post提交的requestboda的编码而不是设置get方法提交的queryString的编码。该方法告诉应用服务器应该采用什么编码解析post传过来的内容。
(2) HttpServletRequest.getPathInfo()返回的结果是由Servlet服务器解码(decode)过的。
(3) HttpServletRequest.getRequestURI()返回的字符串没有被Servlet服务器decoded过。
package com.gtxy.dynaActionForm.web.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 以 UTF-8 输入输出
*
* @author ZhongHuiQiang
*
*/
public
class FilterChar
implements Filter {
private String config;
public
void destroy() {
this.config =
null;
}
/**
* name="FilterChar"
* init-param name="config" value="utf-8"
* mapping url-pattern="/*"
*/
public
void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throwsIOException, ServletException {
HttpServletRequest req;
req = (HttpServletRequest) request;
HttpServletResponse res;
res = (HttpServletResponse) response;
req.setCharacterEncoding(config);
//res.setCharacterEncoding(config);
res.setContentType("text/html;charset=UTF-8");
chain.doFilter(req, res);
}
/**
* 获得web.xml中初始化的参数
*/
public
void init(FilterConfig config)
throws ServletException {
this.config = config.getInitParameter(
"chars");
}
}
OK,FilterChar类写完后,就要配置web.xml了!
在web.xml里面,最主要是下面这段代码
<
filter
>
<
filter-name
>FilterChar
filter-name
>
<
filter-class
>
com.gtxy.dynaActionForm.web.filter.FilterChar
filter-class
>
<
init-param
>
<
param-name
>chars
param-name
>
<
param-value
>utf-8
param-value
>
init-param
>
filter
>
<
filter-mapping
>
<
filter-name
>FilterChar
filter-name
>
<
url-pattern
>/*
url-pattern
>
filter-mapping
>
简单配置步骤:
1.在项目web.xml文件添加过滤器标记和;
2.实现过滤器代码;
3.对Tomcat服务器conf目录里的Server.xml文件配置URIEncoding;
4.前台页面设置contentType的charset值与web.xml里设置的值一致。
详细配置步骤:
1.配置web.xm文件,在web-app标签内添加过滤器的配置和
-
- <filter>
- <filter-name>EncodingFilterfilter-name>
- <filter-class>com.filters.SetCharacterEncodingFilterfilter-class>
- <init-param>
- <param-name>encodingparam-name>
- <param-value>GBKparam-value>
- init-param>
- <init-param>
- <param-name>ignoreparam-name>
- <param-value>trueparam-value>
- init-param>
- filter>
- <filter-mapping>
- <filter-name>EncodingFilterfilter-name>
- <url-pattern>/*url-pattern>
- filter-mapping>
-
2.实现过滤器代码,创建一个SetCharacterEncodingFilter类
- package com.filters;
-
- import java.io.IOException;
-
- import javax.servlet.Filter;
- import javax.servlet.FilterChain;
- import javax.servlet.FilterConfig;
- import javax.servlet.ServletException;
- import javax.servlet.ServletRequest;
- import javax.servlet.ServletResponse;
-
-
-
-
-
-
-
- public class SetCharacterEncodingFilter implements Filter {
-
- protected FilterConfig filterConfig = null;
- protected String encoding = null;
- protected boolean ignore = true;
-
-
-
-
- @Override
- public void destroy() {
- this.encoding = null;
- this.filterConfig = null;
- }
-
-
-
-
- @Override
- public void doFilter(ServletRequest request, ServletResponse response,
- FilterChain chain) throws IOException, ServletException {
-
- if (ignore || (request.getCharacterEncoding() == null)) {
- String encoding = selectEncoding(request);
- if (encoding != null){
- request.setCharacterEncoding(encoding);
-
response.setContentType("text/html;charset="+encoding);
- }
- }
- chain.doFilter(request, response);
- }
-
-
-
-
- @Override
- public void init(FilterConfig filterConfig) throws ServletException {
- this.filterConfig = filterConfig;
- this.encoding = filterConfig.getInitParameter("encoding");
- String value = filterConfig.getInitParameter("ignore");
-
- if (value == null)
- this.ignore = true;
- else if (value.equalsIgnoreCase("true"))
- this.ignore = true;
- else if (value.equalsIgnoreCase("yes"))
- this.ignore = true;
- else
- this.ignore = false;
- }
-
-
-
-
-
-
- protected String selectEncoding(ServletRequest request) {
- return (this.encoding);
- }
- }
3.修改Tomcat服务器conf\Server.xml文件,加入一个URIEncoding="GBK"
-
- <Connector port="8080" protocol="HTTP/1.1"
- connectionTimeout="20000"
- redirectPort="8443"
- URIEncoding="GBK"/>
4.设置前台页面字符编码集
- <%@ page contentType="text/html;charset=GBK" %>
//此处的charset的值要和web.xml里的 值一样
算圆上的一点的坐标
圆心(0,0) 半径1
已知圆上某点坐标 x y
延圆心旋转θ后
新的坐标是多少?
解:设原来点(x,y)对应的角为α,则x=cosα,y=sinα.
设所求的新坐标为(m,n),对应的角应为α-θ,所以m=cos(α-θ),n=sin(α-θ)
展开得m=cos(α-θ)=cosαcosθ+sinαsinθ=xcosθ+ysinθ
n=sin(α-θ)=sinαcosθ-cosαsinθ=ycosθ-xsinθ
所以所求的坐标为(xcosθ+ysinθ,ycosθ-xsinθ)