tomcat让人抓狂,后台java写的一个应用程序生成的静态html居然是ANSI编码格式的文件,前台首页点击查看页面时直接乱码了…

 

使用新的tomcat、重新配置,然后放在webapp下重新弄,不使用Catalina/localhost的方式,依旧不行。。得,同事机器上可以跑,而且正常,就我机器跑不起来。因为要频繁更改模板文件,所以不太好用nginx反向代理到同事的机器上,最好能在本机跑..

 

所以决定用php把那些该死的乱码文件全部全部转一下编码再保存…

在使用file_put_contents的时候,遇到比较郁闷的问题,文件格式是对了,但里面却乱码了,后面想想,觉得应该是先删除文件再进行处理,试了一下,文件格式正确、内容正确。

 

在使用iconv函数时,先开始用gb2312->utf-8发现不行,部分字符串无法读入进去,然后抱着试试的心态,使用gbk->utf-8居然行了~ ~

---注:在转码前需要判定该文件编码格式是否为utf-8,如果为utf-8转码反而出错

 

花几分钟写的,有点乱,达到预期目标就成。

>
   2:  
   3: <?php
   4:     
function reSaveFile($path) {
   6:         
if (is_dir($path)) {
   8:             
if ($handle = opendir($path)) {
while (($file = readdir($handle)) !== FALSE) {
  11:                     
) {
.$file;
  14:                         
, $file)) {
;
  17:                             
  18:                             saveHandler($fileName);
if (is_dir($fileName)) {
;
  21:                             
  22:                             reSaveFile($fileName);
else {
;                            
  25:                         }
  26:                         
  27:                     }
  28:                     
  29:                 }
  30:                 
  31:                 closedir($path);
  32:             }
  33:             
else {
;
  36:         }
  37:         
  38:     }
  39:     
function isUTF8($str) {
)) {
true;
else {
false;
  45:         }
  46:     }
  47:     
function saveHandler($fileName) {
;
  50:         
  51:         $file_content = file_get_contents($fileName);
//        $file_content = strip_tags($file_content);
  53:         
if (isUTF8($file_content)) {
;
return ;
  57:         }
  58:         
, $file_content);
  60:         
if (file_exists($fileName)) {
  62:             unlink($fileName);
  63:         }
  64:         
  65:         file_put_contents($fileName, $file_content);
  66:         
;
  68:         
;
  70:     }
  71:     
);
  73: ?>

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-16
  • 2022-01-07
  • 2022-12-23
  • 2022-12-23
  • 2021-12-14
猜你喜欢
  • 2021-12-05
  • 2021-12-28
  • 2022-12-23
  • 2022-12-23
  • 2021-09-09
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案