NutzCN Logo
问答 dao mysql blob 中文乱码
发布于 910天前 作者 hanang128 2166 次浏览 复制 上一个帖子 下一个帖子
标签: dao blob

使用dao向mysql中读取数据时blob类型的字段中的值时中文乱码。
下边是读取出来的值:
{"resourceId":"canvas","properties":{"process_id":"process","name":"","documentation":"","process_author":"","process_version":"","process_namespace":"http://www.activiti.org/processdef","executionlisteners":"","eventlisteners":"","signaldefinitions":"","messagedefinitions":""},"stencil":{"id":"BPMNDiagram"},"childShapes":[{"resourceId":"sid-B3B1D39F-881E-4EAD-B03F-3F1E42A84E79","properties":{"overrideid":"","name":"","documentation":"","executionlisteners":"","initiator":"","formkeydefinition":"","formproperties":""},"stencil":{"id":"StartNoneEvent"},"childShapes":[],"outgoing":[{"resourceId":"sid-09C869E8-42CB-4006-9197-0D8D45384565"}],"bounds":{"lowerRight":{"x":202.5,"y":150},"upperLeft":{"x":172.5,"y":120}},"dockers":[]},{"resourceId":"sid-121C3CAF-C45D-41BC-8ECB-E0FE70A3E92A","properties":{"overrideid":"FlowNode1","name":"中心主任","documentation":"","customproperty":{"nodeId":"FlowNode1","nodeName":"中心主任","nodeType":"0","nodeIndex":"","selOtherPsn":"no","confirmPsn":"no","nodeApprover":"","bhType":"startNode","specifyName":"","autoPass1":"no","autoPass2":"no","autoPass3":"no","autoPass3Approver":"","timeLimit":"","mailCreater":"no","mailApprover":"no","smsApprover":"no","noticeApprover":""},"code":1,"asynchronousdefinition":"false","exclusivedefinition":"false","executionlisteners":"","multiinstance_type":"None","multiinstance_cardinality":"","multiinstance_collection":"","multiinstance_variable":"","multiinstance_condition":"","isforcompensation":"false","usertaskassignment":"","formkeydefinition":"","duedatedefinition":"","prioritydefinition":"","formproperties":"","tasklisteners":""},"stencil":{"id":"UserTask"},"childShapes":[],"outgoing":[{"resourceId":"sid-28FF1912-9FC0-4347-B860-0ED8F1A4B371"}],"bounds":{"lowerRight":{"x":347.5,"y":175},"upperLeft":{"x":247.5,"y":95}},"dockers":[]},{"resourceId":"sid-09C869E8-42CB-4006-9197-0D8D45384565","properties":{"overrideid":"","name":"","documentation":"","conditionsequenceflow":"","executionlisteners":"","defaultflow":"false"},"stencil":{"id":"SequenceFlow"},"childShapes":[],"outgoing":[{"resourceId":"sid-121C3CAF-C45D-41BC-8ECB-E0FE70A3E92A"}],"bounds":{"lowerRight":{"x":246.65625,"y":135},"upperLeft":{"x":203.109375,"y":135}},"dockers":[{"x":15,"y":15},{"x":50,"y":40}],"target":{"resourceId":"sid-121C3CAF-C45D-41BC-8ECB-E0FE70A3E92A"}},{"resourceId":"sid-D91CD61A-5F6D-422A-BBB9-57E17D1176B9","properties":{"overrideid":"FlowNode2","name":"分管领导","documentation":"","customproperty":{"nodeId":"FlowNode2","nodeName":"分管领导","nodeType":"0","nodeIndex":"","selOtherPsn":"no","confirmPsn":"no","nodeApprover":"","bhType":"startNode","specifyName":"","autoPass1":"no","autoPass2":"no","autoPass3":"no","autoPass3Approver":"","timeLimit":"","mailCreater":"no","mailApprover":"no","smsApprover":"no","noticeApprover":""},"code":2,"asynchronousdefinition":"false","exclusivedefinition":"false","executionlisteners":"","multiinstance_type":"None","multiinstance_cardinality":"","multiinstance_collection":"","multiinstance_variable":"","multiinstance_condition":"","isforcompensation":"false","usertaskassignment":"","formkeydefinition":"","duedatedefinition":"","prioritydefinition":"","formproperties":"","tasklisteners":""},"stencil":{"id":"UserTask"},"childShapes":[],"outgoing":[{"resourceId":"sid-8B8663E2-59F0-4CCB-9012-ACCDC034C2B4"}],"bounds":{"lowerRight":{"x":492.5,"y":175},"upperLeft":{"x":392.5,"y":95}},"dockers":[]},{"resourceId":"sid-28FF1912-9FC0-4347-B860-0ED8F1A4B371","properties":{"overrideid":"","name":"","documentation":"","conditionsequenceflow":"","executionlisteners":"","defaultflow":"false"},"stencil":{"id":"SequenceFlow"},"childShapes":[],"outgoing":[{"resourceId":"sid-D91CD61A-5F6D-422A-BBB9-57E17D1176B9"}],"bounds":{"lowerRight":{"x":391.65625,"y":135},"upperLeft":{"x":348.34375,"y":135}},"dockers":[{"x":50,"y":40},{"x":50,"y":40}],"target":{"resourceId":"sid-D91CD61A-5F6D-422A-BBB9-57E17D1176B9"}},{"resourceId":"sid-87F52C0C-FF52-4061-AC77-2E37B68EEB7B","properties":{"overrideid":"","name":"","documentation":"","executionlisteners":""},"stencil":{"id":"EndNoneEvent"},"childShapes":[],"outgoing":[],"bounds":{"lowerRight":{"x":565.5,"y":149},"upperLeft":{"x":537.5,"y":121}},"dockers":[]},{"resourceId":"sid-8B8663E2-59F0-4CCB-9012-ACCDC034C2B4","properties":{"overrideid":"","name":"","documentation":"","conditionsequenceflow":"","executionlisteners":"","defaultflow":"false"},"stencil":{"id":"SequenceFlow"},"childShapes":[],"outgoing":[{"resourceId":"sid-87F52C0C-FF52-4061-AC77-2E37B68EEB7B"}],"bounds":{"lowerRight":{"x":536.875,"y":135},"upperLeft":{"x":492.890625,"y":135}},"dockers":[{"x":50,"y":40},{"x":14,"y":14}],"target":{"resourceId":"sid-87F52C0C-FF52-4061-AC77-2E37B68EEB7B"}}],"bounds":{"lowerRight":{"x":1200,"y":1050},"upperLeft":{"x":0,"y":0}},"stencilset":{"url":"stencilsets/bpmn2.0/bpmn2.0.json","namespace":"http://b3mn.org/stencilset/bpmn2.0#"},"ssextensions":[]}

