1- MAKE NEW SERVLET
COPY TEXT FROM BELOW TO NEW SERVLET.
DEFINE IMAGE SOURCE WITH SERVLET NAME AND PARAMETER VALUE.
DISPLAY MAGE SERVLET CODING
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.sql.DataSource;
public class Servlet1 extends HttpServlet {
private static final String CONTENT_TYPE = "image/gif; charset=utf-8";
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
response.setContentType(CONTENT_TYPE);
String imageId = request.getParameter("id");
OutputStream os = response.getOutputStream();
Connection conn = null;
try {
Context ctx = new InitialContext();
conn = getOracleConnection();
PreparedStatement statement =
conn.prepareStatement("SELECT employee_id,pic " +
"FROM employees " +
"WHERE employee_id = ?");
statement.setInt(1, new Integer(imageId));
ResultSet rs = statement.executeQuery();
if (rs.next()) {
Blob blob = rs.getBlob("PIC");
BufferedInputStream in = new BufferedInputStream(blob.getBinaryStream());
int b;
byte[] buffer = new byte[10240];
while ((b = in.read(buffer, 0, 10240)) != -1) {
os.write(buffer, 0, b);
}
os.close();
}
} catch (Exception e) {
System.out.println(e);
} finally {
try {
if (conn != null) {
conn.close();
}
} catch (SQLException sqle) {
System.out.println("SQLException error");
}
}
}
public static Connection getOracleConnection() throws Exception {
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@localhost:1521:orcl";
String username = "hr";
String password = "hr";
Class.forName(driver); // load Oracle driver
Connection conn = DriverManager.getConnection(url, username, password);
return conn;
}
}
UPLOAD IMAGE CODING
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.SQLException;
import oracle.adf.model.BindingContext;
import oracle.adf.model.binding.DCBindingContainer;
import oracle.adf.model.binding.DCIteratorBinding;
import oracle.binding.BindingContainer;
import oracle.jbo.Row;
import oracle.jbo.domain.BlobDomain;
import org.apache.myfaces.trinidad.model.UploadedFile;
public class UploadBean {
public UploadBean() {
super();
}
private UploadedFile _file;
public UploadedFile getFile() {
return _file;
}
public void setFile(UploadedFile file) {
_file = file;
}
public String uploadImage() {
UploadedFile myfile = (UploadedFile)this.getFile();
BindingContext bindingctx = BindingContext.getCurrent();
BindingContainer bindings = bindingctx.getCurrentBindingsEntry();
DCBindingContainer bindingsImpl = (DCBindingContainer)bindings;
DCIteratorBinding iter = bindingsImpl.findIteratorBinding("ABCView1Iterator");
Row row = iter.getCurrentRow();
row.setAttribute("Pic", createBlobDomain(myfile));
return null;
}
private BlobDomain createBlobDomain(UploadedFile file) {
InputStream in = null;
BlobDomain blobDomain = null;
OutputStream out = null;
try {
in = file.getInputStream();
blobDomain = new BlobDomain();
out = blobDomain.getBinaryOutputStream();
byte[] buffer = new byte[8192];
int bytesRead = 0;
while ((bytesRead = in.read(buffer, 0, 8192)) != -1) {
out.write(buffer, 0, bytesRead);
}
in.close();
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
e.fillInStackTrace();
}
return blobDomain;
}
}
hi Adeel,
ReplyDeleteI need to display more than one blob image columns at the same time, how do i display multiple images using the http servlet? your help is urgently needed. thanks in advance
one servlet for one image simple :-) make as many servlet as many you want to display images.
ReplyDeleteHi Adeel,
DeleteThanx for this resource in your blog. When i tried this for single image i got error on this line.
error 1:
public class showimage extends HttpServlet {
......
error: Serializable class doesnot declare a static final....
error 2:
while ((b = in.read(buffer, 0, 10240)) != -1) {
error signal(red underline) can bee seen in your video also.
I did as U mentioned but I have no pics to be displayed so What Can I do? or What do You suggest!!!!
ReplyDeleteHello
ReplyDeleteI'm trying to upload employee images to the database but an exception 60097 + 60096 is raised
I used same code and instructions with no luck
I'm using jdeveloper 12c
Thanks
HI wedad ALsinan,
DeleteHave you had any luck in 12c?
HI ,
ReplyDeletegot error in this line while using in Jdeveloper 12c.
while ((b = in.read(buffer, 0, 10240)) != -1) {
Tks.
PreparedStatement statement =
ReplyDeleteconn.prepareStatement("SELECT employee_id,pic " +
"FROM employees " +
"WHERE employee_id = ?");
statement.setInt(1, new Integer(imageId));
how this query would help me get the image using the id sent from there . there is no assurance that id would match up with employee_id
THANKS NICE POST
ReplyDeletei have tried this code a lot of time using adf 12c but image is not displaying, eventhough code is working well , file is uploaded , picure is saved, but it is not saved in the database please help me out
ReplyDeletehi.. were u able to solve this issue ?. . i am stuck on kind a same thing..
Deleteappreciate if u could help me
Hi, I have tried the same steps. Image is inserted in db but unable to display. I am getting error while fetching. Java.net.socket.exception. Connection reset by peer. Request you to please suggest something.
ReplyDelete