IBATIS/myBatis
2017.04.27 / 21:36

MyBatis¸¦ ÀÌ¿ëÇؼ­ À̹ÌÁö ÆÄÀÏÀ» DB¿¡ ÀúÀåÇÏ°í ºÒ·¯¿À±â

Scoda
Ãßõ ¼ö 214

MyBatis ¹öÀü: 3.2.7

MySQL ¹öÀü: 5.6.20-log

OS: Windows 7


Word ¹®¼­¸¦ parsing Çؼ­ À̹ÌÁö ÆÄÀÏÀ» ÃßÃâÇؼ­ Database¿¡ ³Ö°í ½Í¾ú´Ù.


À̹ÌÁö¸¦ byte[] ÀÇ ÇüÅ·Π»©¿Ã ¼ö Àִµ¥, MySQL ÀÇ BLOB Data Type Ä÷³¿¡ 


byte[] µ¥ÀÌÅ͸¦ ±×³É Map<String,Object> ¿¡ ³Ö¾î¼­ insert ÇØ ÁÖ¸é BLOB Ä÷³¿¡ ±×³É µé¾î°£´Ù.


Java ¿¡¼­ byte Array ¸¦ blob Data Type À¸·Î º¯È¯ÇØ ÁÖ´Â ¹æ¹ýÀÌ ÀÖ±â´Â Çѵ¥, MyBatis ¿¡¼­ ÀÚµ¿À¸·Î µÇ´Â °Í °°´Ù.

ÀÌ·¸°Ô Çϳª º¯È¯À» ¾ÈÇØ ÁÖ³ª °á°ú°¡ °°¾Ò´Ù.


How to convert byte array to blob

http://stackoverflow.com/questions/10849893/how-to-convert-byte-array-to-blob


Blob blob = new javax.sql.rowset.serial.SerialBlob(bytes);


HeidiSQL Tool ¿¡¼­, µ¥ÀÌÅÍ°¡ µé¾îÀÖ´Â BLOB Ä÷³À» ´õºí Ŭ¸¯Çؼ­ ³»¿ëÀ» º¸´Ï, 235 bytes ¹Û¿¡ Ç¥½Ã°¡ ¾È µÇ¼­, ¹º°¡ À߸ø µé¾î°£ ÁÙ ¾Ë¾Ò´õ´Ï,


SELECT LENGTH({BLOB Ä÷³ À̸§}) FROM {Å×À̺í À̸§} Çؼ­ µ¥ÀÌÅÍÀÇ ±æÀ̸¦ ÃøÁ¤ÇØ ºÃ´õ´Ï, À̹ÌÁö ÆÄÀÏÀÇ Å©±â ¸¸Å­ Á¦´ë·Î µé¾î°¡ ÀÖ¾ú´Ù.


Data¸¦ »© ¿Ã ¶§µµ Map<String, Object> ÇüÅ·Π¹Ýȯ ¹Þ¾Æ¼­, MAP ÀÇ get() À¸·Î ²¨³»¿Í¼­ byte[] ·Î Çüº¯È¯ÇÏ¸é ±×´ë·Î ²¨³»¿ÍÁø´Ù. ±×·¡¼­ À̹ÌÁö·Î ÀúÀåÇÒ ¼öµµ ÀÖ´Ù.


  1. private static void saveImage(byte[] bytes, String fileName) {

  2.     BufferedImage image = null;
  3.     try {
  4.         image = ImageIO.read(new ByteArrayInputStream(bytes));
  5.     } catch (IOException e1) {
  6.         e1.printStackTrace();
  7.     }

  8.     String filePath = "D:\\temp\\";
  9.     File outputfile = new File(filePath + fileName);

  10.     String extension = fileName.substring(fileName.lastIndexOf('.')+1);

  11.     try {
  12.         ImageIO.write(image, extension, outputfile);
  13.     } catch (IOException e) {
  14.         e.printStackTrace();
  15.     }
  16. }


Âü°íÀûÀ¸·Î hexString À¸·Î ÀúÀåµÈ ¹®ÀÚ¿­À» byte[] ·Î ¹Ù²Ù´Â ÄÚµùÀº ´ÙÀ½°ú °°´Ù.

¾Æ·¡ Äڵ忡¼­ png ÆÄÀÏÀÇ ¸ðµç À̹ÌÁö µ¥ÀÌÅ͸¦ String À¸·Î ¸¸µç °ÍÀÌ ¾Æ´Ï¶ó ¾Õ ºÎºÐ¸¸ À߶ó ³õÀº °ÍÀÌ¶ó¼­ À̹ÌÁö ÆÄÀÏÀÌ ¸¸µé¾îÁöÁö´Â ¾Ê´Â´Ù.


  1. public static void main(String[] args) {


  2.     ArrayList<Byte> byteList = new ArrayList<Byte>();
  3.     String string = "89504E470D0A1A0A0000000D49484452000004560000013F02000000203F000000000173524742003F1C3F0000000467414D410000B18F0B3F0500000009704859730000171100001711013F3F0000657449444154785EEDDD79A04C3FC0F1BBB83F84643F3F212D5289423FA9944A3F4AF5D3373F642D51A1A83F3F49423F5BF6EC3FB9DC7B3F667E3F3F4D771973673F79C6BC5F7F709E673F673F96E73F3F4887C31101000000003F4A3F000000006180140800000040182105020000001046483F000000003F522000000000613F080000004018210502000008312929293F3F";

  4.     for(int i=0; i<string.length(); i=i+2) {
  5.         int parseInt = Integer.parseInt(string.substring(i, i+2), 16);
  6.         byteList.add((byte)parseInt);
  7.     }

  8.     Byte[] byteObjects = byteList.toArray(new Byte[byteList.size()]);

  9.     byte[] bytes = new byte[byteObjects.length];

  10.     int i=0;
  11.     for(byte b: byteObjects)
  12.         bytes[i++] = b;

  13.     saveImage(bytes, "temp.png");
  14. }


[Âü°í]

java¿¡¼­ hexString À» int·Î º¯È¯ ( hexString to int )



Spring3 MVC¿Í MyBatis¸¦ ÀÌ¿ëÇÏ¿© À̹ÌÁö¸¦ BLOB ŸÀÔÀ¸·Î DB ÀúÀå ¹× Ãâ·ÂÇغ¸±â


http://hellogk.tistory.com/129


how to convert byte[] to Byte[], and the other way around?


Get Image from the document using Apache POI



Ãâó: http://bryan7.tistory.com/481 [¹Î¼­³×Áý]