关于用servlet生成静态页面的乱码问题?
package slt;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class ToHtml
extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK ";
//Initialize global variables
public void init() throws
ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException,
IOException {
response.setContentType(CONTENT_TYPE);
service(request,response);
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}
public void destroy() {
}
public void service(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
String url = " ";
String name = " ";
String pName = " ";
ServletContext sc = getServletContext();
url = "/indexbak125.jsp "; //这是要生成HTML的jsp文件,如//http://localhost/index.jsp的执行结果.
name = "indexa.html "; //这是生成的html文件名,如index.htm.
pName = "/usr/tomcat/webapps/ROOT/indexa.html "; //生成html的完整路径
RequestDispatcher rd = sc.getRequestDispatcher(url);
final ByteArrayOutputStream os = new ByteArrayOutputStream();
final ServletOutputStream stream = new ServletOutputStream() {
public void write(byte[] data, int offset, int length) {
os.write(data, offset, length);
}
public void write(int b) throws IOException {
os.write(b);
}
};
final PrintWriter pw = new PrintWriter(new OutputStreamWriter(os));
HttpServletResponse rep = new HttpServletResponseWrapper(response) {
public ServletOutputStream getOutputStream() {
return stream;
}
public PrintWriter getWriter() {
return pw;
}
};
rd.include(request, rep);
pw.flush();
FileOutputStream fos = new FileOutputStream(pName); //把jsp输出的内容写到指定路径的htm文件中
os.writeTo(fos);
fos.close();
response.sendRedirect(name); //书写完毕后转向htm页面
}
}
我用这个servlet生成了静态页面,可为什么生成出来的静态页面是乱码,而且还不能转向呢?
怎么在写文件时设置编码??
------解决方案--------------------FileOutputStream fos =null;
String charset= "UTF-8 ";
try
{
fos = new FileOutputStream(path);
fos.write(value.