数据库blob字段用ajax读取显示

$.ajax({

        type:'POST',
        url: 'hhWebsiteInfoController.do?website-getDt',
        dataType:'json',
        data:'',
        success:function(json){
            alert(1);
             dts = json.attributes.dts;
             
            $("#news").empty();
            quanju=dts;
           
              if(typeof(dts)!=undefined){
              for(var i=0;i<dts.length;i++){
              var a = "<div class='col-md-3 col-sm-3 wow fadeIn' data-wow-delay='0.6s' ><a>"+dts[i].title+"</a></div>";
              var b = "<div class='test2' dataIndex="+i+"><img class='test2' src='"+dts[i].pic1+"'></div>";
          //    var d = "<div class='id1' ><a>"+quanju[i].summary+"</a></div>";
              
              var c = a+b;
            
              $("#news").append(c); 
            
        }
        }}
          

});
(document).on('click',"div.test2",function() {

      
         layer.open({
            
          type: 1,
            area: ['600px', '360px'],
            shadeClose: true, //点击遮罩关闭"+quanju[dataIndex].summary+"
            content: '<div><a>'+quanju[$(this).attr('dataIndex')].content+'</a></div><div><img src="'+quanju[$(this).attr('dataIndex')].pic2+'"></div><div><a>'+quanju[$(this).attr('dataIndex')].summary+'</a></div>'
        
        });
    }); 

content字段是blob类型的 取出是byte[]类型的显示这样113,119,101,114,32,49,50,51,52,53,32,32,-41,-19,-63,-53
怎么转成字符串显示,在后台转吗?

阅读 4.5k
1 个回答

这个需要后台提供服务的接口负责的事情~~

使用registerJsonValueProcessor来执行输出内容的转换


public class MyObject {
    private String name="";
    private byte[] content;
    private String title="";
    private Long id;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public byte[] getContent() {
        return content;
    }
    public void setContent(byte[] content) {
        this.content = content;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
}

public class JSONConfTest {

    public static void main(String[] args) {
        List<MyObject> dt=new ArrayList<MyObject>();
        for(int i=0;i<10;i++){
            MyObject somMyObject=new MyObject();
            dt.add(somMyObject);
            somMyObject.setName("name"+i);
            somMyObject.setTitle("title"+i);
            try {
                somMyObject.setContent((new Date().getTime()+"TIME").getBytes("GBK"));
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
        }
        
        JsonConfig config = new JsonConfig();
        config.setIgnoreDefaultExcludes(false);
        config.setJsonPropertyFilter(new PropertyFilter() {
            public boolean apply(Object source, String name, Object value) {
                // 配置你可能出现递归的属性
                if (!name.equals("id")&& !name.equals("title")&&!name.equals("name")
                        &&!name.equals("content")) {
                    return true;
                } else {
                    return false;
                }
            }
        });
       
        config.registerJsonValueProcessor(byte[].class, new JsonValueProcessor() {  
            @Override  
            public Object processObjectValue(String key, Object value, JsonConfig arg2) {  
               if(key.equals("content")){
                   String result="";
                   try {
                       result=new String((byte[])value,"GBK");
                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                   return result;
                }else{
                   return value;
               }
            }  
            @Override  
            public Object processArrayValue(Object value, JsonConfig arg1) {  
                return value;  
            }  
        });
        JSONArray dtsJSONArray = JSONArray.fromObject(dt, config);
        System.out.println (dtsJSONArray.getJSONObject(0));
    }
}
推荐问题