Click here to Skip to main content
14,767,350 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more: , +
I have a code for saving an image to database but not yet cropped, to save an image to database I'm using "FileInputStream"
FileInputStream imgByte;
File imgLocation = new File(image_location);
stmt =con.prepareStatement("INSERT INTO image(image_id, image_data, image_name, image_ext)VALUES (?, ?, ?, ?);");
imgByte = new FileInputStream(imgLocation);
stmt.setInt(1, image_id+1);
stmt.setBinaryStream(2, (InputStream)imgByte, (int)(imgLocation.length()));
stmt.setString(3, image_name);
stmt.setString(4, image_ext);

I also have a code for cropping an image and displaying the cropped image, this code is for Servlet, I also used jquery(cropped) and jsp(send data/value to servlet) to get the variables data/value("t", "l", "w", "h")
int t=Integer.parseInt(req.getParameter("t"));
int l=Integer.parseInt(req.getParameter("l"));
int w=Integer.parseInt(req.getParameter("w"));
int h=Integer.parseInt(req.getParameter("h"));
String imagePath =                                                                        getServletContext().getRealPath("/")+req.getParameter("i");
BufferedImage File(imagePath));
BufferedImage cropped=outImage.getSubimage(l, t, w, h);
ByteArrayOutputStream out=new ByteArrayOutputStream();
ImageIO.write(cropped,req.getParameter("f"), out);
				new File(getServletContext().getRealPath("")+System.getProperty("file.separator")+"cropped.jpg"));

ServletOutputStream wrt=res.getOutputStream();

Now my problem is the Array of Byte in "out" variable is a "ByteArrayOutputStream" datatype how can I convert that to "FileInputStream" where the "FileInputStream" used in saving the image to database.
Updated 26-Feb-11 2:23am

1 solution

Read the bytes from the output stream; try byte[] bytes = out.toByteArray().

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900