Link to home
Start Free TrialLog in
Avatar of subysri
subysri

asked on

Uploading a file

I need to store a file in database. How can I do this using java?
ASKER CERTIFIED SOLUTION
Avatar of Mayank S
Mayank S
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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 ) ;
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Please proceed with that recommendation.
>>Accept: mayankeagle {http:#10647429}

Not the correct way to do it IMO
Well, the main problem was to know how to insert the data - not about performance, I guess.
It's not a performance issue - it's about a potentially fatal memory problem. The approach i suggested doesn't suffer from this drawback
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.
>>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
Yes, and yes ;-)
Please proceed with that recommendation.