We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Uploading a file

subysri
subysri asked
on
Medium Priority
278 Views
Last Modified: 2010-03-31
I need to store a file in database. How can I do this using java?
Comment
Watch Question

Principal Technologist
CERTIFIED EXPERT
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
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 ) ;
CERTIFIED EXPERT
Top Expert 2016
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
Please proceed with that recommendation.
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>Accept: mayankeagle {http:#10647429}

Not the correct way to do it IMO
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
Well, the main problem was to know how to insert the data - not about performance, I guess.
CERTIFIED EXPERT
Top Expert 2016

Commented:
It's not a performance issue - it's about a potentially fatal memory problem. The approach i suggested doesn't suffer from this drawback
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
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.
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>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
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
Yes, and yes ;-)
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
Please proceed with that recommendation.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.