日期:2014-05-17  浏览次数:20675 次

急!文件上传乱码的奇怪问题
直接上代码,如下:
Java code

/**
 * 下面的这几行代码是在struts2的Action中
 * request返回字符集是UTF-8
 * 文件也创建成功
 * newFile定义为html文件类型
 */
File newFile = new File(subFilePath + File.separator
    + this.getFileName(subFile) + ".html");
System.out.println("字符集-------->"+request.getCharacterEncoding());
System.out.println(context); //这里也可以正常显示中文
newFile.createNewFile();                    
osw.write(context);
osw.close();
logger.info("新建文件成功");



用记事本打开上面代码新生成的文件,一切正常!但是通过浏览器访问却出现乱码。
右键查看页面编码为Unicode(UTF-8),更改为GB2312正常显示。

疑惑:
请问各位大牛:为什么会出现上面的原因???该如何进行修改??
提前谢谢大家了!急!

------解决方案--------------------
文件为GB2312编码,当然要用GB2312编码打开