2010-01-27 12 views
15

पर बीएलओबी के रूप में संग्रहीत एक छवि पुनर्प्राप्त करें मैं डेटाबेस पर मौजूद जानकारी के आधार पर एक पीडीएफ बनाने की कोशिश कर रहा हूं। मुझे पता है कि मुझे एक टीआईएफएफ छवि पुनर्प्राप्त करने की आवश्यकता है जिसे जावा से एक MySQL डेटाबेस पर बीएलओबी के रूप में संग्रहीत किया जाता है। और मुझे नहीं पता कि यह कैसे करना है। मेरे द्वारा प्राप्त किए गए उदाहरण दिखाते हैं कि इसे कैसे पुनर्प्राप्त करें और इसे फ़ाइल (लेकिन डिस्क पर) के रूप में सहेजें और मुझे स्मृति पर रहने की आवश्यकता है।एक MYSQL डीबी

तालिका नाम: IMAGENES_REGISTROS

ब्लॉब फील्ड नाम: IMAGEN

किसी भी विचार?

+0

आप सादे JDBC का उपयोग कर रहे हैं? – Bozho

+0

हाँ बोझो, सादा जेडीबीसी जावा के लिए MySQL lib के साथ। – Sheldon

+0

मेरा अद्यतन उत्तर देखें - यह पता लगाने का एक वैकल्पिक तरीका है :) – Bozho

उत्तर

18

अपने ResultSet कॉल पर:

Blob imageBlob = resultSet.getBlob(yourBlobColumnIndex); 
InputStream binaryStream = imageBlob.getBinaryStream(0, imageBlob.length()); 

वैकल्पिक रूप से, आप कॉल कर सकते हैं:

byte[] imageBytes = imageBlob.getBytes(1, (int) imageBlob.length()); 

BalusC उसकी टिप्पणी में बताया गया है, तो आप बेहतर उपयोग कर सकेंगे:

InputStream binaryStream = resultSet.getBinaryStream(yourBlobColumnIndex); 

और तो कोड इस बात पर निर्भर करता है कि आप छवि को कैसे पढ़ और एम्बेड करने जा रहे हैं।

+0

धन्यवाद, बस एक टिप्पणी। imageBlob एक लंबा लौटाता है। और GetBytes एक इंटीजर की उम्मीद है। मैं पार्स (Integer.ParseInt ..) लांग, और यह काम करता है। मुझे नहीं पता कि आखिर में समस्याएं आ जाएंगी। क्या कोई अन्य तरीका है? धन्यवाद – Sheldon

+3

'परिणामसेट # getBlob() 'या' परिणामसेट # getBytes() 'का उपयोग न करें। बस 'ResultSet # getBinaryStream() 'का उपयोग करें। – BalusC

+0

क्या यह सिर्फ एक सुविधाजनक तरीका है, या इसके लिए कुछ और है? – Bozho

0
final String dbURL = "jdbc:mysql://localhost:3306/portfolio"; 
    final String dbUser = "root"; 
    final String dbPass = ""; 

    Connection conn = null; 
    Statement stmt = null; 

    try { 
     //DriverManager.registerDriver(new com.mysql.jdbc.Driver()); 
     Class.forName("com.mysql.jdbc.Driver"); 

     conn = DriverManager.getConnection(dbURL, dbUser, dbPass); 
     System.out.println("db connected"); 
     stmt = (Statement) conn.createStatement(); 

     ResultSet rs1; 
     rs1 = stmt.executeQuery("select profileImage from tbl_welcome where id = 1117"); 

     if (rs1.next()) { 
      byte[] imgData = rs1.getBytes("profileImage");//Here r1.getBytes() extract byte data from resultSet 
      System.out.println(imgData); 
      response.setHeader("expires", "0"); 
      response.setContentType("image/jpg"); 

      OutputStream os = response.getOutputStream(); // output with the help of outputStream 
      os.write(imgData); 
      os.flush(); 
      os.close(); 

     } 
    } catch (SQLException ex) { 
     // String message = "ERROR: " + ex.getMessage(); 
     ex.printStackTrace(); 
    } finally { 
     if (conn != null) { 
      // closes the database connection 
      try { 
       conn.close(); 
      } catch (SQLException ex) { 
       ex.printStackTrace(); 
      } 
     } 
    } 
+2

आपको उस जानकारी को शामिल करने के लिए अपना उत्तर संपादित करना चाहिए, कोई टिप्पणी नहीं जोड़ें। अधिक स्पष्टीकरण अच्छा है - कोड-केवल उत्तर निराश होते हैं। – Ajean

0
private void loadFileDataBlobFromDataBase() 
      { 
      List<Blob> bFile = jdbcTemplate.query(sql, new RowMapper<Blob>() { 
       @Override 
       public Blob mapRow(ResultSet rs, int rowNum) 
         throws SQLException { 
        return rs.getBlob(1); 
       } 
      }); 
      if (bFile != null && bFile.size() > 0) { 
       bufReader = new BufferedReader(new InputStreamReader(bFile.get(
         0).getBinaryStream())); 
      } 
      if (null != bufReader) { 
       dataVO record = null; 
       String lineStr = bufReader.readLine(); 
       record = (dataVO) lineMapper.mapLine(lineStr, 1);    
      } 
     } 
    } 
संबंधित मुद्दे