富文本框回显乱码问题解决

时间:2025-03-20 10:59:31
  • package ;  
  •   
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  • import ;  
  •   
  • import ;  
  • import ;  
  •   
  • /** 
  •  * className:ConvertBlobTypeHandler 
  •  *  
  •  * 自定义typehandler,解决mybatis存储blob字段后,出现乱码的问题 
  •  * 配置: 
  •  * <result  typeHandler=""/> 
  •  *  
  •  * @author pengyh 
  •  * @version 1.0.0 
  •  * @date 2014-07-09 11:15:23 
  •  *  
  •  */  
  • public class ConvertBlobTypeHandler extends BaseTypeHandler<String> {    
  •     //###指定字符集    
  •     private static final String DEFAULT_CHARSET = "utf-8";    
  •     
  •     @Override    
  •     public void setNonNullParameter(PreparedStatement ps, int i,    
  •             String parameter, JdbcType jdbcType) throws SQLException {    
  •         ByteArrayInputStream bis;    
  •         try {    
  •             //###把String转化成byte流    
  •             bis = new ByteArrayInputStream((DEFAULT_CHARSET));    
  •         } catch (UnsupportedEncodingException e) {    
  •             throw new RuntimeException("Blob Encoding Error!");    
  •         }       
  •         (i, bis, ());    
  •     }    
  •     
  •     @Override    
  •     public String getNullableResult(ResultSet rs, String columnName)    
  •             throws SQLException {    
  •         Blob blob = (columnName);    
  •         byte[] returnValue = null;    
  •         if (null != blob) {    
  •             returnValue = (1, (int) ());    
  •         }    
  •         try {    
  •             //###把byte转化成string    
  •             return new String(returnValue, DEFAULT_CHARSET);    
  •         } catch (UnsupportedEncodingException e) {    
  •             throw new RuntimeException("Blob Encoding Error!");    
  •         }    
  •     }    
  •     
  •     @Override    
  •     public String getNullableResult(CallableStatement cs, int columnIndex)    
  •             throws SQLException {    
  •         Blob blob = (columnIndex);    
  •         byte[] returnValue = null;    
  •         if (null != blob) {    
  •             returnValue = (1, (int) ());    
  •         }    
  •         try {    
  •             return new String(returnValue, DEFAULT_CHARSET);    
  •         } catch (UnsupportedEncodingException e) {    
  •             throw new RuntimeException("Blob Encoding Error!");    
  •         }    
  •     }  
  •   
  •     @Override  
  •     public String getNullableResult(ResultSet arg0, int arg1)  
  •             throws SQLException {  
  •         // TODO Auto-generated method stub  
  •         return null;  
  •     }    
  • }