how to convert image into bytes and also retrive the image from that bytes

Ads

 
 

Share on Google+Share on Google+

Senthil kumar
how to convert image into bytes and also retrive the image from that bytes
2 Answer(s)      8 years ago
Posted in : Java Beginners
Ads
View Answers

December 30, 2008 at 3:04 AM


Hi friend,

Code to help in solving the problem :

import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class DisplayImage extends HttpServlet{
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException{
//PrintWriter pw = response.getWriter();
String connectionURL = "jdbc:mysql://192.168.10.59:3306/example";;
java.sql.Connection con=null;
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
con=DriverManager.getConnection(connectionURL,"root","root");
Statement st1=con.createStatement();
ResultSet rs1 = st1.executeQuery("select image from pictures where id='5'");
String imgLen="";
if(rs1.next()){
imgLen = rs1.getString(1);
System.out.println(imgLen.length());
}
rs1 = st1.executeQuery("select image from pictures where id='5'");
if(rs1.next()){
int len = imgLen.length();
byte [] rb = new byte[len];
InputStream readImg = rs1.getBinaryStream(1);
int index=readImg.read(rb, 0, len);
System.out.println("index"+index);
st1.close();
response.reset();
response.setContentType("image/jpg");
response.getOutputStream().write(rb,0,len);
response.getOutputStream().flush();
}
}
catch (Exception e){
e.printStackTrace();
}
}
}

Thanks



February 20, 2011 at 1:27 AM


This exemple show you how to store byte array in data base and retrive it again. with postgres or mysql using java


Ads









Related Tutorials/Questions & Answers:
how to convert image into bytes and also retrive the image from that bytes - Java Beginners
how to convert image into bytes and also retrive the image from that bytes  how to convert image into bytes and also retrive the image from... = st1.executeQuery("select image from pictures where id='5'"); String imgLen
How to use bytes
How to use bytes  I have saved my project and uploaded the picture...(rs.getBytes("employeephoto")); How to get the correct answer to view the picture by using bytes?   InputStream sImage; if(rs.next()){ byte[] bytearray
Advertisements
How to get bytes from ByteBuffer in java.
How to get bytes from ByteBuffer in java.       ...;  In this tutorial, we will discuss how to get bytes from buffer...() method of ByteBuffer class  reads bytes of from  buffer's
How to read bytes from a Linked list - Java Beginners
How to read bytes from a Linked list  i have stored byte array into a linked list. How to read the bytearray dta byte after byte from a linked list.Thanking u in advance Sameer
store and retrive image from the database
store and retrive image from the database  please provide me with the code and the explanation of what each line does for the below query. -how to store and retreive images from sql database using sql commands -how to store
how to retrive other details with an image - Swing AWT
how to retrive other details with an image  hello I m trying to retrive an image as well as some other values from database.. but only image..... followingis my code..plz advice me how to get both values as well as image at a same
store & retrive the image from oracle database
store & retrive the image from oracle database  how can i store the image path in tha database & fetch that image from oracle database
retrive image from database using jsp without stream
retrive image from database using jsp without stream  How to retrive image from database using jsp without stream like (inputStream
Java file bytes
Java file bytes In this section, you will learn how to read bytes from a file... input bytes from a file in a file system. read()-  This is the method... InputStream class along with the FileInputstream class to obtain the byte of data from
store and retrive image from database - JDBC
store and retrive image from database  how to store and retrive an image in database using java?  Hi friend, Code for store image...()); } } } For retrieve image from database visit to : http
image retrive into DataBase - JDBC
image retrive into DataBase  Dear Deepak Sir, How to retrive... shows you how to retrieve image from database using a servlet and then show..., Retrieving image from database is easy task. JDBC provides all the necessary API
insert and retrive image from sql server database in jsp
insert and retrive image from sql server database in jsp  what is the code to insert and retrive an image from sql server database in jsp
how to convert string to image in java
how to convert string to image in java  how to convert string to image in java? I know we need to convert image to a byte array at some point in my application but do not know how to convert a image into byte string. Please
Order bytes from most significant to least significant.
Order bytes from most significant to least significant. In this tutorial we will discuss on how to use the ByteOrder class for ordering bytes from most significant to a least significant. Code: import 
Order bytes from least significant to most significant.
Order bytes from least significant to most significant. In this tutorial we will discuss on how to use the ByteOrder class for ordering bytes from least significant to a most significant. Code: import 
how to display or retrive an image in jsp whose path is stored in oracle database
how to display or retrive an image in jsp whose path is stored in oracle database  how to display or retrive an image in jsp whose path is stored in oracle database and the image is stored in my pictures folder
retrive the employee details with image from mysql database using jsp servlet
retrive the employee details with image from mysql database using jsp servlet  im doing the web project to retrive the employee profile which i stored in the database using jsp servlet then want to show the result in the next jsp
1024 bytes equals to
1024 bytes equals to  1024 bytes equals to   1024 bytes equals to 1 KB
concatenation of Two bytes in C
concatenation of Two bytes in C  Hi, I need to concatenate two bytes array in C.How to do that. bytes[0] = (n >> 24) & 0xFF; bytes[1] = (n >> 16) & 0xFF; bytes[2] = (n >> 8) & 0xFF; bytes[3] = n &
how to convert image file to text file?
how to convert image file to text file?  hi, can anybody tell how to convert image file to text file? plz help me
Place array of bytes in buffer
Place array of bytes in buffer In this tutorial you will see how to place array of bytes in buffer. The put method of CharBuffer class transfers the entire content of the given source character array into a buffer. Code
How to retrieve image from database
How to retrieve image from database  hi........ How to retrieve image from database when it is stored. I have stored image in postgresql and want to retrieve in java form. Can u tel me hw to do? I am trying n able to do also
How to retrive an inmage from database and displaying it in imgae box - Swing AWT
How to retrive an inmage from database and displaying it in imgae box ...? I want to retrive an image from database and to display it in a small imagebox... = con.createStatement(); ResultSet rs =stmt.executeQuery("select image from image where image
how to fetch image from mysql using jsp
how to fetch image from mysql using jsp  how to fetch image from mysql using jsp
how to store and retrieve image from database
how to store and retrieve image from database  how to store and retrieve images into database(oracle) and how to retrive images from database using jsp   Here is a jsp code that insert and retrieve image from mysql
How to store and retrieve image from database in JSP?
How to store and retrieve image from database in JSP?  Hi, In one of my application I have to store and then display the image in JSP. How to store and retrieve image from database in JSP? Thanks   HI, You can use
image
image   Dear every body please help me how to add and retrive image and video into oracle 11g using jsp
How to retrieve image from database in Servlet?
How to retrieve image from database in Servlet?  Hi, How to retrieve image from database in Servlet? Thanks   Hi, Please check the tutorial Retrieve image from database using Servlet. Thanks
How to retrieve image from mysql database in JSP?
How to retrieve image from mysql database in JSP?  Hi, I need JSP same codes for learning to get image which is stored in MySQL Database. How to retrieve image from mysql database in JSP? Thanks   Hi, You can write
How to retrieve blob image from database in JSP?
How to retrieve blob image from database in JSP?  Hello, JSP is used... the blog image from database. How to retrieve blob image from database in JSP?   Hi, Please check the tutorial Retrieve image from database using
How to retrieve and display image from database in Java?
How to retrieve and display image from database in Java?  Hi, I am.... Please check the it at How to store and retrieve image from database in JSP... on the web page. My application is in Java/JSP. How to retrieve and display image