Win a copy of Learn Java with Math: Using Fun Projects and Games this week in the Beginning Java forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

Uploading file in a database

 
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 ?
 
author & internet detective
Posts: 39655
785
Eclipse IDE VI Editor Java
  • 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?
 
Marshal
Posts: 67313
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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..
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any updates for this .
 
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`)
);
 
Doody calls. I would really rather that it didn't. Comfort me wise and sterile tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!