I need to display a picture which I have saved in a database as a BLOB. Can anyone tell me if these codes are done correctly? Now, when I run, I get no errors however, its just a blank page. How do I allow the user to see my picture file?
Im using mySQL server.
Program is Sun ONE Studio (JSP)
For the uploading part, i used a java bean given by another website. So now, I only need to handle the download part. Please give me your advice. Thanks.
<% String url = "jdbc:mysql://localhost/test";
String userID="root";
String PWD="";
Connection conn = null;
String classPath= "com.mysql.jdbc.Driver";
try{
Class.forName(classPath).newInstance();
conn = DriverManager.getConnection(url,userID,PWD);
}catch(Exception exc){
out.println(exc.toString());
}
Statement stm = null;
ResultSet rst = null;
stm=conn.createStatement();
String strSQL = "SELECT binaryfile FROM uploads WHERE filename='baby.gif'";
rst = stm.executeQuery(strSQL);
if (rst.next()){
byte [] barray = rst.getBytes("binaryfile");
FileOutputStream fo = new FileOutputStream("display.gif");
for(int i=0; i < barray.length;i++){
fo.write(barray);
}
fo.close();
FileInputStream fi = new FileInputStream("display.gif");
int size = fi.available();
for (int i = 0; i <=size; i++){
System.out.print((char)fi.read());
}
fi.close();
}
rst.close();
stm.close();
conn.close();
%>
Im using mySQL server.
Program is Sun ONE Studio (JSP)
For the uploading part, i used a java bean given by another website. So now, I only need to handle the download part. Please give me your advice. Thanks.
<% String url = "jdbc:mysql://localhost/test";
String userID="root";
String PWD="";
Connection conn = null;
String classPath= "com.mysql.jdbc.Driver";
try{
Class.forName(classPath).newInstance();
conn = DriverManager.getConnection(url,userID,PWD);
}catch(Exception exc){
out.println(exc.toString());
}
Statement stm = null;
ResultSet rst = null;
stm=conn.createStatement();
String strSQL = "SELECT binaryfile FROM uploads WHERE filename='baby.gif'";
rst = stm.executeQuery(strSQL);
if (rst.next()){
byte [] barray = rst.getBytes("binaryfile");
FileOutputStream fo = new FileOutputStream("display.gif");
for(int i=0; i < barray.length;i++){
fo.write(barray);
}
fo.close();
FileInputStream fi = new FileInputStream("display.gif");
int size = fi.available();
for (int i = 0; i <=size; i++){
System.out.print((char)fi.read());
}
fi.close();
}
rst.close();
stm.close();
conn.close();
%>