我用springmvc开发的微信公众平台,但是这几天自动回复乱码了求高手解决啊

真J 发布于 2014/12/03 15:07
阅读 4K+
收藏 0

/下面是我的部分代码,前几天还好着呢,不理解啊,,我转了好多码都不行



/ 验证url

@RequestMapping("/url")
public String url(HttpServletResponse response,
HttpServletRequest request) throws Exception {

request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
System.out.println("----");
//验证
String echostr = request.getParameter("echostr");
        String sb = "";
//验证请求是否来自微信服务器
if (XmlUtil.checkSignature(request,cs.getConfig().getToken())) {
if(!"".equals(echostr) && null!=echostr){
sb = echostr;
}else{
Map<String, String> map = XmlUtil.getXml(request);
sb = ws.ControlSkip(map);

}
}

System.out.println("微信接收"+sb);
System.out.println("111111111111");
out.print("<script language='javascript>"); 
out.print("alert(sb);"); 
out.print("</scrpit>");
out.print(sb);
out.print("daw");
out.flush();
out.close();

return null;

public String ControlSkip(Map<String, String> map) throws Exception {
// TODO Auto-generated method stub

Config config = cs.getConfig();
System.out.println("用户发送xml"+map);

String Msgtype = map.get("xml.MsgType").toString();

StringBuffer sb = new StringBuffer();



if("text".equals(Msgtype)){


//执行天气预报
if(map.get("xml.Content").contains("天气") && StringUtils.isNotBlank(map.get("xml.Content").replace("天气", ""))){

sb.append(qs.getWeather(map));
}else{
//执行字符串处理方法
sb.append(qs.reply(map,""));
}

}else if("location".equals(Msgtype)){
//执行地址处理方法
List<News> news = ds.getNewsInfo(map.get("xml.Location_X"),map.get("xml.Location_Y"));

String xmlstr = ConvertXml.getNewsXml(map.get("xml.FromUserName"), map.get("xml.ToUserName"), map.get("xml.CreateTime"), news.size()+"", news,config.getUrl());
sb.append(xmlstr);

}else if("voice".equals(Msgtype)){
//执行语音处理方法
String xmlstr = ConvertXml.getTextXml(map.get("xml.FromUserName"), map.get("xml.ToUserName"), map.get("xml.CreateTime"), "对不起,语音功能还在开发中,敬请期待", map.get("xml.MsgId"));
sb.append(xmlstr);
}else if("event".equals(Msgtype)){
String event = map.get("xml.Event").toString();

if("subscribe".equals(event)){

//执行关注处理方法
sb.append(ss.SubscribeR(map));
}
if("CLICK".equals(event)){
//自定义菜单点击事件
sb.append(ws.findInfoByClick(map));

}

}

return sb.toString();
}

@Override
public String SubscribeR(Map<String, String> map){

// TODO Auto-generated method stub
Config config = cs.getConfig();
Subscribe sub = this.findById(1);
   String xmlstr="";
    if(0 == sub.getReplyType()){
xmlstr = ConvertXml.getTextXml(map.get("xml.FromUserName"), map.get("xml.ToUserName"), map.get("xml.CreateTime"),sub.getContent(), map.get("xml.MsgId"));
         }else if(1 == sub.getReplyType()){

List<News> lst = new ArrayList<News>();
News news = new News();
news.setPicUrl(config.getUrl()+"/upload/subscribe/"+sub.getImageUrl());
news.setTitle(sub.getImageTitle());
news.setContext(sub.getJcontent());
news.setSource("subscribe");
news.setUrl(config.getUrl());
lst.add(news);
  
xmlstr = ConvertXml.getNewsXml(map.get("xml.FromUserName"), map.get("xml.ToUserName"), map.get("xml.CreateTime"), lst.size()+"", lst,config.getUrl());
}


return xmlstr;
}


加载中
0
八一菜刀
八一菜刀

web.xml里面配置url编码试过么

<filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>



配置了后就不需要设置编码了吧。。java里面

0
真J
真J

引用来自“八一菜刀”的评论

web.xml里面配置url编码试过么

<filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>



配置了后就不需要设置编码了吧。。java里面

恩是的,我以前也好着呢,,不知道突然怎么了。我也配置的呢你看

<!-- 编码过滤器 -->
<filter>
<filter-name>Spring character encoding filter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Spring character encoding filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

悲剧了,接收消息好着呢,自动回复就乱码了,。你看图片

0
八一菜刀
八一菜刀

 我一般统一设置的.

用的@ResponseBody这个注解

@RequestMapping("/url") 
@ResponseBody
public String url(HttpServletResponse response, 
HttpServletRequest request) {

    return "中文";

}


然后再spring.xml里面配置编码

<bean
		class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
		<property name="messageConverters">
			<list>
				<bean
					class="org.springframework.http.converter.StringHttpMessageConverter">
					<property name="supportedMediaTypes">
						<list>
							<value>text/html;charset=UTF-8</value>
						</list>
					</property>
				</bean>
			</list>
		</property>
	</bean>



你可以试下

0
真J
真J

引用来自“八一菜刀”的评论

 我一般统一设置的.

用的@ResponseBody这个注解

@RequestMapping("/url") 
@ResponseBody
public String url(HttpServletResponse response, 
HttpServletRequest request) {

    return "中文";

}


然后再spring.xml里面配置编码

<bean
		class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
		<property name="messageConverters">
			<list>
				<bean
					class="org.springframework.http.converter.StringHttpMessageConverter">
					<property name="supportedMediaTypes">
						<list>
							<value>text/html;charset=UTF-8</value>
						</list>
					</property>
				</bean>
			</list>
		</property>
	</bean>



你可以试下

非常感谢我先试试
0
心_行
心_行
tomcat/conf/server.xml  URIEncoding="UTF-8"
kidbei
kidbei
这是对请求进行转码的,而且只针对get请求
0
真J
真J

引用来自“心and行”的评论

tomcat/conf/server.xml  URIEncoding="UTF-8"
不行我就是这样弄的悲剧了<Connector port="8080" protocol="HTTP/1.1" 
               connectionTimeout="20000" 
               redirectPort="8443"
  1.    maxThreads="1000"   
          minSpareThreads="20"  
        acceptCount="100" 
        enableLookups="false"   
        URIEncoding="UTF-8" 
disableUploadTimeout="true" 
             compression="on" 
          compressionMinSize="2048" 
noCompressionUserAgents="gozilla,traviata"              
compressableMimeType="text/html,text/xml,text/javascript,text/css,text/plain" />
   
0
真J
真J

引用来自“八一菜刀”的评论

 我一般统一设置的.

用的@ResponseBody这个注解

@RequestMapping("/url") 
@ResponseBody
public String url(HttpServletResponse response, 
HttpServletRequest request) {

    return "中文";

}


然后再spring.xml里面配置编码

<bean
		class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
		<property name="messageConverters">
			<list>
				<bean
					class="org.springframework.http.converter.StringHttpMessageConverter">
					<property name="supportedMediaTypes">
						<list>
							<value>text/html;charset=UTF-8</value>
						</list>
					</property>
				</bean>
			</list>
		</property>
	</bean>



你可以试下

引用来自“wangbing123”的评论

非常感谢我先试试
不行悲剧了,,,不知道怎么了,,,我怀疑编码格式是不是UTF-8了
0
H
HelloFire
楼主求问乱码问题怎么解决的
0
r
ronny
楼主问题解决没?我也遇到这个问题了
0
小帅帅丶
小帅帅丶
你的tomcat那些也修改了编码格式了吗?
返回顶部
顶部