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):
- <form name=uploadForm action="<%= url %>" method=post enctype="multipart/form-data">
-
<input type=file>
-
</form>
code snippet:
retrieving file-data inside a servlet:
- 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)