求帮助!在JAVA代码中建立JSP出现中文乱码问题

求解决!在JAVA代码中建立JSP出现中文乱码问题,新建JSP文件里面,中文是乱码的。

public  void CreatHtml(String filePath){

        //创建、初始化stringHtml对象       
        StringBuilder stringHtml = new StringBuilder();

        //初始化文件对象
        PrintStream printStream =null;

        try{
            //打开文件
             printStream = new PrintStream(new FileOutputStream(filePath));
        }catch(FileNotFoundException e){
            e.printStackTrace();
        }
        //追加输入HTML文件内容

        stringHtml.append("<html><head>");
        stringHtml.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8 ; pageEncoding=UTF-8\">");\">");
        stringHtml.append("<title>测试报告文档</title>");
        stringHtml.append("</head>");
        stringHtml.append("<body>");

        stringHtml.append("<h1>简单快速用Java动态生成jsp/html页面</h1>");
        stringHtml.append("</body></html>");
        try{
            //将HTML文件内容写入文件中
            printStream.println(stringHtml.toString());
        }catch (Exception e) {
            e.printStackTrace();
        }
    }



        public String execute(){


        String imgname =UUID.randomUUID().toString()+".jsp";
        System.out.println(imgname);
        //文件储存路径
        String filePath = "d:\\MyEclipseWork\\gxAppWebServer\\WebRoot\\"+imgname;
        //创建文件
        CreatHtml(filePath);


        return SUCCESS;
    }

首先,确认你的Java代码是GBK还是UTF-8的。
比如你用Eclipse写的,那就看看你的Eclipse是什么编码。
其次,你看看你写入文件的时候,用的是什么编码写进去的。
最好是指定编码格式来写入。
比如:

                StringBuilder stringHtml = new StringBuilder();
        //追加输入HTML文件内容

        stringHtml.append("<html><head>");
        stringHtml.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">");
        stringHtml.append("<title>测试报告文档</title>");
        stringHtml.append("</head>");
        stringHtml.append("<body>");

        stringHtml.append("<h1>简单快速用Java动态生成jsp/html页面</h1>");
        stringHtml.append("</body></html>");
                String html = stringHtml.toString();

                String imgname =UUID.randomUUID().toString()+".jsp";
        System.out.println(imgname);
                //文件储存路径
                String filePath = "d:\\MyEclipseWork\\gxAppWebServer\\WebRoot\\"+imgname;
                try {
                            File file = new File(filePath);
                            file.createNewFile();
                            FileUtils.write(file, html,"UTF-8");
                } catch (IOException e) {
                        e.printStackTrace();
                }

依赖包:

        <!-- FileUtils依赖 -->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>

结果:
图片说明

内容:
图片说明

用xs编辑就好了 超级好用 方便!

一般来说,默认都是UTF-8

 stringHtml.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">");

亲,java写出文件用PrintStream??(不禁想问一下是谁把你教坏了?) 。写出文件有一个避免中文乱码的标准写法:
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file),"UTF-8"));
bw.write(string);
其中“UTF-8”要跟你编辑代码的.java文件的编码方式一致(都设成UTF-8吧,别整其他幺蛾子了)。

解释一下:建议可以看下java字节流和字符流的相关知识(百度一下也可以)。
上面new FileOutputStream(file)是字节流写文件,套了个 new OutputStreamWriter就是字符流了。你的String(如果不.getBytes)只能写到字符流中。注意new OutputStreamWriter构造方法的第二个参数,设置编码方式:当字符流转成字节流时,必须要设置你的字符流是何种编码方式的,才能正确转成字节流 。
当然,你也可以直接用fileOutputStream.write(string.getBytes("UTF-8")) ,把String转成字节写,直接写出字节流。(还可以再套个BufferedOutputStream)
字符流直接写文件,有个FileWriter类,但是好像不好设置编码方式,容易出错。
你的PrintStream我用得少,不知道有没有设置编码方式的地方。

这个原因是你没有设置文件保存编码,PrintStream创建时设置编码就可以了
//初始化文件对象
PrintStream printStream =null;
try {
printStream = new PrintStream(new FileOutputStream(filePath),true,"UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}

    //追加输入HTML文件内容

    stringHtml.append("<html lang=\"en\"><head>");
    stringHtml.append("<meta charset=\"UTF-8\">");
    stringHtml.append("<title>测试报告文档</title>");
    stringHtml.append("</head>");
    stringHtml.append("<body>");

    stringHtml.append("<h1>简单快速用Java动态生成jsp/html页面</h1>");
    stringHtml.append("</body></html>");
    try{
        //将HTML文件内容写入文件中
        printStream.println(stringHtml.toString());
    }catch (Exception e) {
        e.printStackTrace();
    }