Skip to Main Content

Java Development Tools

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

convert UploadedFile to DataHandler

user5636757Jun 14 2013 — edited Jul 3 2013

hi,

i have to provide an option for user to upload a file on website.

i am using af:inputFile and able to get file in UploadedFile object.

now i've to transfer this file to a server and that server's API is expecting javax.activation.DataHandler object.

does anyone know how to convert UploadedFile to DataHandler?

DataHandler has constructor which accepts URL - but i don't know how to convert UploadedFile to URL (i know java.io.File can be converted to java.net.URL).

It also accepts DataSource interface but I don't know how to create FileDataSource from UploadedFile?

It has another constructor which accepts data (Object) and MIME type - again how to convert UploadedFile to data Object?

I don't want to write file to a temporary location and read it in File object.

any help?

thanks.

This post has been answered by dvohra21 on Jun 26 2013
Jump to Answer

Comments

Timo Hahn

I think you have to use a temporary file anyway as the stream you get from the UploadedFile is only valid during the request and the data handler class needs access multiple times.

Next thing is that you can't create a DataHandler  from a stream which is all you get from the UploadedFile.

Timo

user5636757

yes that's true that i am not able to create data handler from stream which i am getting from uploaded file.

however i didn't get u on this point - data handler class needs access multiple times?

i can simply create data handler object once from uploaded file and do with it... but looks like there is no way

Timo Hahn

No, you have to use a temporary file as the DataHandler requires access to the data multiple times (from the docs). UploadedFile only holds the data during one request, so you can't use this.

A temporary file is the solution.

Timo

user5636757

hi,

i am now using temporary file.

and i am trying to delete it as soon as i read it in file and it's processed.

only issue is - file is not getting deleted - looks like upload process is not finished before 'delete code' is executed...

think i should use thread to control this execution - what's say?

thanks.

Frank Nimphius-Oracle

Hi,

shouldn't be the upload process but the transfer of the data handler. The file wont be transferable if upload hasn't been completed

Frank

user5636757

it worked. thanks man.

1 - 7
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Jul 31 2013
Added on Jun 14 2013
7 comments
1,234 views