Java String Encode encodeFilename(final String filename, final String userAgent)

Here you can find the source of encodeFilename(final String filename, final String userAgent)

Description

Hack to get the correct format of the file name, based on USER-AGENT string.

License

Open Source License

Parameter

Parameter Description
filename File name to encode.
userAgent The request's USER-AGENT string.

Return

The coded file name.

Declaration

public static String encodeFilename(final String filename, final String userAgent) 

Method Source Code


//package com.java2s;
/*// w ww.j av  a2  s  .  c o  m
 * ====================================================================
 * Copyright (c) 2005-2012 sventon project. All rights reserved.
 *
 * This software is licensed as described in the file LICENSE, which
 * you should have received as part of this distribution. The terms
 * are also available at http://www.sventon.org.
 * If newer versions of this license are posted there, you may use a
 * newer version instead, at your option.
 * ====================================================================
 */

import javax.mail.internet.MimeUtility;
import java.io.UnsupportedEncodingException;

import java.net.URLEncoder;

public class Main {
    /**
     * Default charset, UTF-8.
     */
    private static final String DEFAULT_CHARSET = "UTF-8";

    /**
     * Hack to get the correct format of the file name, based on <code>USER-AGENT</code> string.
     * File name will be returned as-is if unable to parse <code>USER-AGENT</code>.
     *
     * @param filename  File name to encode.
     * @param userAgent The request's USER-AGENT string.
     * @return The coded file name.
     */
    public static String encodeFilename(final String filename, final String userAgent) {
        String codedFilename = null;
        try {
            if (null != userAgent && -1 != userAgent.indexOf("MSIE")) {
                codedFilename = URLEncoder.encode(filename, DEFAULT_CHARSET);
            } else if (null != userAgent && -1 != userAgent.indexOf("Mozilla")) {
                codedFilename = MimeUtility.encodeText(filename, DEFAULT_CHARSET, "B");
            }
        } catch (UnsupportedEncodingException uee) {
            // Silently ignore
        }
        return codedFilename != null ? codedFilename : filename;
    }

    /**
     * Encodes given string into <tt>application/x-www-form-urlencoded</tt> format using default encoding (UTF-8).
     *
     * @param str String to encode.
     * @return Encoded string.
     */
    public static String encode(final String str) {
        String s = "";
        try {
            s = URLEncoder.encode(str, DEFAULT_CHARSET);
        } catch (UnsupportedEncodingException e) {
            // ignore
        }
        return s;
    }
}

Related

  1. encodeBase64(String str)
  2. encodeConvenience(String str)
  3. encodeDataPair(final StringBuilder buffer, final String key, final String value)
  4. encodeDoublePercent(String input)
  5. encodeDownloadFileName(String s)
  6. encodeFilename(String filename, String encoding)
  7. encodeFilenameOmittingWhiteSpaces(String filename, String encoding)
  8. encodeForm(Map form)
  9. encodeIncludingSpecialCharacters(String toEncode)