jive中文问题解决方案

03-08-20 袁新宇
jive中文解决方案

1、将所有的jsp页面中的charset由ISO-8859-1改为GBK。

2、在web.xml中加入如下的filter:

<filter>

<filter-name>Set Character Encoding</filter-name>

<filter-class>SetCharacterEncodingFilter</filter-class>

<init-param>

<param-name>encoding</param-name>

<param-value>GBK</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>Set Character Encoding</filter-name>

<servlet-name>jsp</servlet-name>

</filter-mapping>

该类的源文件见附件。

3、在中文环境下重新编译src下面的所有的java文件。

上面三步完成后,基本上就可以输入和显示中文了。

4、在根目录下的header.jsp中加入<%@ page contentType="text/html;charset=GBK" %>

5、将所有页面中的<%@ include file="header.jsp" %>改为<jsp:include page="header.jsp" />

这两步解决头文件的中文问题。

6、修改com.jdon.picture.Upload.java,将36行中的MultipartRequest(request,uploaddir,size * 1024)改为MultipartRequest(request,uploaddir,size * 1024,"GBK")

7、修改upload.jsp,在String icon=request.getParameter(request.getParameter("picname"))后面增加下面这一行

icon=(String)session.getAttribute("picname")+icon.substring(icon.lastIndexOf('.'));

在<input type="hidden" name="picname" value="<%=picname%>">后面增加下面这一行

<%session.setAttribute("picname",picname);%>

这两步解决图片上传时的中文问题。

以上在windows2000+jdk1.4+tomcat4.18+oracle8下测试通过。

下面是SetCharacterEncodingFilter.java的源码

import javax.servlet.Filter;

import javax.servlet.FilterConfig;

import javax.servlet.ServletException;

import javax.servlet.ServletRequest;

import javax.servlet.ServletResponse;

import javax.servlet.FilterChain;

import java.io.IOException;

import javax.servlet.UnavailableException;

public class SetCharacterEncodingFilter implements javax.servlet.Filter {

protected String encoding = "GBK";

protected FilterConfig filterConfig = null;

public void setFilterConfig(FilterConfig config) {

this.filterConfig = config;

}

public FilterConfig getFilterConfig() {

return filterConfig;

}

public void destroy() {

this.encoding = null;

this.filterConfig = null;

}

public void doFilter(ServletRequest request, ServletResponse response,

FilterChain chain)

throws IOException, ServletException {

// Select and set (if needed) the character encoding to be used

String encoding = selectEncoding(request);

if (encoding != null) {

request.setCharacterEncoding(encoding);

System.out.println("encoding:"+encoding);

}

// Pass control on to the next filter

chain.doFilter(request, response);

}

public void init(FilterConfig filterConfig) throws ServletException {

this.filterConfig = filterConfig;

this.encoding = filterConfig.getInitParameter("encoding");

System.out.println("³õʼ»¯encoding...");

}

protected String selectEncoding(ServletRequest request) {

return (this.encoding);

}

}

袁新宇
2003-08-20 17:36
html的敏感字符被过滤了,查看源文件可以看到这些内容。

袁新宇
2003-08-20 19:30
linux 下也测试通过。

操作系统:debian2.4.18

数据库:oracle8.1.7

jdk1.4

tomcat4.1.24

环境变量LANG=zh_CN.GBK

观看效果

zhuojun
2003-08-21 09:28
袁兄:一些字符被过滤了,不太好理解,能否将解决方案发给我?

jsjs666@sina.com

多谢!

cjfeng
2003-08-21 10:53
搜索时候的中文关键字问题如何解决?

现在搜索时输入中文后,就变成乱码

ylt
2003-08-21 12:26

由于现在的j道不支持中文上传,我重新申请了一个账号。

参照我上面提出的前三步,搜索时的中文问题就自动解决了。

ylt
2003-08-21 12:37
> 袁兄:一些字符被过滤了,不太好理解,能否将解决方案发给

> 遥?> jsjs666@sina.com

邮件已发送,请查收。下面是解决方案:

yltV40F7n346t.txt

ylt
2003-08-21 12:48
由于我是在装完jive后才着手解决中文问题的,所以发现admin/setup目录下的那三个setup的jsp文件还是存在中文问题。不过这些文件论坛用户是看不到的,所以我也不想再管它了。有兴趣者可以试试。

