服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

解决中文问题的几个常用的函数


  解决java中文问题:
针对applet和awt:
1:)
font f = new font(uiresource.getstring( "default_font"),font.plain,12);
uimanager.put("label.font",f);
uimanager.put("label.foreground",color.black);
uimanager.put("button.font",f);
uimanager.put("menu.font",f);
uimanager.put("menuitem.font",f);
uimanager.put("list.font",f);
uimanager.put("checkbox.font",f);
uimanager.put("radiobutton.font",f);
uimanager.put("combobox.font",f);
uimanager.put("textarea.font",f);


2:)
font f = new font("隶书",font.plain,15);
uimanager.put("button.font",font);
uimanager.put("togglebutton.font",font);
uimanager.put("radiobutton.font",font);
uimanager.put("checkbox.font",font);
uimanager.put("colorchooser.font",font);
uimanager.put("togglebutton.font",font);
uimanager.put("combobox.font",font);
uimanager.put("comboboxitem.font",font);
uimanager.put("internalframe.titlefont",font);
uimanager.put("label.font",font);
uimanager.put("list.font",font);
uimanager.put("menubar.font",font);
uimanager.put("menu.font",font);
uimanager.put("menuitem.font",font);
uimanager.put("radiobuttonmenuitem.font",font);
uimanager.put("checkboxmenuitem.font",font);
uimanager.put("popupmenu.font",font);
uimanager.put("optionpane.font",font);
uimanager.put("panel.font",font);
uimanager.put("progressbar.font",font);
uimanager.put("scrollpane.font",font);
uimanager.put("viewport",font);
uimanager.put("tabbedpane.font",font);
uimanager.put("tableheader.font",font);
uimanager.put("textfield.font",font);
uimanager.put("passwordfiled.font",font);
uimanager.put("textarea.font",font);
uimanager.put("textpane.font",font);
uimanager.put("editorpane.font",font);
uimanager.put("titledborder.font",font);
uimanager.put("toolbar.font",font);
uimanager.put("tooltip.font",font);
uimanager.put("tree.font",font);

 3:)针对jsp和servlet:
解决办法:
第一:
在jsp页面加入:
<%@ page contenttype="text/html; charset=gb2312" %>
或者在servlet里面
public void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
response.setcontenttype("text/html; charset=gb2312");//这是重要的
上面的如果在不行就用如下的方法在数据入库前进行调用:
public static string unicodetochinese(string s){
try{
if(s==null||s.equals("")) return "";
string newstring=null;
newstring=new string(s.getbytes("iso8859_1"),"gb2312");
return newstring;
}
catch(unsupportedencodingexception e)
{
return s;
}
}

public static string chinesetounicode(string s){
try{
if(s==null||s.equals("")) return "";
string newstring=null;
newstring=new string(s.getbytes("gb2312"),"iso8859_1");
return newstring;
}
catch(unsupportedencodingexception e)
{
return s;
}
}

3:)解决weblogic/webshpere中文问题:
在web.xml文件中需要配置中文环境。r如下:
<context-param>
<param-name>weblogic.httpd.inputcharset./*</param-name>
<param-value>gb2312</param-value>
</context-param>
  4:)javamail附件中文乱码:
/*
@从bodypart中提取使用iso-8859-1编吗的文件名
@因为bodypart.getfilename()过程已经对文件名作了一次编码,有时不能直接使用
*/
public static string getisofilename(part body){
//设置一个标志,判断文件名从content-disposition中获取还是从content-type中获取
boolean flag=true;
if(body==null){
return null;
}
string[] cdis;
try{
cdis=body.getheader("content-disposition");
}
catch(exception e){
return null;
}
if(cdis==null){
flag=false;
}
if(!flag){
try{
cdis=body.getheader("content-type");
}
catch(exception e){
return null;
}
}
if(cdis==null){
return null;
}
if(cdis[0]==null){
return null;
}
//从content-disposition中获取文件名
if(flag){
int pos=cdis[0].indexof("filename=");
if(pos<0){
return null;
}
//如果文件名带引号
if(cdis[0].charat(cdis[0].length()-1)==´"´){
return cdis[0].substring(pos+10,cdis[0].length()-1);
}
return cdis[0].substring(pos+9,cdis[0].length());
}
else{
int pos=cdis[0].indexof("name=");
if(pos<0){
return null;
}
//如果文件名带引号
if(cdis[0].charat(cdis[0].length()-1)==´"´){
return cdis[0].substring(pos+6,cdis[0].length()-1);
}
return cdis[0].substring(pos+5,cdis[0].length());
}
}
8:字符串分割:
    public int getcount(string str,string sign){//查找某一字符串中str,特定子串s的出现次数
if(str==null) return 0;
stringtokenizer s=new stringtokenizer(str,sign);
return s.counttokens();
}
 public string[] getarray(string str,string sign){//按特定子串s为标记,将子串截成数组。
int count=getcount(str,sign);
int j=0;
string[] arr=new string[count];
for(int i=0;i<count;i++){
if(str.indexof(sign)!=-1){
j =str.indexof(sign);
arr[i]=str.substring(0,j);
str =str.substring(j+1);
}else{
arr[i]=str;
}
}
return arr;

}
9:jdk1.3没有字符串替换函数,(jdk1.4有)。
解决1.3中的个这个问题如下:

public string stringreplace(string sourcestring, string toreplacestring, string replacestring)
{
string returnstring = sourcestring;
int stringlength = 0;
if(toreplacestring != null)
{
stringlength = toreplacestring.length();
}
if(returnstring != null && returnstring.length() > stringlength)
{
int max = 0;
string s4 = "";
for(int i = 0; i < sourcestring.length(); i++)
{
max = i + toreplacestring.length() > sourcestring.length()? sourcestring.length():i + stringlength;
string s3 = sourcestring.substring(i, max);
if(!s3.equals(toreplacestring))
{
s4 += s3.substring(0,1);
}else{
s4 += replacestring;
i += stringlength -1 ;
}
}
returnstring = s4;
}
return returnstring;
}
9:设置weblogic连接池:

pool的配置:
假设已配置服务:expserv
且数据库服务器机器名为:expserv
数据库sid:expservsid,用户名和密码都为:expserv
以weblogic7.0为例,首先启动服务
http://localhost:port/console
打开servicejdbcconnection pools
配置oraclepool如下:
configuration:
name: oraclepool
url: jdbc:oracle:thin:@expserv:1521:expservsid
driver classname: oracle.jdbc.driver.oracledriver
properties(key=value): user=expserv
targets:
targets-server:expserv
在weblogic7.0中除了数据库密码,其他的pool参数都可以在config.xml中直接用文本编辑器直接修改。

扫描关注微信公众号