I am creating web application where I am storing image in MYSQL DB as MEDIUMBLOB and displaying them on the browser.
PreparedStatement psmt = con.prepareStatement("SELECT pdfImage FROM pdfInfo WHERE pdfinfoid=?");
psmt.setString(1, what);
ResultSet rs1 = psmt.executeQuery();
while (rs1.next()) {
System.out.println("inside rs1.mext");
String imgLen = rs1.getString(1);
System.out.println(imgLen.length());
int len = imgLen.length();
System.out.println("length inside == " + len);
byte[] rb = new byte[len];
InputStream readImg = rs1.getBinaryStream(1);
InputStream inputStream = readImg;
int index = readImg.read(rb, 0, len);
System.out.println("index----------------" + index);
response.reset();
response.setHeader("Content-Length", String.valueOf(len));
response.setHeader("Content-disposition", "inline;filename=/file.png");
response.setContentType("image/png");
response.getOutputStream().write(rb, 0, len);
response.getOutputStream().flush();
}
The problem is that I am able to see images locally and when I take project live. The problem is with one image only.
Output for the sout statements are as below.
INFO: inside rs1.mext
INFO: 5942414
INFO: length inside == 5942414
INFO: index----------------5942414
INFO: leng here is 5942414
When this info is on localhost, I can view the image, however when I take this project online, I am not able to view the image.
Do I need to set any parameter anywhere so that I can see the image.
Other sout statements are as below for which I can see images.
INFO: inside rs1.mext
INFO: 204999
INFO: length inside == 204999
INFO: index----------------204999
INFO: leng here is 204999
INFO: inside rs1.mext
INFO: 515274
INFO: length inside == 515274
INFO: index----------------515274
INFO: leng here is 515274
Your problem may be the line:
The
readmethod is not guaranteed to read all the data you are requesting.http://docs.oracle.com/javase/6/docs/api/java/io/InputStream.html#read(byte%5B%5D,%20int,%20int)
“Reads up to len bytes of data from the input stream into an array of bytes. An attempt is made to read as many as len bytes, but a smaller number may be read. The number of bytes actually read is returned as an integer.”