用Java解码SQL数据类型“image”数据

时间:2012-02-08 12:37:03

标签: java

我搜索了论坛,发现了一些主题,但没有具体针对我的挑战。

我正在读一个名为" filebytes"的SQL表列。其数据类型为" image"。这个" filebytes" column包含二进制编码文件(在本例中为pdf)。我已将值(见下文)提取为字符串,将其写入xml并通过网络传输xml。我被困在需要解码回人类可读文件的地方。

最初我尝试使用Base64,但之后只实现了base64只能使用ASCII而不是二进制,所以一旦解码,文件最终会损坏。

我不知道如何解码它,因为我唯一的输入"目前是这个字符串,我不知道如何正确地将字符串转换为byte []并进行解码。

非常感谢任何帮助。

下面是SQL中出现的二进制数据,它以字符串形式提取:

  

0x255044462D312E350D0A25B5B5B5B50D0A312030206F626A0D0A3C3C2F547970652F436174616C6F672F50616765732032203020522F4C616E6728656E2D474229202F53747275637454726565526F6F742038203020522F4D61726B496E666F3C3C2F4D61726B656420747275653E3E3E3E0D0A656E646F626A0D0A322030206F626A0D0A3C3C2F547970652F50616765732F436F756E7420312F4B6964735B2033203020525D203E3E0D0A656E646F626A0D0A332030206F626A0D0A3C3C2F547970652F506167652F506172656E742032203020522F5265736F75726365733C3C2F466F6E743C3C2F46312035203020523E3E2F50726F635365745B2F5044462F546578742F496D616765422F496D616765432F496D616765495D203E3E2F4D65646961426F785B20302030203834312E3932203539352E33325D202F436F6E74656E74732034203020522F47726F75703C3C2F547970652F47726F75702F532F5472616E73706172656E63792F43532F4465766963655247423E3E2F546162732F532F537472756374506172656E747320303E3E0D0A656E646F626A0D0A342030206F626A0D0A3C3C2F46696C7465722F466C6174654465636F64652F4C656E677468203133323E3E0D0A73747265616D0D0A789C758EBB0AC2401444FB0BF71FA68CCD3ECC4A56098164B3BE6045F 076216DACFCFFD6A4106E230353CD1C0EEC136D6B4BBA8D705D87614C1884C99E3D42842C4C1E6E8D47B34768A2A9211F2687F75617A6A992FC92137A913E5D4B7EC86E86DC99F28AD950BFFF211C4DA81563AAA0A6C825014AC72B1DEDE082897F015F013F293F0D0A656E6473747265616D0D0A656E646F626A0D0A352030206F626A0D0A3C3C2F547970652F466F6E742F537562747970652F54727565547970652F4E616D652F46312F42617365466F6E742F417269616C2F456E636F64696E672F57696E416E7369456E636F64696E672F466F6E7444657363726970746F722036203020522F4669727374436861722033322F4C617374436861722038342F576964746873203135203020523E3E0D0A656E646F626A0D0A362030206F626A0D0A3C3C2F547970652F466F6E7444657363726970746F722F466F6E744E616D652F417269616C2F466C6167732033322F4974616C6963416E676C6520302F417363656E74203930352F44657363656E74202D3231302F436170486569676874203732382F4176675769647468203434312F4D6178576964746820323636352F466F6E74576569676874203430302F58486569676874203235302F4C656164696E672033332F5374656D562034342F466F6E7442426F785B202D363635202D3231302032303030203732385D203E3E0D0A656 E646F626A0D0A372030206F626A0D0A3C3C2F417574686F7228526F7279204861696768292F43726561746F7228FEFF004D006900630072006F0073006F0066007400AE0020004F0066006600690063006500200057006F0072006400200032003000300037292F4372656174696F6E4461746528443A323031323032303730383535333529202F4D6F644461746528443A323031323032303730383535333529202F50726F647563657228FEFF004D006900630072006F0073006F0066007400AE0020004F0066006600690063006500200057006F0072006400200032003000300037293E3E0D0A656E646F626A0D0A31332030206F626A0D0A3C3C2F547970652F4F626A53746D2F4E20362F46697273742033362F46696C7465722F466C6174654465636F64652F4C656E677468203135363E3E0D0A73747265616D0D0A789C6D8F4D0A83400C85F78277783798445B3A05F102820CEA4E5C14095DB596E908F5F69D58A185BACADF97F7126610F880FC08269C08161C9333D8C64E868C2C8AC2389D121AD31A67AA1E34C0B82B72ED95659AEC20BC87D88D68650C2BA4F68D1E10C3808DEB96879836F8790C9D1769A6291877F1725FCB789C8AE8367DD67E86B5BC42250B7893AAE7DBB3D717B3AFC39F6D9ABC01665C3E810D0A656E6473747265616D0D0A656E646F626A0D0A3135203 0206F626A0D0A5B2032373820302030203020302030203020302030203020302030203020302030203020302030203020302030203020302030203020302032373820302030203020302030203020363637203020373232203020363637203020302037323220302030203020302038333320373232203020302030203020363637203631315D200D0A656E646F626A0D0A31362030206F626A0D0A3C3C2F547970652F585265662F53697A652031362F575B2031203420325D202F526F6F742031203020522F496E666F2037203020522F49445B3C31354437323535423842423230323433414234314331454541443545433639443E3C31354437323535423842423230323433414234314331454541443545433639443E5D202F46696C7465722F466C6174654465636F64652F4C656E6774682036363E3E0D0A73747265616D0D0A789C63600082FFFF1981A420030388AA81505BC014E30230C59407A6987920D41BA00850092F0333846281504C100A2AC70A54C9720DC66304F258813C00F4B307760D0A656E6473747265616D0D0A656E646F626A0D0A787265660D0A302031370D0A3030303030303030303820363535333520660D0A30303030303030303137203030303030206E0D0A30303030303030313234203030303030206E0D0A30303030303030313830203030303030206 E0D0A30303030303030343136203030303030206E0D0A30303030303030363232203030303030206E0D0A30303030303030373830203030303030206E0D0A30303030303031303034203030303030206E0D0A3030303030303030303920363535333520660D0A3030303030303030313020363535333520660D0A3030303030303030313120363535333520660D0A3030303030303030313220363535333520660D0A3030303030303030313320363535333520660D0A3030303030303030313420363535333520660D0A3030303030303030303020363535333520660D0A30303030303031343934203030303030206E0D0A30303030303031363433203030303030206E0D0A747261696C65720D0A3C3C2F53697A652031372F526F6F742031203020522F496E666F2037203020522F49445B3C31354437323535423842423230323433414234314331454541443545433639443E3C31354437323535423842423230323433414234314331454541443545433639443E5D203E3E0D0A7374617274787265660D0A313930380D0A2525454F460D0A787265660D0A3020300D0A747261696C65720D0A3C3C2F53697A652031372F526F6F742031203020522F496E666F2037203020522F49445B3C31354437323535423842423230323433414234314331454541443545433639443E3C3135443 7323535423842423230323433414234314331454541443545433639443E5D202F5072657620313930382F5852656653746D20313634333E3E0D0A7374617274787265660D0A323430330D0A2525454F46

