Advertisement
Guest User

Untitled

a guest
Jul 31st, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. Blob imageBlob = resultSet.getBlob(yourBlobColumnIndex);
  2. InputStream binaryStream = imageBlob.getBinaryStream(0, imageBlob.length());
  3.  
  4. byte[] imageBytes = imageBlob.getBytes(1, (int) imageBlob.length());
  5.  
  6. InputStream binaryStream = resultSet.getBinaryStream(yourBlobColumnIndex);
  7.  
  8. final String dbURL = "jdbc:mysql://localhost:3306/portfolio";
  9. final String dbUser = "root";
  10. final String dbPass = "";
  11.  
  12. Connection conn = null;
  13. Statement stmt = null;
  14.  
  15. try {
  16. //DriverManager.registerDriver(new com.mysql.jdbc.Driver());
  17. Class.forName("com.mysql.jdbc.Driver");
  18.  
  19. conn = DriverManager.getConnection(dbURL, dbUser, dbPass);
  20. System.out.println("db connected");
  21. stmt = (Statement) conn.createStatement();
  22.  
  23. ResultSet rs1;
  24. rs1 = stmt.executeQuery("select profileImage from tbl_welcome where id = 1117");
  25.  
  26. if (rs1.next()) {
  27. byte[] imgData = rs1.getBytes("profileImage");//Here r1.getBytes() extract byte data from resultSet
  28. System.out.println(imgData);
  29. response.setHeader("expires", "0");
  30. response.setContentType("image/jpg");
  31.  
  32. OutputStream os = response.getOutputStream(); // output with the help of outputStream
  33. os.write(imgData);
  34. os.flush();
  35. os.close();
  36.  
  37. }
  38. } catch (SQLException ex) {
  39. // String message = "ERROR: " + ex.getMessage();
  40. ex.printStackTrace();
  41. } finally {
  42. if (conn != null) {
  43. // closes the database connection
  44. try {
  45. conn.close();
  46. } catch (SQLException ex) {
  47. ex.printStackTrace();
  48. }
  49. }
  50. }
  51.  
  52. private void loadFileDataBlobFromDataBase()
  53. {
  54. List<Blob> bFile = jdbcTemplate.query(sql, new RowMapper<Blob>() {
  55. @Override
  56. public Blob mapRow(ResultSet rs, int rowNum)
  57. throws SQLException {
  58. return rs.getBlob(1);
  59. }
  60. });
  61. if (bFile != null && bFile.size() > 0) {
  62. bufReader = new BufferedReader(new InputStreamReader(bFile.get(
  63. 0).getBinaryStream()));
  64. }
  65. if (null != bufReader) {
  66. dataVO record = null;
  67. String lineStr = bufReader.readLine();
  68. record = (dataVO) lineMapper.mapLine(lineStr, 1);
  69. }
  70. }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement