• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Uploading files

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello!
I am using JSP technology.I want to upload a file on the client side from the file system of the client and want the content of the whole file to be stored into the database on the server side.I am using postgres as the database. So can anyone please give me the logic to how to accomplish this.If anybody has the code then that would be of really great help since its too urgent.
Johnson
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
does that database support blob. what i mean is are you going to store that file in the db as a blob ? if yes then the code goes this way:

MultipartParser mp = null;
Connection con=//Write the code to get java.sql.Connection object
Statement st=null;

try {
mp = new MultipartParser(request, 10*1024*1024); // 10MB
}
catch (IOException ex) {
throw new UploadException("Error initializing multi-part parser",ex);
}
st = con.createStatement();
st.executeUpdate("insert into " + tableName + " (" +
primaryKeyFieldName
+ "," + blobFieldName + ") values (" + primaryFieldValue
+ ",EMPTY_BLOB() ) ");
/*the table where you blob data will recide. Insert an empty blob. The function EMPTY_BLOB() is provided by the Oracle Database itself. Check out if u have this feature in ur db
*/
rs = st.executeQuery("select " + blobFieldName + " from "
+ tableName + " where "
+ primaryKeyFieldName + " = "
+ primaryFieldValue + " FOR UPDATE ");
if (!rs.next()) {
throw new BlobInsertException("Unable to insert record with PrimaryKey " + primaryFieldValue);
}
Object o = rs.getBlob(1);
OutputStream os = ((weblogic.jdbc.common.OracleBlob) o).getBinaryOutputStream();
byte buffer[] = new byte[8192];

int size = 0;
int nread = 0;
//here input stream is from the the file
while ((nread = inputStream.read(buffer)) != -1) {
os.write(buffer, 0, nread);
size += nread;
}
os.flush();
os.close();
inputStream.close();

}
catch (SQLException ex) {
throw new BlobInsertException("Some database problem",ex);
}
catch (IOException ex) {
throw new BlobInsertException("Some problem in data streams",ex);
}
finally {
try {
if(rs != null) rs.close();
if(st != null) st.close();
}
catch (Exception ex) { }
}

let me know if u face any problems
 
No one can make you feel inferior without your consent - Eleanor Roosevelt. tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic