Example usage for javax.mail.internet ContentType setParameter

List of usage examples for javax.mail.internet ContentType setParameter

Introduction

In this page you can find the example usage for javax.mail.internet ContentType setParameter.

Prototype

public void setParameter(String name, String value) 

Source Link

Document

Set the specified parameter.

Usage

From source file:immf.Util.java

public static void setFileName(Part part, String filename, String charset, String lang)
        throws MessagingException {

    ContentDisposition disposition;/*from   w w  w .  j av a2 s  .c o  m*/
    String[] strings = part.getHeader("Content-Disposition");
    if (strings == null || strings.length < 1) {
        disposition = new ContentDisposition(Part.ATTACHMENT);
    } else {
        disposition = new ContentDisposition(strings[0]);
        disposition.getParameterList().remove("filename");
    }

    part.setHeader("Content-Disposition",
            disposition.toString() + encodeParameter("filename", filename, charset, lang));

    ContentType cType;
    strings = part.getHeader("Content-Type");
    if (strings == null || strings.length < 1) {
        cType = new ContentType(part.getDataHandler().getContentType());
    } else {
        cType = new ContentType(strings[0]);
    }

    try {
        // I want to public the MimeUtility#doEncode()!!!
        String mimeString = MimeUtility.encodeWord(filename, charset, "B");
        // cut <CRLF>...
        StringBuffer sb = new StringBuffer();
        int i;
        while ((i = mimeString.indexOf('\r')) != -1) {
            sb.append(mimeString.substring(0, i));
            mimeString = mimeString.substring(i + 2);
        }
        sb.append(mimeString);

        cType.setParameter("name", new String(sb));
    } catch (UnsupportedEncodingException e) {
        throw new MessagingException("Encoding error", e);
    }
    part.setHeader("Content-Type", cType.toString());
}