We help IT Professionals succeed at work.

File Uploading - Help - Urgent...

bakyaraj
bakyaraj asked
on
Hi Guys,
I have got a requirement to upload a file from the client machine through  JSP and save it in the oracle 8i database as Blob.
Another thing, I need to retrieve the same Blob object from the database and show it in the screen as image.

How to do it?
I am using Iplanet web server.

Can anyone give me a solution(if possible with sample coding in working condition!)?

It's very urgent.


Comment
Watch Question

We have used smartupload component to do that, Unfortunately it wasn't in my module, so i can't help much but you can refer to the site below and download this component, its FREE!!!

http://www.aspsmart.com/

on this site u wil find the component under the link of "aspSmartUpload".

all the best.

thanx
vivek

Author

Commented:
vivek i am using JSP not ASP.

yeah but still u can use it yaar.....we also made the whole project on JSP only file uploading component was from ASP. If that wont work then sorry for giving u a wrong advice, and if u find such type of JSP compo. then please tell me also about it.

thanx in advance
vivek
There is a reference book titled "Java Servlet Programming" published by Oreilly. You can go to www.oreilly.com and download the source code of that book. You can make used of the MultipartRequest class for the file uploading stuff.

good luck.

Author

Commented:
hi vincent,
i am using the package provided by oreilly(MultipartRequest) . But what i need is , the file should be stored in database as blob. Then the same blob object should be retrieved and shown in the screen along with the other fields.

do u have any suggestions?
oic. i have done it with MultipartRequest with some modification in my last year project.

1. blob to oracle part:
=> I think you can handle it, it's easy.

2. For the display part, there are many methods.
a) Use one SQL for all fields include the blob. Then use a filestream to create a physical file on server. And display that link to client with other fields.

b) Use one SQL for the blob and another SQL for other fields. For other fields, you can handle it as usual. For the blob field, you can write a individual page for it and include some caller in the main page such as  
<img src=<%=photoJSP.jsp?BLOBID="ABC123"%>
<a href=<%=exeJSP.jsp?BLOBID="ABC456"%>
to get the blob.

Set the Response.contenttype to the file type of your file (eg jpg, gif, exe, zip). And write all the blob data to the jsp directly. This method would not create a physical file in server but consume one connection for one blob.

Author

Commented:
Hi Guys,
I am able to store the file in the database and as well as able to retrieve but there is one limitation. If the file exceeds 3900 bytes(approx.) then the setobject method of preparedstatement stores null value. I tried setBinaryStream, it allows only 2000 bytes.

I am using oracle thin driver. I think the problem is with the driver.

Can anyone suggest proper driver?
Commented:
Hi,
  The best solution i can give is to use jspsmartupload.
1 advice i can give to u is that instead of storing the image into the database u can store the image in the server and u can store the image name in the database and while showing to the client u can get the image from the server and not from the database.

This will be helpful in 2 ways.
1.Storing a large object to the database(time factor)
2. retrieving the image from database.

This will take a lot of time and i think my proposed answer will hold good for u.

Use jsp smart upload..I am sending the sample code for u...
Uploading and saving the image...
<FORM  NAME="UpForm" method=POST ACTION="saveLogo.jsp" ENCTYPE="multipart/form-data" onSubmit="return check()">
<table>
<tr><h3>Logo Upload</h3></tr>
<tr><td><input type=file name=logo></td></tr>
<tr><td><input type=submit value='Submit'></td></tr>
<input type=hidden name=bizid>
</table>
</form>
//note the form type please

//U have to download jsp smartupload..(from google search and download )
<jsp:useBean id="myUpload" scope="page" class="com.jspsmart.upload.SmartUpload" />
     myUpload.initialize(pageContext);
     myUpload.upload();
     com.jspsmart.upload.File jspfile= null;
     String fname="";
     String fext="";
     String custcode=(String)session.getValue("custcode");
     session.putValue("custcode",custcode);
     com.jspsmart.upload.Request jsprequest = myUpload.getRequest();
     String bizid=jsprequest.getParameter("bizid");
     bizid=bizid==null ? "" : bizid;


     out.println("bizid is  "+bizid);
     int bizcode=0;
     String nfname="";


     for (int i=0;i<myUpload.getFiles().getCount();i++){          
       if (!myUpload.getFiles().getFile(i).isMissing()){
               fname=myUpload.getFiles().getFile(i).getFileName();
               fext=myUpload.getFiles().getFile(i).getFileExt();
     }
     }
          java.sql.Connection con = sqlcon.getConnection();
          nuser.setConnection(con);
          if(bizid.equals("")){
               bizcode=nuser.getStdBiz_id();
               nfname="s"+bizcode+"."+fext;
          }
          else
               nfname=bizid+"."+fext;
          Vector v= new Vector();
          v.addElement(nfname);
          myUpload.save("/logo",v,myUpload.SAVE_VIRTUAL);

GoodLuck
Vasan S
You can ma
First make a jsp like below to submit the file choosen with post method:

  <form enctype="multipart/form-data" method="post" action="<your upload file servlet>">
  <INPUT TYPE=FILE NAME="file"><BR>
  <input type="submit">
  </form>

With using the oreilly servlet package.
Get connection from your database.
First insert a record with EMPTY_BLOB() for the file data.
Then get it back. You can upload the image like below.

  PrepareStatement pstmt = conn.prepareStatement("select <FILE FIELD> from <TABLE> where key = ? ");
  pstmt.setInt( 1, <primary key> );
  Resultset rs = pstmt.executeQuery();
  if (rs.next()) {
    oracle.sql.BLOB blob = ((OracleResultSet)rs).getBLOB(1);
    OutputStream os = ((oracle.sql.BLOB) blob).getBinaryOutputStream();
    int nread = (int)filePart.writeTo(os); // Write to Blob
    os.close();
  }


For display the image from BLOB. Use a servlet to get the BLOB:
Get the database connection.
and set the response.
  OutputStream os = response.getOutputStream();
  response.setContentType("application/xxx");        

Then get the blob from the database and write it to the OutputStream:
  PrepareStatement pstmt = conn.prepareStatement("select <FILE FIELD> from <TABLE> where key = ? ");
  pstmt.setInt( 1, <primary key> );
  Resultset rs = pstmt.executeQuery();
  if (rs.next()) {
    Blob blob = (Resultset)rs.getBlob("FILE_DATA");
    length =  new Long(blob.length()).intValue();
    InputStream in = blob.getBinaryStream();
    int length = 0;
    byte[] bytes = new byte[1024]; //1K at a time            
    while( length != -1 ) {
      length = in.read(bytes);
      if( length > 0 ) {
        os.write(bytes, 0, length);
      }  
    }
  }

At the jsp, just called like below:
<img src="<your get file servlet>?ID=<your primary key at database>" ><BR>

Hope this suit you!


Author

Commented:
no comments

Commented:
how bout jspSmartUpload (also free)
http://www.jspsmart.com/
bakyaraj:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.

Commented:
Recommendation -> Split points b/w vasan_sr and woodstock
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:


[split points between vasan_sr and woodstock]


Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
sudhakar_koundinya
EE Cleanup Volunteer
---------------------
If you feel that your question was not properly addressed, or that none of the comments received were appropriate answers, please post your concern in THIS thread.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.