Java File Name Extract extractFileNameFromContentDisposition(String contentDisposition)

Here you can find the source of extractFileNameFromContentDisposition(String contentDisposition)

Description

Extract the file name from the content disposition header.

License

Apache License

Parameter

Parameter Description
contentDisposition - the content-disposition header. Cannot be <code>null>/code>.

Return

the file name, or null if the content-disposition header does not contain the filename attribute.

Declaration

public static final String extractFileNameFromContentDisposition(String contentDisposition) 

Method Source Code

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

public class Main {
    /**// w ww  .  j  a  va  2 s .  c  om
     * Extract the file name from the content disposition header.
     * <p>
     * See <a
     * href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html">http:
     * //www.w3.org/Protocols/rfc2616/rfc2616-sec19.html</a> for detailled
     * information regarding the headers in HTML.
     * 
     * @param contentDisposition
     *            - the content-disposition header. Cannot be <code>null>/code>.
     * @return the file name, or <code>null</code> if the content-disposition
     *         header does not contain the filename attribute.
     */
    public static final String extractFileNameFromContentDisposition(String contentDisposition) {
        System.out.println("content disposition = " + contentDisposition);
        String[] attributes = contentDisposition.split(";");

        for (String a : attributes) {
            if (a.toLowerCase().contains("filename")) {
                // The attribute is the file name. The filename is between
                // quotes.
                try {
                    return a.substring(a.indexOf('\"') + 1, a.lastIndexOf('\"'));
                } catch (Exception e) {
                    return a.substring(a.indexOf('=') + 1, a.length());
                }
            }
        }

        // not found
        return null;
    }
}

Related

  1. extractFilename(String path)
  2. extractFileName(String path)
  3. extractFilename(String path)
  4. extractFileName(String url)
  5. extractFileNameFromBAMLocation(String location)
  6. extractFileNameFromPath(final String filePath, char separatorChar)
  7. extractFileNameFromPath(final String path)
  8. extractFilenameFromPath(String path)
  9. extractFilenameFromURL(String url)