1. 程式人生 > >富文字框回顯亂碼問題解決

富文字框回顯亂碼問題解決

富文字編輯器是將樣式及內容一起放入資料庫,所用資料型別為blob型,在資料裝入資料庫及從資料庫中讀取時應注意編碼格式問題,以防止出現亂碼。出現亂碼時主要解決手段:

1.檢視建立blob物件是不是用的“utf8”編碼格式

2.查詢時是否進行了二次編碼解析,主要體現在mapper.xml檔案中。blob對應欄位要進行處理,處理內容如下:

  1. package cn.ffcs.drive.common.util;  
  2. import java.io.ByteArrayInputStream;  
  3. import java.io.UnsupportedEncodingException;  
  4. import java.sql.Blob;  
  5. import java.sql.CallableStatement;  
  6. import java.sql.PreparedStatement;  
  7. import java.sql.ResultSet;  
  8. import java.sql.SQLException;  
  9. import org.apache.ibatis.type.BaseTypeHandler;  
  10. import org.apache.ibatis.type.JdbcType;  
  11. /** 
  12.  * className:ConvertBlobTypeHandler 
  13.  *  
  14.  * 自定義typehandler,解決mybatis儲存blob欄位後,出現亂碼的問題
     
  15.  * 配置mapper.xml: 
  16.  * <result  typeHandler="cn.ffcs.drive.common.util.ConvertBlobTypeHandler"/> 
  17.  *  
  18.  * @author pengyh 
  19.  * @version 1.0.0 
  20.  * @date 2014-07-09 11:15:23 
  21.  *  
  22.  */
  23. publicclass ConvertBlobTypeHandler extends BaseTypeHandler<String> {    
  24.     //###指定字符集  
  25.     privatestaticfinal String DEFAULT_CHARSET = 
    "utf-8";    
  26.     @Override
  27.     publicvoid setNonNullParameter(PreparedStatement ps, int i,    
  28.             String parameter, JdbcType jdbcType) throws SQLException {    
  29.         ByteArrayInputStream bis;    
  30.         try {    
  31.             //###把String轉化成byte流  
  32.             bis = new ByteArrayInputStream(parameter.getBytes(DEFAULT_CHARSET));    
  33.         } catch (UnsupportedEncodingException e) {    
  34.             thrownew RuntimeException("Blob Encoding Error!");    
  35.         }       
  36.         ps.setBinaryStream(i, bis, parameter.length());    
  37.     }    
  38.     @Override
  39.     public String getNullableResult(ResultSet rs, String columnName)    
  40.             throws SQLException {    
  41.         Blob blob = rs.getBlob(columnName);    
  42.         byte[] returnValue = null;    
  43.         if (null != blob) {    
  44.             returnValue = blob.getBytes(1, (int) blob.length());    
  45.         }    
  46.         try {    
  47.             //###把byte轉化成string  
  48.             returnnew String(returnValue, DEFAULT_CHARSET);    
  49.         } catch (UnsupportedEncodingException e) {    
  50.             thrownew RuntimeException("Blob Encoding Error!");    
  51.         }    
  52.     }    
  53.     @Override
  54.     public String getNullableResult(CallableStatement cs, int columnIndex)    
  55.             throws SQLException {    
  56.         Blob blob = cs.getBlob(columnIndex);    
  57.         byte[] returnValue = null;    
  58.         if (null != blob) {    
  59.             returnValue = blob.getBytes(1, (int) blob.length());    
  60.         }    
  61.         try {    
  62.             returnnew String(returnValue, DEFAULT_CHARSET);    
  63.         } catch (UnsupportedEncodingException e) {    
  64.             thrownew RuntimeException("Blob Encoding Error!");    
  65.         }    
  66.     }  
  67.     @Override
  68.     public String getNullableResult(ResultSet arg0, int arg1)  
  69.             throws SQLException {  
  70.         // TODO Auto-generated method stub
  71.         returnnull;  
  72.     }    
  73. }   


定義上面類之後,在mapper.xml中配置<result property="content" column="CONTENT" typeHandler="cn.ffcs.drive.common.util.ConvertBlobTypeHandler"/>即可。

實體接收使用String接收。

轉載自http://blog.csdn.net/p793049488/article/details/37818989


相關推薦

文字亂碼問題解決

富文字編輯器是將樣式及內容一起放入資料庫,所用資料型別為blob型,在資料裝入資料庫及從資料庫中讀取時應注意編碼格式問題,以防止出現亂碼。出現亂碼時主要解決手段:1.檢視建立blob物件是不是用的“utf8”編碼格式2.查詢時是否進行了二次編碼解析,主要體現在mapper.x

關於c#MVC後臺接收百度文字中的值失敗原因的解決!

