qq_34424362 2019-04-25 18:24 采纳率: 50%
浏览 753
已结题

求帮助!在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;
    }
  • 写回答

5条回答 默认 最新

  • Mizzz迷醉 2019-04-25 19:30
    关注

    首先,确认你的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>
    

    结果:
    图片说明

    内容:
    图片说明

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog