On Thu, 26 Feb 2004, Tony Yat-Tung Cheung wrote:
> Hi,
>
> I am trying to make a servlet to receive a file with FileUpload, and
> then send out an e-mail with the received file as an attachment.
>
> The FileItem does not provide a way to get a DataSource out of it, which
> is required by the JavaMail's MimeBodyPart.
>
> Is there any way?
>
> The FileItem's API documentations seem to hint the solution, but I do
> not quite get the idea.
> "While this interface does not extend javax.activation.DataSource per se
> (to avoid a seldom used dependency), several of the defined methods are
> specifically defined with the same signatures as methods in that
> interface. This allows an implementation of this interface to also
> implement javax.activation.DataSource with minimal additional work."
>
> Do we have an easy solution here? I think it is one of the common
> applications of the commons-fileupload.
I disagree that it's common, especially given that you're the first to
ask, but it is simple to accomplish. ;-)
First, you'll want a class that implements DataSource. Just add it to
DefaultFileItem:
public class MyDefaultFileItem extends DefaultFileItem
implements DataSource {
// Add any DataSource methods that DefaultFileItem doesn't
// already implement
}
Now you need to get FileUpload to use this new class. That means you need
a factory:
public class MyDefaultFileItemFactory extends DefaultFileItemFactory {
public FileItem createItem(...) {
// Basically, just copy the one from the base class, but make
// it instantiate your new class, above, instead of the default.
}
}
You're almost done. Now all you need to do is construct your FileUpload
class and specify your own factory:
DiskFileUpload upload = new DiskFileUpload(
new MyDefaultFileItemFactory());
That's it. Now the file items you get back from FileUpload will be of your
own class, and so implement DataSource.
--
Martin Cooper
>
> Thank you very much.
>
> Best Regards,
> Tony Cheung
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]