2013年9月15日星期日

Help, read the local image into a byte array

Want java read the image, turn into byte format stored in the database on the Internet looking for a long time , are not OK .
help you big ox , thank you !



For example, I want to upload this picture
File image = new File (". / images / header.jpg");

I find how to convert it ?


------ Solution ------------------------------------ --------
read the image as a stream, and then use the stream to get a byte array , so the
------ Solution ----------- ---------------------------------
BufferedInputStream in = new BufferedInputStream (new FileInputStream ("f :/ 2.jpg"));
ByteArrayOutputStream out = new ByteArrayOutputStream (1024);

byte [] temp = new byte [1024]; ;
int size = 0;
while ((size = in.read (temp))! = -1) {
out.write (temp, 0, size); ;
}
in.close ();
byte [] content = out.toByteArray ();
------ For reference only ---------------------------------------
Why do you have to put pictures into the database it put into the database path , the path is not directly read better , the way you pass the database , read it again coming out from the inside , do you use the input stream , it is also used to write the output stream . Too cumbersome
------ For reference only ------------------------------------ ---
have to get Thank you !
------ For reference only -------------------------------------- -
have to get Thank you !
------ For reference only -------------------------------------- -
how to give a method to get the ah ah
------ For reference only -------------------- -------------------
ask how to solve ? You can give a way to do this ? Thank you very much !
------ For reference only -------------------------------------- -
this raised questions and then say get knot stickers, and subsidies for the solutions people are S B, pure S B
------ For reference only --- ------------------------------------
public static byte [] imageToByteArray ( String imgPath) {
BufferedInputStream in;
try {
in = new BufferedInputStream (new FileInputStream (imgPath));
ByteArrayOutputStream out = new ByteArrayOutputStream ();
int size = 0;
byte [] temp = new byte [1024];
while ((size = in.read (temp))! = -1) {
out.write (temp, 0, size);
}
in.close ();
return out.toByteArray ();
} catch (IOException e) {
e.printStackTrace ();
return null;
}
}
------ For reference only --------------------------------- ------

没有评论:

发表评论