Image upload and retrieve from BLOB in jsp

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Samita
    New Member
    • Jul 2007
    • 7

    Image upload and retrieve from BLOB in jsp

    I m completely new to java.Can anybody provide me detailed JSP code to upload image from file control into a BLOB field into DB2 database using servlet and also code for retrieving the image and showing in webpage.please give it with an axample ...Urgently
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    Originally posted by Samita
    I m completely new to java.Can anybody provide me detailed JSP code to upload image from file control into a BLOB field into DB2 database using servlet and also code for retrieving the image and showing in webpage.please give it with an axample ...Urgently
    I will not give complete spoon-feed code here, you should do it by yourself. (read the guidelines of this forum). This forum is only for helping with a java problem and not for providing free code.

    So I will tell you how to develop your own solution:

    just submit an (image-)file with contentType=mul tipart/form-data
    and load it into a byte array. Then make a prepared statement to your database and submit the data.

    code snippet:
    submitting image from webpage (upload):
    Code:
    <form name=uploadForm action="<%= url %>" method=post enctype="multipart/form-data">
    <input type=file>
    </form>
    code snippet:
    retrieving file-data inside a servlet:
    Code:
    public class UploadServlet extends HttpServlet
    {
    	public void service(HttpServletRequest request, HttpServletResponse response)
    	throws ServletException, IOException
    	{
    
    		String contentType = request.getContentType();
    		if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0))
    		{
    			DataInputStream in = new DataInputStream(request.getInputStream());
    			int formDataLength = request.getContentLength();
    
    			byte dataBytes[] = new byte[formDataLength];
    			int byteRead = 0;
    			int totalBytesRead = 0;
    
    			while (totalBytesRead < formDataLength) 
    			{
    				byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
    ...
    now, to save this dataBytes array into database, you should read about "JDBC" and how to use "prepared statements" (these are the statements with a question mark inside). It's too much to describe here, also a code snippet is too long. If you already know how to save data into database, then just use the column type "BLOB" and do the same.
    But it is better not to save it as BLOB inside the database, but as file and only save the reference to this file inside the database.

    If you don't care about assigning user-rights for viewing the images, you can directly save the file on the webserver. Then it can be viewed by just providing its's URL (instead of retrieving it from the database)

    Comment

    • johnomniflex
      New Member
      • Dec 2008
      • 1

      #3
      Using JSP to create a file link to a BLOB?

      I do care about restricting user access to the blobs in my database (else I would have stored them in the file system, as you suggested).

      How does one create the JSP code to create an HTML <a> tag? My code already displays the blobs on the screen, but I want my users to be able to select an icon and be offered a download option.

      If you are able to help then Thanks !

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by johnomniflex
        If you are able to help then Thanks !
        You are either hijacking someone else's thread which is considered rude or you are the original poster in which case you should've told us so; just to reduce confusion.

        kind regards,

        Jos (moderator)

        Comment

        Working...