create table photos ( id number(5) primary key, title varchar(50), photo blob );
<html> <head> <title>Add Photo</title> </head> <body> <h2>Add Photo</h2> <form id="form1" enctype="multipart/form-data" action="addphoto" method="post"> <table> <tr> <td>Enter Photo Id :</td> <td><input type="text" name="id"/></td> </tr> <tr> <td>Enter Title For Photo :</td> <td><input type="text" name="title"/></td> </tr> <tr> <td>Select Photo </td> <td><input type="file" name="photo" /> </tr> </table> <p/> <input type="submit" value="Add Photo"/> </form> <p/> <a href="listphotos">List Photos </a> </body> </html>
import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; public class AddPhotoServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { // Apache Commons-Fileupload library classes DiskFileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload sfu = new ServletFileUpload(factory); if (! ServletFileUpload.isMultipartContent(request)) { System.out.println("sorry. No file uploaded"); return; } // parse request List items = sfu.parseRequest(request); FileItem id = (FileItem) items.get(0); String photoid = id.getString(); FileItem title = (FileItem) items.get(1); String phototitle = title.getString(); // get uploaded file FileItem file = (FileItem) items.get(2); // Connect to Oracle Class.forName("oracle.jdbc.driver.OracleDriver"); Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "hr", "hr"); con.setAutoCommit(false); PreparedStatement ps = con.prepareStatement("insert into photos values(?,?,?)"); ps.setString(1, photoid); ps.setString(2, phototitle); // size must be converted to int otherwise it results in error ps.setBinaryStream(3, file.getInputStream(), (int) file.getSize()); ps.executeUpdate(); con.commit(); con.close(); out.println("Photo Added Successfully. <p> <a href='listphotos'>List Photos </a>"); } catch(Exception ex) { out.println( "Error --> " + ex.getMessage()); } } }
import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ListPhotosServlet extends HttpServlet { protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { Class.forName("oracle.jdbc.driver.OracleDriver"); Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "hr", "hr"); PreparedStatement ps = con.prepareStatement("select * from photos"); ResultSet rs = ps.executeQuery(); out.println("<h1>Photos</h1>"); while ( rs.next()) { out.println("<h4>" + rs.getString("title") + "</h4>"); out.println("<img width='600' height='600' src=displayphoto?id=" + rs.getString("id") + "></img> <p/>"); } con.close(); } catch(Exception ex) { } finally { out.close(); } } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } }
import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.sql.Blob; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class DisplayPhotoServlet extends HttpServlet { protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { Class.forName("oracle.jdbc.driver.OracleDriver"); Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "hr", "hr"); PreparedStatement ps = con.prepareStatement("select photo from photos where id = ?"); String id = request.getParameter("id"); ps.setString(1,id); ResultSet rs = ps.executeQuery(); rs.next(); Blob b = rs.getBlob("photo"); response.setContentType("image/jpeg"); response.setContentLength( (int) b.length()); InputStream is = b.getBinaryStream(); OutputStream os = response.getOutputStream(); byte buf[] = new byte[(int) b.length()]; is.read(buf); os.write(buf); os.close(); } catch(Exception ex) { System.out.println(ex.getMessage()); } } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } }
<servlet> <servlet-name>AddPhoto</servlet-name> <servlet-class>AddPhotoServlet</servlet-class> </servlet> <servlet> <servlet-name>ListPhotos</servlet-name> <servlet-class>ListPhotosServlet</servlet-class> </servlet> <servlet> <servlet-name>DisplayPhotoServlet</servlet-name> <servlet-class>DisplayPhotoServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>AddPhoto</servlet-name> <url-pattern>/addphoto</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>ListPhotos</servlet-name> <url-pattern>/listphotos</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>DisplayPhotoServlet</servlet-name> <url-pattern>/displayphoto</url-pattern> </servlet-mapping>