java处理金证中登查询图片二进制流问题
package com.szkingdom.kess.model; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.List; import java.util.Vector; import org.apache.axis.encoding.Base64; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element; import com.szkingdom.kess.service.ConfigService; import com.szkingdom.kess.ui.ClientFrame; /** * 处理接口的返回结果集 */ public class ServiceResult { private String operation = null; private String resultStr = null; private String recordType = null; private String binaryOutput = null; private String flag = null; private String prompt = null; private String[] resultHeads = new String[0]; private Vector<Vector<String>> allRecord = new Vector<Vector<String>>(); private byte[] imgBytes = null; private String recordXml = null; public ServiceResult(String operation, String resultStr) throws DocumentException, IOException { this.operation = operation; this.resultStr = resultStr; Service service = ConfigService.getInstance().getServices().getService(operation); this.recordType = service.getRecordType(); this.binaryOutput = service.getBinaryOutput(); if(null == this.binaryOutput || this.binaryOutput.equals("0")) { //处理xml格式的数据 handleXmlResult(resultStr); } else { //处理二进制的数据,图片 byte[] resultStrBytes = Base64.decode(resultStr); if(4 == resultStrBytes.length) { handleXmlResult(ClientFrame.clientReqServices.parseXMLResultError("未能查询到对应的影像数据")); } else { int xmlLen = 0; for (int i = 0; i < 4; i++) { int shift = (4 - 1 - i) * 8; xmlLen += (resultStrBytes[i + 0] & 0x000000FF) << shift; } this.resultStr = new String(resultStrBytes, 4, xmlLen); handleXmlResult(this.resultStr); imgBytes = new byte[resultStrBytes.length - xmlLen - 4]; System.arraycopy(resultStrBytes, xmlLen + 4, imgBytes, 0, imgBytes.length); File file = new File("images"); if(!file.exists()) { file.mkdir(); } FileOutputStream fops = new FileOutputStream(new File("images/download" + getImgType(imgBytes))); fops.write(imgBytes); fops.close(); } } } private void handleXmlResult(String xmlStr) throws DocumentException { Document doc = DocumentHelper.parseText(xmlStr); Element rootEle = doc.getRootElement(); Element resultEle = rootEle.element("result"); this.flag = resultEle.element("flag").getText(); this.prompt = resultEle.element("prompt").getText(); List<Element> rowEleList = rootEle.element("record").elements("row"); if(this.recordType.equals("1")) { for(int i = 0, rowLen = rowEleList.size(); i < rowLen; i++) { Element rowEle = rowEleList.get(i); List<Element> resultEleList = rowEle.elements(); int resultLen = resultEleList.size(); if(0 == i) { resultHeads = new String[resultLen]; } Vector<String> tmpVec = new Vector<String>(); for(int j = 0; j < resultLen; j++) { Element rowResultEle = resultEleList.get(j); if(0 == i) { resultHeads[j] = rowResultEle.getName(); } tmpVec.add(rowResultEle.getText()); } allRecord.add(tmpVec); } } else { String recordNodeXml = rootEle.element("record").asXML(); recordXml = recordNodeXml.equals("<record/>") ? "" : recordNodeXml.substring(recordNodeXml.indexOf("<record>") + 8, recordNodeXml.lastIndexOf("</record>")); } } private String getImgType(byte[] imgBytes) { if(null == imgBytes) { return null; } StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < 4; i++) { String hexStr = Integer.toHexString(imgBytes[i] & 0xFF); //byte to int stringBuilder.append(hexStr.length() < 2 ? "0" : hexStr); } String typeStr = stringBuilder.toString().toUpperCase(); if(typeStr.contains("424D")) { return ".bmp"; } if(typeStr.contains("FFD8FF")) { return ".jpg"; } if(typeStr.contains("47494638")) { return ".gif"; } if(typeStr.contains("49492A0")) { //tif格式有两个版本的标准,"49492A00" return ".tif"; } if(typeStr.contains("89504E47")) { return".png"; } return ".jpg"; } public String getResultStr() { StringBuilder strBuilder = new StringBuilder(); strBuilder.append("接口名称:" + this.operation + "\n"); strBuilder.append("执行结果:" + ("1".equals(this.flag) ? "执行成功" : "执行失败") + "\n"); strBuilder.append("提示信息:" + this.prompt + "\n"); StringBuilder strBuilderRec = new StringBuilder(); if(this.recordType.equals("1")) { for(int i = 0; i < this.resultHeads.length; i++) { strBuilderRec.append(this.resultHeads[i].trim() + "\t"); } strBuilderRec.append("\n"); for(int i = 0, rowLen = this.allRecord.size(); i < rowLen; i++) { Vector<String> rowVec = this.allRecord.get(i); for(int j = 0, colLen = rowVec.size(); j < colLen; j++) { strBuilderRec.append(rowVec.get(j).trim() + "\t"); } strBuilderRec.append("\n"); } } else { strBuilderRec.append(this.recordXml); } String recStr = strBuilderRec.toString().trim(); strBuilder.append(recStr.equals("") ? "" : "返回结果:\n" + recStr); return strBuilder.toString(); } public byte[] getImgBytes() { return imgBytes; } public String getOperation() { return operation; } public String getRecordType() { return recordType; } public String getFlag() { return flag; } public String getPrompt() { return prompt; } public String[] getResultHeads() { return resultHeads; } public String getRecordXml() { return recordXml; } }