You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by Rob Cameron <ca...@homes.com> on 2005/11/01 16:28:27 UTC

RE: POI & JAF?

Iain,

This code works for me -- unfortunately it doesn't get me any further along.
My attachment still shows up as ATTxxxxx.dat. I believe the difference would
be that somewhere in your setup (for me it is going in mailcap.default
inside activation.jar) a value is associated with "x-java-content-handler"
for the "application/msexcel" mime type. Could you please take a look at
your system and see if you see anything associated with
"application/msexcel"?

To possibly help clarify, here is my code (now):

...
//user picks whether they get a .txt or .xls -- everything with the .txt
works fine
String mimeType = null;
DataHandler dataHandler = null;
if (sFileExt.equals("xls")) {
	byte[] attachmentContentByteArray = attachmentContent.getBytes(); 
	DataSource dsData = new MemoryDataSource(attachmentContentByteArray,
"export.xls", "application/msexcel");
	dataHandler = new DataHandler(dsData);
} else {
	dataHandler = new DataHandler(attachmentContent,"text/plain");
}
messageBodyPart = new MimeBodyPart();

messageBodyPart.setDataHandler(dataHandler);

multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);

Transport.send(message);


Thanks,
Rob

-----Original Message-----
Rob,

Here is the code I use, which I think covers what you are talking about.

Iain


---------------------------------------
import javax.activation.DataHandler;
import javax.activation.DataSource;

DataSource dsData = new MemoryDataSource(
                poiByteArray, "foo.xls", "application/msexcel");
DataHandler handler = new DataHandler(dsData);



---------------------------------------
File: MemoryDataSource.java

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.lang.Validate;

/**
 * A read-only DataSource that wraps a binary attachment, which is
already in
 * memory and the MIME type is already known.
 * <p>
 * NB This refers to the Java Activation Framework's idea of
DataSources.
 * Nothing to do with SQL. Java Mail uses this for framework to handle
 * mail attachments.
 * <p>
 * The JAF 'activation.jar' must be present if this class is used.
 * 
 * @see <a
href="http://java.sun.com/products/javabeans/glasgow/jaf.html">
 *  Java Activation Framework</a>
 */
public class MemoryDataSource implements javax.activation.DataSource
{
    private byte[] data;
    private String name;
    private String mimeType;
    
    /**
     * Creates a MemoryDataSource.
     * @param data     Binary data. <b>NB - no defensive copy is
made.</b>
     * @param name     Some sort of filename for the data.
     * @param mimeType MIME type of the data.
     */
    public MemoryDataSource(byte[] data, String name, String mimeType)
    {
        Validate.notNull(data, "data is null");
        Validate.notEmpty(name, "name is null or empty");
        Validate.notEmpty(mimeType, "mimeType is null or empty");
        this.data = data;
        this.name = name;
        this.mimeType = mimeType;
    }
    
    
    /**
     * @see javax.activation.DataSource#getContentType()
     */
    public String getContentType()
    {
        return mimeType;
    }


    /**
     * @see javax.activation.DataSource#getInputStream()
     */
    public InputStream getInputStream() throws IOException
    {
        return new ByteArrayInputStream(data);
    }


    /**
     * @see javax.activation.DataSource#getName()
     */
    public String getName()
    {
        return name;
    }


    /**
     * Not implemented.
     * @throws IOException Always.
     * @see javax.activation.DataSource#getOutputStream()
     */
    public OutputStream getOutputStream() throws IOException
    {
        throw new IOException("Output is not implemented");
    }

}




-----Original Message-----
From: Rob Cameron [mailto:cameronr@homes.com] 
Sent: 28 October 2005 13:12
To: poi-user@jakarta.apache.org
Subject: POI & JAF?


Hi all,

I am wondering if POI can be applied to a problem I am having.

I have some content in memory that I want to email to the user as an
.xls attachment. Using javax.activation, I create a DataHandler, passing
my content and the appropriate mime type as parameters to the
constructor. For this to work properly, there needs to be a class
associated with x-java-content-handler for that mime type (inside
activation.jar in mailcap.default or elsewhere). This all works just
fine for a .txt attachment.

I am wondering if there is a class in POI (HSSF) that could be used as
the x-java-content-handler for xls (application/msexcel). If not, I am
wondering if anyone has any other ideas for how to approach this issue.

Thanks,
Rob Cameron


---------------------------------------------------------------------
To unsubscribe, e-mail: poi-user-unsubscribe@jakarta.apache.org
Mailing List:     http://jakarta.apache.org/site/mail2.html#poi
The Apache Jakarta Poi Project:  http://jakarta.apache.org/poi/