469,356 Members | 2,016 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,356 developers. It's quick & easy.

Image upload and retrieve from BLOB in jsp

7
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
Feb 19 '08 #1
3 20922
chaarmann
785 Expert 512MB
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=multipart/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):
Expand|Select|Wrap|Line Numbers
  1. <form name=uploadForm action="<%= url %>" method=post enctype="multipart/form-data">
  2. <input type=file>
  3. </form>
code snippet:
retrieving file-data inside a servlet:
Expand|Select|Wrap|Line Numbers
  1. public class UploadServlet extends HttpServlet
  2. {
  3.     public void service(HttpServletRequest request, HttpServletResponse response)
  4.     throws ServletException, IOException
  5.     {
  6.  
  7.         String contentType = request.getContentType();
  8.         if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0))
  9.         {
  10.             DataInputStream in = new DataInputStream(request.getInputStream());
  11.             int formDataLength = request.getContentLength();
  12.  
  13.             byte dataBytes[] = new byte[formDataLength];
  14.             int byteRead = 0;
  15.             int totalBytesRead = 0;
  16.  
  17.             while (totalBytesRead < formDataLength) 
  18.             {
  19.                 byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
  20. ...
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)
Feb 19 '08 #2
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 !
Dec 4 '08 #3
JosAH
11,448 Expert 8TB
@johnomniflex
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)
Dec 5 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by DH | last post: by
reply views Thread by Paul Hamlington | last post: by
2 posts views Thread by Faree | last post: by
3 posts views Thread by Michael | last post: by
pritipshah
1 post views Thread by pritipshah | last post: by
3 posts views Thread by Bill H | last post: by
matheussousuke
26 posts views Thread by matheussousuke | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.