9 回复

Blob里面存的是二进制数据,没有编码信息

所以,问题出在 String转Blob, 或Blob转String的代码, 贴相关代码.

POJO

package com.celesea.workflow.bean;

import java.io.InputStream;
import java.sql.Blob;

import org.nutz.dao.entity.annotation.ColDefine;
import org.nutz.dao.entity.annotation.ColType;
import org.nutz.dao.entity.annotation.Column;
import org.nutz.dao.entity.annotation.Id;
import org.nutz.dao.entity.annotation.Table;

@Table("t_workflow")
public class Workflow extends BasePojo{

	@Id
	private int id;
	@Column
	private String flowname;
	@Column
	private String category;
	@Column
	private String description;
	@Column
	private String version;
	@Column("json_xml")
	@ColDefine(type=ColType.BINARY)
	private Object jsonxml;
	@Column("svg_xml")
	@ColDefine(type=ColType.BINARY)
	private InputStream svgxml;
	
	public Workflow(){
		
	}
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getFlowname() {
		return flowname;
	}
	public void setFlowname(String flowname) {
		this.flowname = flowname;
	}
	public String getCategory() {
		return category;
	}
	public void setCategory(String category) {
		this.category = category;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public String getVersion() {
		return version;
	}
	public void setVersion(String version) {
		this.version = version;
	}
	public Object getJsonxml() {
		return jsonxml;
	}
	public void setJsonxml(Object jsonxml) {
		this.jsonxml = jsonxml;
	}

	public InputStream getSvgxml() {
		return svgxml;
	}

	public void setSvgxml(InputStream svgxml) {
		this.svgxml = svgxml;
	}
	

}

module中dao操作

