473,396 Members | 1,877 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 21403
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

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

Similar topics

4
by: DH | last post by:
I have a "file upload form" that works OK, but I have been unsuccessful in my attempt to also resize the uploaded .JPG (if it is too wide), over-writing the original .JPG, and then create and save...
0
by: Paul Hamlington | last post by:
Hello, I've been programming in ASP for a little while now and quite an advanced user, but I have come across an unusual problem in which I need assistance. I have built my own image upload,...
2
by: Faree | last post by:
Hi, I am a PHP Developer.can any one help me on image upload and display of that image.what i want is uploading an image to a directory ,displaying of that image as a thumbnail.when i...
3
by: Michael | last post by:
Hello, I want to write a componant that helps a user upload an image or file. I want something like you see on yahoo's image upload page. It has to work on mac and windows. Can anyone help me get...
1
pritipshah
by: pritipshah | last post by:
Hi All, I am using vtiger open source CRM and in that they are using FCKeditor for Image Upload (Resource browser window) but it is not upload or make new folder for image. Vtiger CRM...
7
by: mishrarajesh44 | last post by:
hii all Truly telling i hav got this code from net & i am finding error while running the code below.. code:- <?php $idir = "photo/"; // Path To Images Directory $tdir =...
7
by: dragiton | last post by:
SA Upload SQL Database variable types (image upload and storage) I am having trouble with the SA Upload utility. The following code used to work correctly. However, I lost my database and had to...
3
by: Bill H | last post by:
I have written many programs for file / image upload over the years in perl, but know that I am working more in php I find I need to be able to do it in php now. I was gonna just do a google serach...
1
by: chennaibala | last post by:
can any one send me mutiple image upload program and save the file name with extension in mysql table.we must cheak uploaded file type like bmp or any image file while uploading. i develop...
26
matheussousuke
by: matheussousuke | last post by:
May someone help me correct this script? there a few ' and " and ; in the wrong places: <?php include "./comm.inc"; connectdb(); $sql = "SELECT imgid,imgtype FROM...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.