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
Image upload and retrieve from BLOB in jsp
Collapse
X
-
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.Originally posted by SamitaI 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
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 snippet:Code:<form name=uploadForm action="<%= url %>" method=post enctype="multipart/form-data"> <input type=file> </form>
retrieving file-data inside a servlet:
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.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); ...
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) -
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
-
Comment
Comment