dreameng
2003-09-02 10:21
过滤功能对中文还是不起作用啊,怎么办?

j4v4
2003-09-05 09:16
不幸的是,我加了filter也不能正确显示中文。

mouseadapter
2003-12-08 09:21

5、将所有页面中的<%@ include file="header.jsp" %>改为<jsp:include page="header.jsp" />
<p>

这样改,参数title就不能传给header.jsp了,请问是怎么解决的?

togetherj
2003-12-29 15:48
可靠吗?我也试一试^_^

alluser
2003-12-29 23:41
bu

alluser
2003-12-29 23:44
可以吗

cats_tiger
2003-12-30 10:31
Jive中文问题解决方案

环境:

Win2000 server sp4

Oracle8i 简体中文字符集

Tomcat5.0.16

中文问题的原因:

数据库字符集不相符

方案1:

修改数据库字符集为8859_1-没有试过,估计不行,而且会影响以前的数据

方案2:

用JBuilder打开Jive(Jdon版),重建每一个jsp,使“文件本身”的编码改为GBK,

然后修改所有charset=ISO8859_1为charset=GBK

这个方案太麻烦太老土

方案3:

1.加入过滤器,使得提交到数据库中的字符编码正确

package com.jivesoftware.encoding;

import javax.servlet.Filter;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.FilterChain;
import java.io.IOException;

public class EncodingFilter
  implements Filter {
  private String encoding = "GBK";
  public void init(FilterConfig filterConfig) throws ServletException {
    encoding = filterConfig.getInitParameter("encoding");
  }

  public void doFilter(ServletRequest request,
                       ServletResponse response,
                       FilterChain chain) throws IOException, ServletException {
    request.setCharacterEncoding(encoding);
    chain.doFilter(request, response);
  }

  public void destroy() {
  }
}
/*
 web.xml:
 <filter>
    <filter-name>EncodingFilter</filter-name>
    <filter-class>com.jivesoftware.encoding.EncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>GBK</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>EncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
*/
<p>

2.编写一个类,对输入参数重新编码

package com.jivesoftware.encoding.util;

import com.jivesoftware.forum.*;

public class StringEncoding {
  public static final String oraEncoding =
    JiveGlobals.getJiveProperty("encoding.orgencoding");
  public static final String encoding =
    JiveGlobals.getJiveProperty("encoding.toencoding");

  public static String reEncoding(String param) {
    String res = param;

    try {
      //res = new String(
      //  param.getBytes(JiveGlobals.getJiveProperty("encoding.orgencoding")),
      //  JiveGlobals.getJiveProperty("encoding.toencoding"));
      res = new String(param.getBytes(oraEncoding), encoding);
    }
    catch (Exception e) {
      res = param;

    }

    return res;
  }

  public static boolean reEncoding(boolean param) {
    return param;
  }

  public static long reEncoding(long param) {
    return param;
  }

  public static int reEncoding(int param) {
    return param;
  }

  public static char reEncoding(char param) {
    return param;
  }

  public static double reEncoding(double param) {
    return param;
  }

  public static java.util.Date reEncoding(java.util.Date param) {
    return param;
  }

  public static Object reEncoding(Object param) {
    return (Object) reEncoding(param.toString());
  }

  public static byte reEncoding(byte param) {
    return param;
  }

}
<p>

2.编写一个类,修改所有jsp页面<%=%>的部分,利用上述StringEncoding类修改输出的编码

package com.jivesoftware.encoding.util;

import java.io.*;
import java.util.*;
import com.jivesoftware.util.StringUtils;

public class JspReOut {

  private Iterator itr;
  private String path;
  private String orgEncoding;
  private String encoding;
  private String header;
  private String footer;
  //Constructor
  public JspReOut(String path, String orgEncoding, String encoding) {
    this.path = path;
    this.orgEncoding = orgEncoding;
    this.encoding = encoding;
    this.header = "com.jivesoftware.encoding.util.StringEncoding.reEncoding(";
    this.footer = ")";
  }
  /**
   * Get all files of the directory
   */
  private Iterator fileList() {
    File jspFile = new File(path);
    File[] files = jspFile.listFiles();
    int count = files.length;
    List list = new ArrayList(count);

    for (int i = 0; i < count; i++) {
      if (!files[i].isDirectory()) {
        list.add(files[i]);
      }
    }

    return list.iterator();
  }
  
