• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 236
  • Last Modified:

Uploading a file

I need to store a file in database. How can I do this using java?
0
subysri
Asked:
subysri
  • 7
  • 4
2 Solutions
 
Mayank SAssociate Director - Product EngineeringCommented:
You can get the contents into a byte array and store it. Have a look at:

http://javaalmanac.com/egs/java.sql/InsertBinary.html

- for storing/ reading binary data to/ from databases.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
You can make a FileInputStream and read from the file into a byte[] array.

File f = new File ( "filename" ) ;
FileInputStream fis = new FileInputStream ( f ) ; // or new FileInputStream ( "filename" ) ;
byte[] array = new byte[f.length ()] ;
fis.read ( array ) ;
0
 
CEHJCommented:
You would be better to treat the insert as a stream, as in addition to not being an efficient way of making an insert, you may get out of memory errors if the file is too big if you try to load it all into memory as above. See

http://java.sun.com/j2se/1.4.2/docs/api/java/sql/PreparedStatement.html#setBinaryStream(int, java.io.InputStream, int)
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Mayank SAssociate Director - Product EngineeringCommented:
Please proceed with that recommendation.
0
 
CEHJCommented:
>>Accept: mayankeagle {http:#10647429}

Not the correct way to do it IMO
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Well, the main problem was to know how to insert the data - not about performance, I guess.
0
 
CEHJCommented:
It's not a performance issue - it's about a potentially fatal memory problem. The approach i suggested doesn't suffer from this drawback
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Well - memory problems may or may not come. That's about optimizing your approach - the difference between "how to do it" and "how to do it best". I agree that your method is more efficient, but I assumed that this one only asks "how to do it" because it said nothing else ;-) So I didn't go into the details. The questioner should have responded if he faced memory problems, too.... anyways, I would be glad with a split.
0
 
CEHJCommented:
>>The questioner should have responded if he faced memory problems,

That error may well occur only rarely, and may well be quite difficult to debug
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Yes, and yes ;-)
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Please proceed with that recommendation.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 7
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now