最近在使用百度富文字框的時候 ,遇見了一個令我很無語的操作,我在前臺使用jQuery獲取百度富文字框中的值,路徑什麼的都是對的, 也沒有語法錯誤,但是就進不了後臺,後臺就一直接收不到資料 最後通過百度找到了解決的方法:如下 HttpRequest 類使用輸入驗證標誌來跟蹤是否對通過 Co

selenium如何處理特殊的文字------例如知乎

常見的富文字框是input, textarea文字框,如果有iframe巢狀,需要進行表單切換,可以參考https://blog.csdn.net/supramolecular/article/details/81364061, 但是對於div富文字框,既不包括input 也不包括 textar

Ueditor 百度文字的使用(二次渲染)其他的在文件中都有

富文字編輯器有很多。好用的,不好用的,功能簡單的,功能複雜的。 現在,我選擇的是百度的UEditor編輯器。這個編輯器的唯一有點就是功能多。比kindeditor 這些編輯器的功能要多。當然,像layui 提供的富文字框我沒有用,所以,現在不能拿來對比。因為當初想要用layui的時候,我套了一下

文字TinyMCE上傳本地圖片基本配置

注意:上傳本地圖片是TinyMCE 4.3才新引入的功能,所以該配置只適合4.3及其以上 <!doctype html> <html> <head> <script src='https://cloud.tinymce.com/stable/tinymce.m

使用vue製作文字

這裡分享一個富文字框外掛,如圖 使用方法: 1-安裝 npm install --save vue2-editor 或者 yarn add vue2-editor 2- 使用 // Basic Use - Covers most scenarios

下拉

引入jstl <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>  下拉框回顯 <select name="queryMySelect" class="texts"> <

前臺用UMeditor文字的形式存取內容

完成效果圖如下: 可以去官網:https://ueditor.baidu.com/website/index.html ue版本功能多,um版本日常功能都有,我下載的um把版本 1)  需要依賴static/Ueditor <link href="${pa

freemarker 下拉

使用的是Boolean值,如果為flase,返回的則是 ' ' 空字串,否則返回 'true' 字串 <select id="hot" class="easyui-combobox" data-

ubuntu文字模式/終端中文亂碼解決

# Configuration for FbTerm # Lines starting with '#' are ignored. # Note that end-of-line comments are NOT supported, comments must be on a line of their o

layui表格資料複選設定

layui2.3版本,本身並不帶有複選框回顯功能,那麼需要從源頭解決此事,F12程式碼除錯,找到與複選框關聯的地方發現: 我們只需要在渲染資料回撥時找到每個複選框根據資料的不同來設定回顯。 layui這裡有一個坑,設定class屬性後會造成二次點選效果,千萬不要手動修改class屬性,

自己動手實現簡易的div可編輯文字及按下tab鍵後增加4個空格功能

需求分析:最近需要製作一個簡單的使用者評論輸入框,在網上找了一些富文字輸入框,但是它們的功能太多,不適合自己的需求,於是決定自己動手實現一個簡易的富文字輸入框。第一步:想要實現富文字輸入框並不是難事,在<div>標籤內加入   contenteditable="t

vue-cli webpack 引入 wangeditor(輕量級文字)

1:使用npm下載: //(注意 wangeditor 全部是小寫字母) npm install wangeditor 2: 直接在專案模板中引用 import E from 'wanged

thinkphp5下百度文字UEditor的使用

1到官網下載 UEditor 最新版2解壓下載的包,將解壓後的目錄放到thinkphp public資料夾下3. 引用時    html頁面<body><!-- 載入編輯器的容器 --><scriptid="container"name="con

select下拉的幾種方法

第一種: jsp程式碼: <selectid="csrqnf"name="csrqnf"id="selecte"class="shortselect"value="${nfResult}">                               

如何獲取文字中的內容

js程式碼: <!-- 文字編輯器 開始 --> <textarea name="content" id="contect_text" class="ckedito

在html頁面寫一個文字

第一步:獲取到富文字框的這個檔案,連結地址:https://ckeditor.com/cke4/builder(1)裡面有幾個選擇,看你需求自己進行選擇,然後滑鼠一直拖到最下面,開始下載包        下面我只是舉個例子:我選擇的事第三個full;後面還有些選擇類似。注意:

Ueditor文字編輯器報錯解決方案

ueditor 它配置時會請求配置檔案 在Thinkphp下如果你在config.php檔案裡開啟show_page_trace=true在一切和官網配置無異前提下,上傳時出現後端配置出錯的

文字的使用

相關資料下載地址 首先引進相關的js、css <link rel="stylesheet" href="${basePath}/static/font-awesome-3.2.1/css/font-awesome.min.css" type="text/css"> <lin

專案中用到的文字編輯器wangEditor

文件地址:http://www.wangeditor.com/index.html  <div id="div1"></div> <script src="js/wangEditor.min.js"></script> var E = w