  private String reOut(String orgStr){
    StringBuffer buf = new StringBuffer(this.header);
    buf.append(orgStr);
    buf.append(this.footer);

    return buf.toString();
  }

  private String recoverStr(String orgStr){
    String res = orgStr;
    res = StringUtils.replace(res, this.header, "");
    StringBuffer  buf = new StringBuffer(res);
    buf.deleteCharAt(buf.lastIndexOf(")"));

    return buf.toString();
  }
 
  private void replace(File file, boolean isRecover) throws IOException{
    BufferedReader reader = new BufferedReader(new FileReader(file));
    int fLen = (int) file.length();
    char []buf = new char[fLen + 100];
    reader.read(buf);
    StringBuffer buffer = new StringBuffer(fLen*2);
    
    for( int i = 0; i< fLen + 100; i ++){
      //如果是输出块<%=xxx%>
      if( buf[i] == '<' &&
          buf[i+1] =='%' &&
          buf[i+2] == '='){
        //找到输出块之间的数据
        String sec = this.getOutputSection( buf, i+3);
        int len = sec.length();
        //System.out.println("*"+sec+"*");
        i = i + 2 + len + 2;//调整索引的位置
        if(isRecover){
          sec = this.recoverStr(sec);
        }
        else{
          sec = this.reOut(sec);
        }
        
        buffer.append("<%= ");
        buffer.append(sec);
        buffer.append(" %>");
      }
      else{//如果不是输出块
        buffer.append(buf[i]);
      }
    }
    reader.close();
    //System.out.println(buffer.toString());
    BufferedWriter writer = new BufferedWriter(new FileWriter(file));
    int newFLen = buffer.length();
    for( int i = 0; i < newFLen; i ++){
     writer.write(buffer.charAt(i));
    }
    writer.flush();
    writer.close();
  }
  
  private String getOutputSection(char[] org, int start){
    int nLen = org.length;
    StringBuffer buf = new StringBuffer("");

    for( int i = start; i < nLen; i ++){
      if(org[i] == '%' && org[i+1] == '>'){
        break;
      }
      buf.append(org[i]);
    }

    return  buf.toString();
  }
  
  public void replace(boolean isRecover){
    Iterator itr = this.fileList();
    while(itr.hasNext()){
      File file = (File)itr.next();
      try{
        this.replace(file, isRecover);
      }
      catch(IOException e){
        e.printStackTrace();
      }
    }
  }
  //TEST
  public static void main(String[] args) throws IOException {
    JspReOut jro = new JspReOut("E:\\jivejdon-2.5\\jivejdon\\application",
                                "GBK",
                                "ISO8859_1");
    /*Iterator itr = jro.fileList();
    while(itr.hasNext()){
      System.out.println(((File)itr.next()).getName());
    }*/
    //System.out.println(jro.reOut("aaa"));
    //System.out.println(jro.recoverStr(jro.reOut("aaa")));
    //jro.replace(new File("E:\\jivejdon-2.5\\jivejdon\\application\\admin\\skin.jsp"),false);
    //jro.replace(new File("E:\\jivejdon-2.5\\jivejdon\\application\\admin\\locale.jsp"),false);
    //jro.replace(new File("E:\\jivejdon-2.5\\jivejdon\\application\\admin\\forums.jsp"),false);
    //jro.replace(new File("E:\\jivejdon-2.5\\jivejdon\\application\\admin\\sidebar.jsp"),false);
    //jro.replace(new File("E:\\jivejdon-2.5\\jivejdon\\application\\admin\\editForum.jsp"),false);
    //jro.replace(new File("E:\\jivejdon-2.5\\jivejdon\\application\\admin\\removeForum.jsp"),false);
    //jro.replace(new File("E:\\jivejdon-2.5\\jivejdon\\application\\admin\\editUser.jsp"),false);
    //jro.replace(new File("E:\\jivejdon-2.5\\jivejdon\\application\\admin\\userSearch.jsp"),false);
    jro.replace(new File("E:\\jivejdon-2.5\\jivejdon\\application\\uploadfile.jsp"),false);
    //jro.replace(false);


  }
}
<p>

我测试了,在JBuilder9下运行OK

猜你喜欢
2Go 1 2 下一页