救救我,怎么将文件编码从UTF-8转换为ANSI(gb2312)?
jafan 2002-12-17 09:01:26 我想用下面的jsp文件将文件编码从UTF-8转换为ANSI(gb2312),结果总是失败。恳请大虾帮我修改一下吧,
-----------------
transform.jsp:
<%@ page contentType="text/html; charset=gb2312" %>
<%@ page language="java" import="java.io.*" %>
<%@ page language="java" import="java.awt.*" %>
<html>
<head><title>文件格式转换</title></head>
<body>
hello, start.....<br>
<%
String iFile=request.getRealPath("./scc.htm"); //输入的源文件
String oFile=request.getRealPath("./sccturned.htm"); //输出的目标文件
//FileInputStream inStrm=new FileInputStream(iFile);
PrintWriter pw=new PrintWriter(new FileOutputStream(oFile)); //输出writer
//FileReader fr=new FileReader(new InputStreamReader(new FileInputStream(iFile)));
String s="hello";
byte buff[] = new byte[800];
//char buff[] = new char[800];
try {
InputStream fileIn = new FileInputStream(iFile);
//Reader fileIn = new FileReader(iFile);
int i = fileIn.read(buff);
s = new String(buff);
out.println(s+"<br><br>");
s=new String(s.getBytes("UTF-8"),"GB2312");
//s=new String(s.getBytes("asci"),"GB2312");
//s=new String(s.getBytes("unUnicode"),"GB2312");
out.println(s);
}
catch(FileNotFoundException e) {
}
catch(IOException e) {
}
pw.println(s); //一次写入一行从输入文件中的数据
pw.println("我要死了!<br>Happy birthday!");
pw.close();
%>
<br> end.
</body>
</html>