Norman Maurer wrote:
> Hi all,
>
> i just looked at the mime4j javadoc and wonder what whould be the best
> solution to get all attachment names of a given inputstream.. I thought
> this will maybe give me a better performance then using javamail for
> this... Im wrong ?
>   
Mime4j should be faster for this kind of things. See the attachment for
an example of how to get the filename parameter from the
Content-Disposition fields using Mime4j. I think you are looking for
something like this, right?
> The other thing i noticed is that mime4j has no "altering" support.
> Anything planned here ?
>   
Nope, nothing planned at the moment. But if you have any ideas of how
Mime4j could be improved, please share them.

-- 
Niklas Therning
www.spamdrain.net

import java.io.FileInputStream;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.mime4j.AbstractContentHandler;
import org.mime4j.EOLConvertingInputStream;
import org.mime4j.MimeStreamParser;
import org.mime4j.field.Field;
import org.mime4j.field.UnstructuredField;


public class Main {
    private static final Pattern P = Pattern.compile(".*filename=(\".*?\"|[^\\s]*).*");

    public static void main(String[] args) throws IOException {
        MyHandler handler = new MyHandler();
        MimeStreamParser parser = new MimeStreamParser();
        parser.setContentHandler(handler);
        parser.parse(new EOLConvertingInputStream(new FileInputStream(args[0])));
    }

    public static class MyHandler extends AbstractContentHandler {
        @Override
        public void field(String fieldData) {
            if (fieldData.toLowerCase().startsWith("content-disposition:")) {
                UnstructuredField field = (UnstructuredField) Field.parse(fieldData);
                Matcher m = P.matcher(field.getValue());
                if (m.matches()) {
                    String filename = m.group(1);
                    System.out.println(filename);
                }
            }
        }
    }
}

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to