It is currently Mon May 21, 2012 11:42 pm


Post a new topicPost a reply Page 1 of 1   [ 2 posts ]
Author Message
 Post subject: Uploading File to an oracle database
PostPosted: Sun Oct 26, 2008 4:51 pm 

Joined: Fri Oct 24, 2008 2:14 pm
Posts: 14
Hi the following is how you can enable your website to upload files into oracle DB

First you will need to add to the upload page an input file tag in the form like this

Code:
<inpt type="file" name="myfile" />


then you will change the tag of the form and add the following
Code:
enctype="multipart/form-data" method="post"

this will tell the browser to format the request in a format that is able to hold the file
in the action attribute of the form put the path of the servlet that will handle the file upload
In the doPost of the servlet you will need a library such as apache file upload
http://commons.apache.org/fileupload/using.html

You will call this library and give it the request and it will give u an input stream for the file
Read the file into an object

Convert the object into byte[]

Create a CLOB field in the table in oracle

Use the something like the following code to insert into the CLOB field
Code:
// Inserting CLOB value with PreparedStatement.setBytes()
      PreparedStatement ps = con.prepareStatement(
        "INSERT INTO FileTable(ID, Subject, Body) VALUES (2,?,?)");
      ps.setString(1, subject);
      byte[] bodyIn = yourObject.toByteArray;
      ps.setBytes(2, bodyIn);
      int count = ps.executeUpdate();
      ps.close();


Hope I could Help

Khalil


Top
 Profile Send private message  
 
 Post subject: Re: Uploading File to an oracle database
PostPosted: Tue Oct 28, 2008 1:01 pm 

Joined: Mon Oct 27, 2008 4:38 pm
Posts: 1
This is a very nice solution sent by Julio
This solution is very good for huge files and does not put a lot of load on the JVM heap
Hi!
this is a scriptlet of upload/download of BLOBs on Oracle DB. This solution have some years (was original developed for a Ora8i and tested later on 9 and 10), and was tunned for Very Large Blobs (>500Mb) using jdbc only. You see in this solution that Blob content never is stored on memory, which gives a very good scalability.

I erase all error management on the examples, please you MUST have a LOT of error management!!

UPLOAD
Code:
public int write(String id, InputStream is) {

  Connection con = getConnection();
  //
  // create a new record with empty blob

  String sql = "INSERT INTO "+getTableName()+" (ID,"+getBlobFieldName()+") VALUES (?,empty_blob())";
  PreparedStatement pstmt = con.prepareStatement(sql);
  pstmt.setString(1, id);
  int rowsAffected = pstmt.executeUpdate();
  pstmt.close();
  //
  // Get empty blob for update
  sql = "SELECT "+getFieldName()+" FROM "+getTableName()+" WHERE ID="+id+" FOR UPDATE";
  pstmt = con.prepareStatement(sql);
  ResultSet rs = pstmt.executeQuery();
  rs.next();

  oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob(1);
  OutputStream os = blob.getBinaryOutputStream();
  //
  // fill blob
  byte[] buf = new byte[1024];

  int len = -1;

  int len_total = 0;

  while ( (len=is.read(buf,0,1024)) != -1) {

    os.write(buf,0,len);

    len_total += len;

  }
  //
  // close resources
  os.close();
  rs.close();
  pstmt.close()
  con.close()
  //
  // return writed bytes
  return len_total;
}

- The InputStream of Document can be get from HTTPRequest.
- Be carefull with cast to oracle.sql.BLOB on Application Servers JEE 1.4 o later, this use was because Oracle have a Bug with the homonym function on JDBC Blob class.

DOWNLOAD

Code:
public int read(OutputStream os) {
  String sql = "SELECT "+getBlobFieldName()+" FROM "+getTableName()+" WHERE "+getConstraint();

  Connection con = getConnection();

  PreparedStatement pstmt = con.prepareStatement(sql);
  ResultSet rs = pstmt.executeQuery();

  rs.next();

  java.sql.Blob blob = rs.getBlob(1);
  InputStream is = blob.getBinaryStream();


  byte[] buf = new byte[1024];

  int len = -1;

  int len_total = 0;

  while ( (len=is.read(buf,0,1024)) != -1) {

    os.write(buf,0,len);

    len_total += len;

  }

  is.close();
  rs.close();
  pstmt.close()
  con.close()


  return len_total;

}

//
// This is a Servlet doGet method
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  response.setContentType(getMimeType());

  response.setHeader("Content-Disposition","in-line;filename=\""+getFilename()+"\"");

  java.io.OutputStream out = response.getOutputStream();

  read(out);

  out.flush();

  out.close();

}


Top
 Profile Send private message  
 
Display posts from previous:  Sort by  
Post a new topicPost a reply Page 1 of 1   [ 2 posts ]


Who is online

Registered users: Aleffletlierm, Breedagrigued, HanoItennawer, LoldtodY, SuRaica


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
twilightBB Style by Daniel St. Jules of Gamexe.net

[
SEO MOD © 2007 StarTrekGuide ]