Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

display image from database in java servlet

package org.o7planning.servletexamples;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.o7planning.servletexamples.model.Person;
import org.o7planning.tutorial.jdbc.ConnectionUtils;

// /image?id=123
@WebServlet(urlPatterns = { "/image" })
public class DisplayImageServlet extends HttpServlet {
  private static final long serialVersionUID = 1L;

  public DisplayImageServlet() {
      super();
  }

  private Person getImageInTable(Connection conn, Long id) throws SQLException {
      String sql = "Select p.Id,p.Name,p.Image_Data,p.Image_File_Name "//
              + " from Person p where p.id = ?";
      PreparedStatement pstm = conn.prepareStatement(sql);
      pstm.setLong(1, id);
      ResultSet rs = pstm.executeQuery();
      if (rs.next()) {
          String name = rs.getString("Name");
          byte[] imageData = rs.getBytes("Image_Data");
          String imageFileName = rs.getString("Image_File_Name");
          return new Person(id, name, imageFileName, imageData);
      }
      return null;
  }

  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
      Connection conn = null;
      try {
          // Get Database Connection.
         // (See more in JDBC Tutorial)
          conn = ConnectionUtils.getMyConnection();
          Long id = null;
          try {
              id = Long.parseLong(request.getParameter("id"));
          } catch (Exception e) {

          }
          Person person = getImageInTable(conn, id);

          if (person == null) {
              // No record found, redirect to default image.
              response.sendRedirect(request.getContextPath() + "/images/noimage.jpg");
              return;
          }
       
          // trump.jpg, putin.png
          String imageFileName = person.getImageFileName();
          System.out.println("File Name: "+ imageFileName);
       
          // image/jpg
          // image/png
          String contentType = this.getServletContext().getMimeType(imageFileName);
          System.out.println("Content Type: "+ contentType);
       
          response.setHeader("Content-Type", contentType);
       
          response.setHeader("Content-Length", String.valueOf(person.getImageData().length));
       
          response.setHeader("Content-Disposition", "inline; filename="" + person.getImageFileName() + """);

          // Write image data to Response.
          response.getOutputStream().write(person.getImageData());

      } catch (Exception e) {
          throw new ServletException(e);
      }
  }

  @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
      doGet(request, response);
  }

}
Comment

PREVIOUS NEXT
Code Example
Java :: list java initialize 
Java :: convert class to xml string 
Java :: convert date to calendar java 
Java :: how to get length of integer in java 
Java :: gravity in java 
Java :: merge sort java 
Java :: adding 2 variable in java 
Java :: compare date with current date in android 
Java :: How to efficiently find the index of smallest value that is larger than some target within a sorted array, in Java? 
Java :: file to image javafx 
Java :: How to compare lists of custom classes without defining equals() and hashCode()? 
Java :: jackson ignore value if null 
Java :: what is the difference between sc.nextLine() and sc.next() in java 
Java :: convert string to byte java 
Java :: java get unique elements from array 
Java :: reverse sentence in java 
Java :: mongoose collection name 
Java :: procedural programming Java code example 
Java :: printing in java 
Java :: open camera in android 
Java :: capitalize string java 
Java :: create a toast message in android 
Java :: shell sort java 
Java :: how to convert int to integer in java 
Java :: write json file java 
Java :: java 16 raspberry pi 
Java :: how to find the average in an arraylist 
Java :: java random guid 
Java :: why java platform independent 
Java :: how to iterate a queue in java 
ADD CONTENT
Topic
Content
Source link
Name
9+4 =