Return-Path: Delivered-To: apmail-jakarta-commons-user-archive@www.apache.org Received: (qmail 93921 invoked from network); 28 Feb 2004 23:08:23 -0000 Received: from daedalus.apache.org (HELO mail.apache.org) (208.185.179.12) by minotaur-2.apache.org with SMTP; 28 Feb 2004 23:08:23 -0000 Received: (qmail 24305 invoked by uid 500); 28 Feb 2004 23:08:07 -0000 Delivered-To: apmail-jakarta-commons-user-archive@jakarta.apache.org Received: (qmail 24032 invoked by uid 500); 28 Feb 2004 23:08:03 -0000 Mailing-List: contact commons-user-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Jakarta Commons Users List" Reply-To: "Jakarta Commons Users List" Delivered-To: mailing list commons-user@jakarta.apache.org Received: (qmail 24017 invoked from network); 28 Feb 2004 23:08:03 -0000 Received: from unknown (HELO minotaur.apache.org) (209.237.227.194) by daedalus.apache.org with SMTP; 28 Feb 2004 23:08:03 -0000 Received: (qmail 93873 invoked by uid 1339); 28 Feb 2004 23:08:16 -0000 Received: from localhost (sendmail-bs@127.0.0.1) by localhost with SMTP; 28 Feb 2004 23:08:16 -0000 Date: Sat, 28 Feb 2004 15:08:16 -0800 (PST) From: Martin Cooper To: Jakarta Commons Users List Subject: Re: Using FileUpload to send an e-mail with attachment with JavaMail In-Reply-To: <403DC3DC.2040907@asiayeah.com> Message-ID: <20040228142455.R13698@minotaur.apache.org> References: <403DC3DC.2040907@asiayeah.com> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Spam-Rating: localhost 1.6.2 0/1000/N X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: minotaur-2.apache.org 1.6.2 0/1000/N 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: commons-user-unsubscribe@jakarta.apache.org > For additional commands, e-mail: commons-user-help@jakarta.apache.org > > --------------------------------------------------------------------- To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org For additional commands, e-mail: commons-user-help@jakarta.apache.org