			wf = new Workflow();
			wf.setFlowname(params.getString("name"));
			wf.setCategory(params.getString("category"));
			wf.setDescription(params.getString("description"));
			try {
				wf.setJsonxml(new String(params.getString("json_xml").getBytes("utf-8")));
			} catch (UnsupportedEncodingException e2) {
				// TODO Auto-generated catch block
				e2.printStackTrace();
			}
			//wf.setSvgxml(params.get("svg_xml"));
			wf.setCreateTime(new Date());

读取的代码

	Workflow wf = dao.fetch(Workflow.class,Integer.parseInt(modelId));
			log.debug(wf.getFlowname());
			log.debug("model:" + wf.getJsonxml());
			try {
				log.debug("model:" + new String(wf.getJsonxml().toString().getBytes("utf-8")));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			res.put("model", Json.fromJson(wf.getJsonxml().toString()));

难道你这代码跑着非UTF8环境下,不然这样写有神马意义

wf.setJsonxml(new String(params.getString("json_xml").getBytes("utf-8"))); // 把字符串按utf8分解为byte[],然后按容器编码转成String,蛋很疼

正常来说, 上面的语句是多余的.

检查启动时nutz打印出来的容器编码, 改好那个才是正路.

启动日志
应该是UTF-8
2016-06-22 13:00:28,648 org.nutz.mvc.impl.NutLoading.load(NutLoading.java:55) INFO - Nutz Version : 1.r.56
2016-06-22 13:00:28,648 org.nutz.mvc.impl.NutLoading.load(NutLoading.java:56) INFO - Nutz.Mvc[nutz] is initializing ...
2016-06-22 13:00:28,648 org.nutz.mvc.impl.NutLoading.load(NutLoading.java:60) DEBUG - Web Container Information:
2016-06-22 13:00:28,650 org.nutz.mvc.impl.NutLoading.load(NutLoading.java:61) DEBUG - - Default Charset : UTF-8
2016-06-22 13:00:28,653 org.nutz.mvc.impl.NutLoading.load(NutLoading.java:62) DEBUG - - Current . path : D:\JAVA_OA\workflow.
2016-06-22 13:00:28,653 org.nutz.mvc.impl.NutLoading.load(NutLoading.java:63) DEBUG - - Java Version : 1.8.0_05
2016-06-22 13:00:28,653 org.nutz.mvc.impl.NutLoading.load(NutLoading.java:64) DEBUG - - File separator : \
2016-06-22 13:00:28,654 org.nutz.mvc.impl.NutLoading.load(NutLoading.java:65) DEBUG - - Timezone : Asia/Shanghai
2016-06-22 13:00:28,654 org.nutz.mvc.impl.NutLoading.load(NutLoading.java:66) DEBUG - - OS : Windows 8.1 amd64
2016-06-22 13:00:28,660 org.nutz.mvc.impl.NutLoading.load(NutLoading.java:67) DEBUG - - ServerInfo : Apache Tomcat/7.0.47
2016-06-22 13:00:28,661 org.nutz.mvc.impl.NutLoading.load(NutLoading.java:68) DEBUG - - Servlet API : 3.0
2016-06-22 13:00:28,662 org.nutz.mvc.impl.NutLoading.load(NutLoading.java:73) DEBUG - - ContextPath : /workflow
2016-06-22 13:00:28,662 org.nutz.mvc.impl.NutLoading.load(NutLoading.java:74) DEBUG - - context.tempdir : D:\JAVA_OA\workflow\target\tomcat\work\Tomcat\localhost\workflow

那就去掉这些转码的代码试试.

去掉不行才加的;去掉又试了下,还是不行。用SQLyog查看mysql 中的数据是正常的。

{"resourceId":"canvas","properties":{"process_id":"process","name":"","documentation":"","process_author":"","process_version":"","process_namespace":"http://www.activiti.org/processdef","executionlisteners":"","eventlisteners":"","signaldefinitions":"","messagedefinitions":""},"stencil":{"id":"BPMNDiagram"},"childShapes":[{"resourceId":"sid-197B5AAE-721C-4F36-9BE9-18437E8A4D56","properties":{"overrideid":"","name":"","documentation":"","executionlisteners":"","initiator":"","formkeydefinition":"","formproperties":""},"stencil":{"id":"StartNoneEvent"},"childShapes":[],"outgoing":[{"resourceId":"sid-1766F55E-FAE5-4392-AFA0-B24DEA0675BD"}],"bounds":{"lowerRight":{"x":188.5,"y":131},"upperLeft":{"x":158.5,"y":101}},"dockers":[]},{"resourceId":"sid-901DB459-D958-4F54-8095-CAB69E080D25","properties":{"overrideid":"FlowNode1","name":"中心主任","documentation":"","customproperty":{"nodeId":"FlowNode1","nodeName":"中心主任","nodeType":"0","nodeIndex":"","selOtherPsn":"no","confirmPsn":"no","nodeApprover":"","bhType":"startNode","specifyName":"","autoPass1":"no","autoPass2":"no","autoPass3":"no","autoPass3Approver":"","timeLimit":"","mailCreater":"no","mailApprover":"no","smsApprover":"no","noticeApprover":""},"code":1,"asynchronousdefinition":"false","exclusivedefinition":"false","executionlisteners":"","multiinstance_type":"None","multiinstance_cardinality":"","multiinstance_collection":"","multiinstance_variable":"","multiinstance_condition":"","isforcompensation":"false","usertaskassignment":"","formkeydefinition":"","duedatedefinition":"","prioritydefinition":"","formproperties":"","tasklisteners":""},"stencil":{"id":"UserTask"},"childShapes":[],"outgoing":[{"resourceId":"sid-242FF20E-D8A8-47CB-8016-E913D4BA3926"}],"bounds":{"lowerRight":{"x":333.5,"y":156},"upperLeft":{"x":233.5,"y":76}},"dockers":[]},{"resourceId":"sid-1766F55E-FAE5-4392-AFA0-B24DEA0675BD","properties":{"overrideid":"","name":"","documentation":"","conditionsequenceflow":"","executionlisteners":"","defaultflow":"false"},"stencil":{"id":"SequenceFlow"},"childShapes":[],"outgoing":[{"resourceId":"sid-901DB459-D958-4F54-8095-CAB69E080D25"}],"bounds":{"lowerRight":{"x":232.65625,"y":116},"upperLeft":{"x":189.109375,"y":116}},"dockers":[{"x":15,"y":15},{"x":50,"y":40}],"target":{"resourceId":"sid-901DB459-D958-4F54-8095-CAB69E080D25"}},{"resourceId":"sid-A85B836A-C20E-4F99-82DC-17FCB654CC33","properties":{"overrideid":"","name":"","documentation":"","executionlisteners":""},"stencil":{"id":"EndNoneEvent"},"childShapes":[],"outgoing":[],"bounds":{"lowerRight":{"x":406.5,"y":130},"upperLeft":{"x":378.5,"y":102}},"dockers":[]},{"resourceId":"sid-242FF20E-D8A8-47CB-8016-E913D4BA3926","properties":{"overrideid":"","name":"","documentation":"","conditionsequenceflow":"","executionlisteners":"","defaultflow":"false"},"stencil":{"id":"SequenceFlow"},"childShapes":[],"outgoing":[{"resourceId":"sid-A85B836A-C20E-4F99-82DC-17FCB654CC33"}],"bounds":{"lowerRight":{"x":377.875,"y":116},"upperLeft":{"x":333.890625,"y":116}},"dockers":[{"x":50,"y":40},{"x":14,"y":14}],"target":{"resourceId":"sid-A85B836A-C20E-4F99-82DC-17FCB654CC33"}}],"bounds":{"lowerRight":{"x":1200,"y":1050},"upperLeft":{"x":0,"y":0}},"stencilset":{"url":"stencilsets/bpmn2.0/bpmn2.0.json","namespace":"http://b3mn.org/stencilset/bpmn2.0#"},"ssextensions":[]}

@wendal 应该是Blob转String的代码的问题了,可是这个在哪里处理呢?

private Object jsonxml;
// 改成
private byte[] jsonxml;

好了,多谢啊。

添加回复
请先登陆
回到顶部