解析表字段内容为Gzip格式的数据

来源:互联网 发布:三菱q系列plc编程手册 编辑:程序博客网 时间:2024/05/29 10:51
import org.apache.commons.io.IOUtils;import org.codehaus.jackson.JsonFactory;import org.codehaus.jackson.JsonNode;import org.codehaus.jackson.JsonParser;import org.codehaus.jackson.map.ObjectMapper;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.sql.*;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.zip.GZIPInputStream;/** * Created by hnx on 2017/7/4. */public class MysqlTest {    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";    static final String DB_URL = "jdbc:mysql://localhost:3306/test";    static final String USER = "root";    static final String PASS = "root";    static final String SQL = "SELECT id, project_name, flow_id, enc_type, data FROM execution_options";    /**     * @param args     */    public static void main(String[] args) throws SQLException {        Connection conn = null;        Statement stmt = null;        try {            conn = getConnection();            stmt = conn.createStatement();            ResultSet rs = stmt.executeQuery(SQL);            System.out.println(rs.next());            while (rs.next()) {                int id = rs.getInt(1);                String projectName = rs.getString(2);                String flowId = rs.getString(3);                int encodingType = rs.getInt(4);                byte[] data = rs.getBytes(5);                String jsonObj = null;                if (data != null) {                    String jsonString = unGzipString(data, "UTF-8");                    jsonObj = parseJSONFromString(jsonString).toString();                    System.out.println(jsonObj);                }            }            // System.out.println("Goodbye!");        } catch (SQLException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        } finally {            conn.close();        }    }    public static byte[] unGzipBytes(byte[] bytes) throws IOException {        ByteArrayInputStream byteInputStream = new ByteArrayInputStream(bytes);        GZIPInputStream gzipInputStream = new GZIPInputStream(byteInputStream);        ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();        IOUtils.copy(gzipInputStream, byteOutputStream);        return byteOutputStream.toByteArray();    }    public static String unGzipString(byte[] bytes, String encType)            throws IOException {        byte[] response = unGzipBytes(bytes);        return new String(response, encType);    }    public static Object parseJSONFromString(String json) throws IOException {        ObjectMapper mapper = new ObjectMapper();        JsonFactory factory = new JsonFactory();        JsonParser parser = factory.createJsonParser(json);        JsonNode node = mapper.readTree(parser);        return toObjectFromJSONNode(node);    }    private static Object toObjectFromJSONNode(JsonNode node) {        if (node.isObject()) {            HashMap obj = new HashMap();            Iterator iter = node.getFieldNames();            while (iter.hasNext()) {                String fieldName = (String)iter.next();                JsonNode subNode = node.get(fieldName);                Object subObj = toObjectFromJSONNode(subNode);                obj.put(fieldName, subObj);            }            return obj;        } else if (node.isArray()) {            ArrayList array = new ArrayList();            Iterator iter = node.getElements();            while (iter.hasNext()) {                JsonNode element = (JsonNode)iter.next();                Object subObject = toObjectFromJSONNode(element);                array.add(subObject);            }            return array;        } else if (node.isTextual()) {            return node.asText();        } else if (node.isNumber()) {            if (node.isInt()) {                return node.asInt();            } else if (node.isLong()) {                return node.asLong();            } else if (node.isDouble()) {                return node.asDouble();            } else {                System.err.println("ERROR What is this!? " + node.getNumberType());                return null;            }        } else if (node.isBoolean()) {            return node.asBoolean();        } else {            return null;        }    }    private static Connection getConnection() {        Connection conn = null;        try {            Class.forName(JDBC_DRIVER);            conn = DriverManager.getConnection(DB_URL, USER, PASS);        } catch (ClassNotFoundException e) {            e.printStackTrace();        } catch (SQLException e) {            e.printStackTrace();        }        return conn;    }}
原创粉丝点击