注:开发IDE是eclilpse,本地开发环境是windows,测试环境是linux
项目之前是一位兄弟开发的,项目编码为GBK编码,半路做其它项目,我接接手,一次也没布署过测试环境。
我在本地一切开发好之后,测试没有问题,布到linux上就有乱码问题。
找了半天终于找到原因,测试服务器linux上是UTF-8编码,使用locale命令查看:
LANG=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=
无奈,只得把项目中所有带GBK的地方全部改成UTF-8,这包括: web.xml
所有的jsp页面: <%@ page contentType="text/html;charset=UTF-8" language="java"%>
这些还罢了,最麻烦的是所有的类文件都要编译为UTF-8编码的,具体操作为:
在eclipse中选中一个JAVA文件,右击 -> properties -> Text file encoding 修改为UTF-8,
可这样一修改,JAVA文件中所有的中文字符全部变成了乱码,解决的办法为:
先把JAVA文件修改为GBK编码,Ctrl+A全部选择,再把JAVA文件修改为UTF-8编码,在类文件中Ctrl+A全选,删掉,再Ctrl+V粘贴刚才复制的内容。
好,大功告成!
可这只是修改了一个JAVA文件!什么??!!!! 对,你工程里有多少个JAVA文件都要这样修改,否则就会有以下后果
1、所有JAVA类中的中文注释都不可用(这个在某种情况下可以舍弃)2
2、所有JAVA类中中文消息(存储在request中供在JSP中显示的)全部为乱码,这样JSP中不管如何设置,始终是乱码!!
于是,一项浩大的编码改造工程开始了。。。。。
不能直接改linux服务器上的编码吗?
如果不能那真是悲剧...
不过可以考虑自己写程序来改啊
使用程序来改,不知道正不正确:
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileFilter; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.Reader; import java.io.Writer; public class FileEncodingConverter { // Java file extention public static final String FILE_EXTENTION_JAVA = ".java"; public static final int BUFFER_SIZE = 1024 * 4; // File encoding public static final String FILE_ENCODING_UTF8 = "UTF-8"; public static final String FILE_ENCODING_GBK = "GBK"; public static final String FILE_ENCODING_GB2312 = "gb2312"; /** * File filter, only directory and java file */ private static FileFilter fileFilter = new FileFilter() { public boolean accept(File file) { // directory and java file return file.isDirectory() || (file.isFile() && file.getName().endsWith(FILE_EXTENTION_JAVA)); } }; /** * @param sourceDirectoryPath * sourceDirectory * @param destDirectoryPath * destDirectory * @param sourceFileEncoding * sourceEncoding * @param destFileEncoding * destEncoding */ public static void scanDirectory(String sourceDirectoryPath, String destDirectoryPath, String sourceFileEncoding, String destFileEncoding) { File destDirectory = new File(destDirectoryPath); if (!destDirectory.exists()) { destDirectory.mkdir(); } File sourceDirectory = new File(sourceDirectoryPath); scanDirectory(sourceDirectory, sourceDirectoryPath, destDirectoryPath, sourceFileEncoding, destFileEncoding); } /** * @param directory * directory * @param sourceDirectoryPath * sourceDirectory * @param destDirectoryPath * destDirectory * @param sourceFileEncoding * sourceEncoding * @param destFileEncoding * destEncoding */ private static void scanDirectory(File directory, String sourceDirectoryPath, String destDirectoryPath, String sourceFileEncoding, String destFileEncoding) { File[] files = directory.listFiles(fileFilter); File destFile; for (File file : files) { if (file.isDirectory()) { destFile = new File(destDirectoryPath + file.getAbsolutePath() .substring(sourceDirectoryPath.length())); if (!destFile.exists()) { destFile.mkdir(); } scanDirectory(file, sourceDirectoryPath, destDirectoryPath, sourceFileEncoding, destFileEncoding); } else { System.out .println("Source file:/t" + file.getAbsolutePath() + "/nDest file:/t" + (destDirectoryPath + file.getAbsolutePath().substring( sourceDirectoryPath.length())) + "/n-----------------------------------------------------------------"); convertFile(file.getAbsolutePath(), destDirectoryPath + file.getAbsolutePath().substring(sourceDirectoryPath.length()), sourceFileEncoding, destFileEncoding); } } } /** * @param sourceFilePath * sourceFilePath * @param destFilePath * destFilePath * @param sourceFileEncoding * sourceFileEncoding * @param destFileEncoding * destFileEncoding */ private static void convertFile(String sourceFilePath, String destFilePath, String sourceFileEncoding, String destFileEncoding) { InputStream in = System.in; OutputStream out = System.out; Reader reader = null; Writer writer = null; try { // set up byte streams if (sourceFilePath != null) { in = new FileInputStream(sourceFilePath); } if (destFilePath != null) { out = new FileOutputStream(destFilePath); } // Use default encoding if no encoding is specified. if (sourceFileEncoding == null) sourceFileEncoding = System.getProperty("file.encoding"); if (destFileEncoding == null) destFileEncoding = System.getProperty("file.encoding"); // Set up character stream reader = new BufferedReader(new InputStreamReader(in, sourceFileEncoding)); writer = new BufferedWriter(new OutputStreamWriter(out, destFileEncoding)); char[] buffer = new char[BUFFER_SIZE]; int len; while ((len = reader.read(buffer)) != -1) { writer.write(buffer, 0, len); } } catch (Exception e) { e.printStackTrace(); } finally { if (writer != null) { try { writer.flush(); writer.close(); } catch (IOException e) { e.printStackTrace(); } } if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } try { in.close(); out.close(); } catch (IOException e) { e.printStackTrace(); } } } public static void main(String[] args) { String sourceDirectoryPath = "D://Dev//workspace//ProjectTest//src"; String destDirectoryPath = "D://Dev//workspace//ProjectTest//dest"; scanDirectory(sourceDirectoryPath, destDirectoryPath, FILE_ENCODING_GBK, FILE_ENCODING_UTF8); } }