aspose file tools*
The moose likes Servlets and the fly likes Uploading file in a database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Uploading file in a database" Watch "Uploading file in a database" New topic
Author

Uploading file in a database

sovan chatt
Ranch Hand

Joined: Aug 09, 2010
Posts: 43
I want to uplaod an image file in a database through blob object...I have a jsp page where I insert the file and when I click the submit button a servlet is called.But how would I retrieve the name of that file in the servlet ?
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31057
    
232

Sovan,
Why do you need the file name? Aren't you storing the contents? Search for "file upload" to get an open source library that reads the contents of an uploaded file.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
sovan chatt
Ranch Hand

Joined: Aug 09, 2010
Posts: 43
@Jeanne Boyarsky I had this code...Here file name is given in the servlet itself.But what I want to do is to retrieve the name of the file from the jsp page and use the following code..Why can't I do that?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67

You can, but you are doing it very very wrong. Please see the JSP FAQ for information on properly handing file uploads. You do not read the uploaded file information from the file system -- you can't, in fact. The server has no access to the client's file system.

You need to read the information from the multi-part request, preferably using one of the popular 3rd party libraries for doing so. Trying to do it yourself is like hitting yourself with a hammer; best not done.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
sovan chatt
Ranch Hand

Joined: Aug 09, 2010
Posts: 43
@Bear Bibeault I just wanted to know how toretrieve the file name in the servlet ...please tell me how to do that..
manidhar karnatakam
Greenhorn

Joined: Sep 27, 2007
Posts: 3
Any updates for this .
Charles 'King
Ranch Hand

Joined: Jul 05, 2009
Posts: 56

sovan chatt wrote: how toretrieve the file name in the servlet ...please tell me how to do that..


There are many ways to do this. One option is to use: org.apache.commons.fileupload.FileItem

String fileName = fileItem.getName();

I recommend you review this: http://commons.apache.org/fileupload/apidocs/org/apache/commons/fileupload/FileItem.html
sovan chatt
Ranch Hand

Joined: Aug 09, 2010
Posts: 43

When I try to run the above code I get an error message... java.sql.SQLException: Io exception: Connection reset by peer: socket write error .
Why??
sovan chatt
Ranch Hand

Joined: Aug 09, 2010
Posts: 43
I had used the following query to create the table...
CREATE TABLE test (
`id` varchar2(10),
`image` blob,
PRIMARY KEY (`id`)
);
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Uploading file in a database