才子佳人博客

我的故事我讲述

jsp网站后台程序迁移服务器后乱码问题解决办法
 
来源:xjh  编辑:xjh  2010-12-13

虚拟主机环境:linux+jsp+tomcat+mysql

问题描述:jsp网站后台程序在Windows平台下一切正常,但迁移上传到服务器(虚拟主机),后台程序处理生成的html页面发生乱码。

原因分析:程序中使用了String(byte[]bytes)函数,该函数使用服务器默认字符集。

解决办法:更换构造函数为:String(byte[]bytes,StringcharsetName)明确指定字符集。

程序部分代码:

String templateContent = "";

FileInputStream fileinputstream = new FileInputStream(filePath);

int lenght = fileinputstream.available();

byte bytes[] = new byte[lenght];

fileinputstream.read(bytes);

fileinputstream.close();
templateContent = new String(bytes,"UTF-8");//指定具体字符集为UTF-8

jdk文档详述:
public String(byte[]bytes)
Constructs a new String by decoding the specified array of bytes using the platforms default charset. The length of the new String is a function of the charset, and hence may not be equal to the length of the byte array.

The behavior of this constructor when the given bytes are not valid in the default charset is unspecified. The CharsetDecoder class should be used when more control over the decoding process is required.

Parameters:
bytes - the bytes to be decoded into characters
public String(byte[]bytes,
StringcharsetName)
throws UnsupportedEncodingException
Constructs a new String by decoding the specified array of bytes using the specified charset. The length of the new String is a function of the charset, and hence may not be equal to the length of the byte array.

The behavior of this constructor when the given bytes are not valid in the given charset is unspecified. The CharsetDecoder class should be used when more control over the decoding process is required.

Parameters:
bytes - the bytes to be decoded into characters
charsetName - the name of a supported charset

Standard charsets

Every implementation of the Java platform is required to support the following standard charsets. Consult the release documentation for your implementation to see if any other charsets are supported. The behavior of such optional charsets may differ between implementations.

Charset Description


US-ASCII Seven-bit ASCII, a.k.a. ISO646-US, a.k.a. the Basic Latin block of the Unicode character set
ISO-8859-1 ISO Latin Alphabet No. 1, a.k.a. ISO-LATIN-1
UTF-8 Eight-bit UCS Transformation Format

参考:JDKDocumentation


分类:编程开发| 查看评论
相关文章
文章点击排行
本年度文章点击排行
发表评论:
  • 昵称: *
  • 邮箱: *
  • 网址:
  • 评论:(最多100字)
  • 验证码: