Java String Encode encodeFilenameOmittingWhiteSpaces(String filename, String encoding)

Here you can find the source of encodeFilenameOmittingWhiteSpaces(String filename, String encoding)

Description

encode Filename Omitting White Spaces

License

Apache License

Declaration

public static String encodeFilenameOmittingWhiteSpaces(String filename, String encoding) throws Exception 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.net.URLEncoder;

public class Main {
    public static String encodeFilenameOmittingWhiteSpaces(String filename, String encoding) throws Exception {

        if (filename == null || filename.length() == 0)
            return "";
        else {/*from w  ww. j av a2s  .c  o  m*/
            int index = filename.indexOf(' ');
            if (index == -1) {
                return encodeFilename(filename, encoding);
            } else {
                return URLEncoder.encode(filename.substring(0, index), encoding) + " "
                        + encodeFilename(filename.substring(index + 1), encoding);
            }

        }
    }

    public static String encodeFilename(String filename, String encoding) throws Exception {
        String splChars = ".*[#$%?@].*";
        if (filename.matches(splChars))
            return URLEncoder.encode(filename, encoding);

        return filename;
    }
}

Related

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