提前致谢。

3 个答案:

答案 0 :(得分:2)

String data = "255044462D312E350D0A25B5B5B5B50D0A312030206F626A0"
        + "D0A3C3C2F547970652F436174616C6F672F50616765732032203"
        + "020522F4C616E6728656E2D474229202F5374727563745472656"
        + "5526F6F742038203020522F4D61726B496E666F3C3C2F4D61726"
        + "B656420747275653E3E3E3E0D0A656E646F626A0D0A322030206"
        + "F626A0D0A3C3C2F547970652F50616765732F436F756E7420312"
        + "F4B6964735B2033203020525D203E3E0D0A656E646F626A0D0A3"
        + "32030206F626A0D0A3C3C2F547970652F506167652F506172656"
        + "E742032203020522F5265736F75726365733C3C2F466F6E743C3"
        + "C2F46312035203020523E3E2F50726F635365745B2F5044462F5"
        + "46578742F496D616765422F496D616765432F496D616765495D2"
        + "03E3E2F4D65646961426F785B20302030203834312E393220353"
        + "9352E33325D202F436F6E74656E74732034203020522F47726F7"
        + "5703C3C2F547970652F47726F75702F532F5472616E737061726"
        + "56E63792F43532F4465766963655247423E3E2F546162732F532"
        + "F537472756374506172656E747320303E3E0D0A656E646F626A0"
        + "D0A342030206F626A0D0A3C3C2F46696C7465722F466C6174654"
        + "465636F64652F4C656E677468203133323E3E0D0A73747265616"
        + "D0D0A789C758EBB0AC2401444FB0BF71FA68CCD3ECC4A5609816"
        + "4B3BE6045F076216DACFCFFD6A4106E230353CD1C0EEC136D6B4"
        + "BBA8D705D87614C1884C99E3D42842C4C1E6E8D47B34768A2A92"
        + "11F2687F75617A6A992FC92137A913E5D4B7EC86E86DC99F28AD"
        + "950BFFF211C4DA81563AAA0A6C825014AC72B1DEDE082897F015"
        + "F013F293F0D0A656E6473747265616D0D0A656E646F626A0D0A3"
        + "52030206F626A0D0A3C3C2F547970652F466F6E742F537562747"
        + "970652F54727565547970652F4E616D652F46312F42617365466"
        + "F6E742F417269616C2F456E636F64696E672F57696E416E73694"
        + "56E636F64696E672F466F6E7444657363726970746F722036203"
        + "020522F4669727374436861722033322F4C61737443686172203"
        + "8342F576964746873203135203020523E3E0D0A656E646F626A0"
        + "D0A362030206F626A0D0A3C3C2F547970652F466F6E744465736"
        + "3726970746F722F466F6E744E616D652F417269616C2F466C616"
        + "7732033322F4974616C6963416E676C6520302F417363656E742"
        + "03930352F44657363656E74202D3231302F43617048656967687"
        + "4203732382F4176675769647468203434312F4D6178576964746"
        + "820323636352F466F6E74576569676874203430302F584865696"
        + "76874203235302F4C656164696E672033332F5374656D5620343"
        + "42F466F6E7442426F785B202D363635202D32313020323030302"
        + "03732385D203E3E0D0A656E646F626A0D0A372030206F626A0D0"
        + "A3C3C2F417574686F7228526F7279204861696768292F4372656"
        + "1746F7228FEFF004D006900630072006F0073006F0066007400A"
        + "E0020004F0066006600690063006500200057006F00720064002"
        + "00032003000300037292F4372656174696F6E4461746528443A3"
        + "23031323032303730383535333529202F4D6F644461746528443"
        + "A323031323032303730383535333529202F50726F64756365722"
        + "8FEFF004D006900630072006F0073006F0066007400AE0020004"
        + "F0066006600690063006500200057006F0072006400200032003"
        + "000300037293E3E0D0A656E646F626A0D0A31332030206F626A0"
        + "D0A3C3C2F547970652F4F626A53746D2F4E20362F46697273742"
        + "033362F46696C7465722F466C6174654465636F64652F4C656E6"
        + "77468203135363E3E0D0A73747265616D0D0A789C6D8F4D0A834"
        + "00C85F78277783798445B3A05F102820CEA4E5C14095DB596E90"
        + "8F5F69D58A185BACADF97F7126610F880FC08269C08161C9333D"
        + "8C64E868C2C8AC2389D121AD31A67AA1E34C0B82B72ED95659AE"
        + "C20BC87D88D68650C2BA4F68D1E10C3808DEB96879836F8790C9"
        + "D1769A6291877F1725FCB789C8AE8367DD67E86B5BC42250B789"
        + "3AAE7DBB3D717B3AFC39F6D9ABC01665C3E810D0A656E6473747"
        + "265616D0D0A656E646F626A0D0A31352030206F626A0D0A5B203"
        + "2373820302030203020302030203020302030203020302030203"
        + "0203020302030203020302030203020302030203020302030203"
        + "0203237382030203020302030203020302036363720302037323"
        + "2203020363637203020302037323220302030203020302038333"
        + "320373232203020302030203020363637203631315D200D0A656"
         + "E646F626A0D0A31362030206F626A0D0A3C3C2F547970652F585"
        + "265662F53697A652031362F575B2031203420325D202F526F6F7"
        + "42031203020522F496E666F2037203020522F49445B3C3135443"
        + "7323535423842423230323433414234314331454541443545433"
        + "639443E3C3135443732353542384242323032343341423431433"
        + "1454541443545433639443E5D202F46696C7465722F466C61746"
        + "54465636F64652F4C656E6774682036363E3E0D0A73747265616"
        + "D0D0A789C63600082FFFF1981A420030388AA81505BC014E3023"
        + "0C59407A6987920D41BA00850092F0333846281504C100A2AC70"
        + "A54C9720DC66304F258813C00F4B307760D0A656E64737472656"
        + "16D0D0A656E646F626A0D0A787265660D0A302031370D0A30303"
        + "03030303030303820363535333520660D0A30303030303030303"
        + "137203030303030206E0D0A30303030303030313234203030303"
        + "030206E0D0A30303030303030313830203030303030206E0D0A3"
        + "0303030303030343136203030303030206E0D0A3030303030303"
        + "0363232203030303030206E0D0A3030303030303037383020303"
        + "0303030206E0D0A30303030303031303034203030303030206E0"
        + "D0A3030303030303030303920363535333520660D0A303030303"
        + "0303030313020363535333520660D0A303030303030303031312"
        + "0363535333520660D0A303030303030303031322036353533352"
        + "0660D0A3030303030303030313320363535333520660D0A30303"
        + "03030303030313420363535333520660D0A30303030303030303"
        + "03020363535333520660D0A30303030303031343934203030303"
        + "030206E0D0A30303030303031363433203030303030206E0D0A7"
        + "47261696C65720D0A3C3C2F53697A652031372F526F6F7420312"
        + "03020522F496E666F2037203020522F49445B3C3135443732353"
        + "5423842423230323433414234314331454541443545433639443"
        + "E3C3135443732353542384242323032343341423431433145454"
        + "1443545433639443E5D203E3E0D0A7374617274787265660D0A3"
        + "13930380D0A2525454F460D0A787265660D0A3020300D0A74726"
        + "1696C65720D0A3C3C2F53697A652031372F526F6F74203120302"
        + "0522F496E666F2037203020522F49445B3C31354437323535423"
        + "842423230323433414234314331454541443545433639443E3C3"
        + "1354437323535423842423230323433414234314331454541443"
        + "545433639443E5D202F5072657620313930382F5852656653746"
        + "D20313634333E3E0D0A7374617274787265660D0A323430330D0"
        + "A2525454F46";

