This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Please help me complete steps to upload an image

 
Timothy Sam
Ranch Hand
Posts: 751
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm trying to enumarate the steps I need to make in order to upload an image and save it to a database. I'm using MS SQLServer 2000. The steps I could think of so far is...

1. Create a servlet
2. Create multipart form
3. Save the image to database

It would seem that I'm missing steps after step number 2... What happens after the user presses the button(upload)? What are the steps involved? I need details like what I read on the internet(parse multipart request, create BinaryImageStream). I'm a little lost and don't really know where to begin, thank you...
[ May 30, 2006: Message edited by: Bear Bibeault ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64700
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you read the JSP FAQ on this topic?
 
Timothy Sam
Ranch Hand
Posts: 751
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I have. Previously I was thinking of using Jakarta Commons Net. But then I didn't know where to begin... As I was searching right now I found this...

http://www.roseindia.net/struts/strutsfileupload.shtml

which I think could be useful for me since I'm using struts... But still, I want to accomplish this with plain old Servlets and JSP. Commons Net would be helpful for me... But then again, I dunno where or should I say... How to begin...
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64700
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you download the commons upload package and read the documentation?
 
Timothy Sam
Ranch Hand
Posts: 751
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok... That's what I'm gonna do now... But I'm curious if anyone here have written his own API for file upload. I would really appreciate if you let me take a look at your code. Thank you.
 
Timothy Sam
Ranch Hand
Posts: 751
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can file upload and other requests be done simultaneously?
 
Shrikant Kulkarni
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear,
You can use apache's common.FileUpload and common.io packages which serves your purpose.
While you are inserting image into the BLOB column of the DB, first you insert empty BLOB object. And again you get the BLOB column using a SELECT sql statement open a OutputStream.
I think this code may help you
prapredStatement = connection.prepareStatement("select image from image_upload where log_id = ? and image_name = ? for update");
prapredStatement.setInt(1,logId);
prapredStatement.setString(2,imageName);
// Execute the SQL statement
resultSet = prapredStatement.executeQuery();
// Retrieve Blob streams for Image column, and load the sample file
if(resultSet.next())
{
// Get the Blob locator and open output stream for the Blob
Blob mapBlob = resultSet.getBlob(1);
OutputStream blobOutputStream = ((oracle.sql.BLOB)mapBlob).getBinaryOutputStream();
// Open the FileInputStream for insertion into the Blob column
InputStream sampleFileStream = new FileInputStream(saveTo);
// Buffer to hold chunks of data to being written to the Blob.
byte[] buffer = new byte[10* 1024];
// Read a chunk of data from the sample file input stream, and write the chunk to the Blob column output stream.
//Repeat till file has been fully read.
int nread = 0; // Number of bytes read
while( (nread= sampleFileStream.read(buffer)) != -1 ) // Read from file
blobOutputStream.write(buffer, 0, nread); // Write to Blob
// Close both streams
sampleFileStream.close();
blobOutputStream.close();
}
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Timothy Sam:
Ok... That's what I'm gonna do now... But I'm curious if anyone here have written his own API for file upload. I would really appreciate if you let me take a look at your code. Thank you.


There is sample code in the documentation for commons fileupload.
It's very easy.
Give it a read. If you get stuck on a particular part, write back and someone will be happy to help you with it.
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OReilly has example code for parsing a multipart request that is fairly simple and easy to follow. It allows the parsing of other parameters on the request along side the file.

http://servlets.com/cos/
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic