posts - 22,comments - 35,trackbacks - 0
          (此為個(gè)人學(xué)習(xí)心得,以后會(huì)逐漸完善)

          (本例使用的數(shù)據(jù)庫(kù)是:MySql)

          1.當(dāng)你用流讀取文件或者從數(shù)據(jù)庫(kù)讀取數(shù)據(jù)時(shí),取得的字符串的編碼要與頁(yè)面的一致,否則會(huì)亂碼

          例如:

          public class FileOperation {

              
          //path為文件的全路徑
              public static String readFile(String path){
                  String templateContent
          ="";
                  
          try{
                      BufferedReader br
          =new BufferedReader(new FileReader(path));
                      String temp
          =null;
                      
          while((temp=br.readLine())!=null){
                          templateContent
          =templateContent+temp+"\n";
                      }

                      br.close();
                  }

                  
          catch(Exception e){
                      System.out.println(
          "讀取文件出錯(cuò)");
                      e.printStackTrace();
                  }

                  
                  
          return templateContent;
              }

          }


          (假設(shè)頁(yè)面的編碼為UTF-8)

          調(diào)用以上函數(shù)只需要傳遞一個(gè)完整的文件路徑就可以以字符串的形式讀取文件.

          ......

          String str
          =FileOperation.readFile("d:\11.txt");

          ......

          request.setAttribute("str",str);

          ......

          則頁(yè)面用requset.getAttribute("str")取得的中文字符將會(huì)是亂碼.

          解決方案:

          將上段取中文字符串的代碼改成:



          String str
          =new String(FileOperation.readFile("d:\11.txt").getBytes("UTF-8"));



          request.setAttribute(
          "str",str);




          posted on 2005-12-28 16:16 kelven 閱讀(544) 評(píng)論(0)  編輯  收藏 所屬分類: Struts

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 大田县| 任丘市| 衡山县| 兴和县| 繁昌县| 宿迁市| 商城县| 威信县| 巴南区| 锦州市| 田东县| 丹棱县| 略阳县| 秦安县| 怀安县| 天水市| 丹凤县| 潮州市| 扎鲁特旗| 旅游| 眉山市| 蒙阴县| 濮阳市| 同江市| 赤峰市| 扶绥县| 天气| 安阳市| 行唐县| 离岛区| 辉南县| 石棉县| 图木舒克市| 清水县| 宁河县| 隆化县| 文安县| 东光县| 涿鹿县| 故城县| 伊宁市|