Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Uploading file in a database

 
sovan chatt
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34071
331
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
sovan chatt
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Pie
Posts: 64606
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
sovan chatt
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any updates for this .
 
Charles 'King
Ranch Hand
Posts: 56
Eclipse IDE Postgres Database Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had used the following query to create the table...
CREATE TABLE test (
`id` varchar2(10),
`image` blob,
PRIMARY KEY (`id`)
);
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic