• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Image Upload

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an image in my database,and i want to display it on a jsp using struts.how can i write this in servlet ??
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Julie

You need to be specific about the type of database and the data type in which your images are stored (typically in Oracle: BLOB or BFILE, on older versions Long Raw).

For Oracle, you will find many useful examples on otn.oracle.com.

Below a simple example of a servlet that reads the content of a BLOB column - without struts. Works on Oracle 9i R2 with JDBC thin driver (taken from OTN). The servlet is called with the parameter "imageID" to pass the value of the primary key that corresponds to the image you want to display. It uses the Oracle specific type oracle.sql.BLOB
--------------------------------------------------------------------------
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import oracle.jdbc.driver.OracleResultSet;
import oracle.sql.BLOB;

public class ReadBlobServlet extends HttpServlet {
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// the lImageID is used to read the PK from the tables that containst the BLOB
long lImageID = new Long(request.getParameter("imageID")).longValue();
String sql = "select content from img where id = ?";
String connStr = "jdbc racle:thin:@host ort:instance";
String usrStr = "usr";

ServletOutputStream lOut = response.getOutputStream();
response.setContentType("text/html");

try {
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection conn = DriverManager.getConnection(connStr, usrStr,usrStr);
PreparedStatement ps = conn.prepareStatement(sql);
ps.setLong(1, lImageID);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
// using oracle.sql.BLOB and oracle.jdbc.driver.OracleResultSet datatype
BLOB b = ((OracleResultSet)rs).getBLOB(1);
// using the java.sql.Blob data type
// Blob b = rs.getBlob(1);
long len = b.length();
InputStream is = b.getBinaryStream();
byte[] data = new byte[(int) len];
int byte_read = is.read(data);

lOut.write(data);

}
} catch (SQLException sqle) {
sqle.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
lOut.close();
} // service
}
--------------------------------------------------------------------------
HTH and good luck
 
Ranch Hand
Posts: 429
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shouldn't there be a way to do it with a JNDI/JDBC Datasource, in a way that's not specific to a database vendor?

-Yuriy
 
reply
    Bookmark Topic Watch Topic
  • New Topic