Java URL to File Name getFileNameFromContentDisposition(URLConnection connection)

Here you can find the source of getFileNameFromContentDisposition(URLConnection connection)

Description

get File Name From Content Disposition

License

Open Source License

Declaration

private static String getFileNameFromContentDisposition(URLConnection connection) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.net.URLConnection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    private static final String CONTENT_DISPOSITION_HEADER = "Content-Disposition";
    private static final Pattern CONTENT_DISPOSITION_FILE_NAME_PATTERN = Pattern
            .compile(".*filename=\"([^\\s;]*)\".*");
    private static final int CONTENT_DISPOSITION_FILE_NAME_PATTERN_GROUP = 1;

    private static String getFileNameFromContentDisposition(URLConnection connection) {
        String contentDispositionHeaderValue = connection.getHeaderField(CONTENT_DISPOSITION_HEADER);
        if (contentDispositionHeaderValue != null) {
            Matcher matcher = CONTENT_DISPOSITION_FILE_NAME_PATTERN.matcher(contentDispositionHeaderValue);
            if (matcher.matches()) {
                return matcher.group(CONTENT_DISPOSITION_FILE_NAME_PATTERN_GROUP);
            }/*from w ww .j  a  va2  s. c  o m*/
        }
        return null;
    }
}

Related

  1. getFileName(URL url)
  2. getFileName(URL url)
  3. getFileName(URLConnection urlC)
  4. getFileName2(String url)
  5. getFileNameArray(String url)
  6. getFileNameFromUrl(URL inputUrl)
  7. getFileNameFromUrl(URL inputUrl)
  8. URIToFilename(String str)