BigInteger hex = new BigInteger(data.substring(2), 16);
byte[] byte_data = hex.toByteArray();

ByteArrayInputStream input = new ByteArrayInputStream(byte_data);
FileOutputStream output = new FileOutputStream(new File("/home/user/file.pdf"));

byte[] buffer = new byte[1024 * 4];
int n = 0;
while (-1 != (n = input.read(buffer))) {
    output.write(buffer, 0, n);
}

output.close();
input.close();

该文件包含TEST : ATTACHMENT

答案 1 :(得分:1)

public static byte[] hex2bin(String data) {
    if ((data.length() % 2) != 0) throw new IllegalArgumentException();
    int len = data.length() / 2;
    byte[] out = new byte[len];
    for (int i = 0; i < len; i++) {
        out[i] = (byte)(
            (Character.digit(data.charAt(i * 2 + 0), 16) << 4) |
            (Character.digit(data.charAt(i * 2 + 1), 16))
        );
    }
    return out;
}

(首先修剪“0x”。)例如,

byte[] binData = hex2bin("0x255044462D312E350D0A25B5...".substring(2));

答案 2 :(得分:0)

Base64是一个专门为此设计的解码器 - 将数据编码/解码到ASCII /从ASCII。

但您在此处提供的数据看起来不像Base64。它看起来像一个很长的十六进制字符串。注意前导'0x'和正在使用的字符(0-9,A-F)。我会尝试将它们解码为字节对。

但是,了解正在使用的数据库产品以了解它如何编码“图像”会更容易。