本帖最后由 CrazyProgram 于 2013-5-4 18:36 编辑
- class MyRequest{
- private HttpServletRequest request;
- public MyRequest(HttpServletRequest request){
- this.request = request;
- }
- public HttpServletRequest getProxy(){
- return (HttpServletRequest) Proxy.newProxyInstance(
- MyRequest.class.getClassLoader(),
- request.getClass().getInterfaces(),
- new InvocationHandler() {
-
- @Override
- public Object invoke(
- Object proxy,
- Method method,
- Object[] args)throws Throwable {
- if("getParameter".equals(method.getName())){
- String m = request.getMethod();
- if("post".equalsIgnoreCase(m)){
- request.setCharacterEncoding("UTF-8");
- String value = request.getParameter((String)args[0]);//正码
- return value;//为什么返回的是value而不是return method.invoke(request, args);
- }else if("get".equalsIgnoreCase(m)){
- String value = request.getParameter((String)args[0]);//乱码
- byte[] buf = value.getBytes("ISO8859-1");
- value = new String(buf,"UTF-8");//正码
- return value;//为什么返回的是value而不是return method.invoke(request, args);
- }
- }else{
- return method.invoke(request, args);
- }
- return null;
- }
- });
- }
- }
复制代码 |