This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Servlets and the fly likes Please help me complete steps to upload an image Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Please help me complete steps to upload an image" Watch "Please help me complete steps to upload an image" New topic
Author

Please help me complete steps to upload an image

Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 746
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 ]

SCJP 1.5
http://devpinoy.org/blogs/lamia/ - http://everypesocounts.com/
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

Have you read the JSP FAQ on this topic?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 746
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

Joined: Jan 10, 2002
Posts: 61092
    
  66

Did you download the commons upload package and read the documentation?
Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 746
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

Joined: Sep 18, 2005
Posts: 746
Can file upload and other requests be done simultaneously?
Shrikant Kulkarni
Ranch Hand

Joined: May 10, 2005
Posts: 42
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

Joined: Dec 11, 2004
Posts: 13410

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.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
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/


I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
 
 
subject: Please help me complete steps to upload an image