Example usage for java.net URLConnection getFileNameMap

List of usage examples for java.net URLConnection getFileNameMap

Introduction

In this page you can find the example usage for java.net URLConnection getFileNameMap.

Prototype

public static FileNameMap getFileNameMap() 

Source Link

Document

Loads filename map (a mimetable) from a data file.

Usage

From source file:nl.clockwork.common.util.Utils.java

public static String getMimeType(String fileName) throws java.io.IOException {
    FileNameMap fileNameMap = URLConnection.getFileNameMap();
    return fileNameMap.getContentTypeFor(fileName);
}

From source file:com.whiuk.philip.opensmime.PathConverter.java

private static FileInformation handleFileScheme(Context context, Uri uri) {
    FileInputStream fileInputStream = null;
    final String filePath = uri.getPath();
    try {/*from ww  w. j a  v a  2s . co  m*/
        File srcFile = new File(filePath);
        fileInputStream = new FileInputStream(srcFile);
        File tmpFile = copyToTempFile(context, fileInputStream);
        FileNameMap fileNameMap = URLConnection.getFileNameMap();
        String mimeType = fileNameMap.getContentTypeFor(filePath);
        String fileName = FilenameUtils.getName(filePath);

        return new FileInformation(tmpFile, fileName, mimeType);
    } catch (IOException e) {
        Log.e(OpenSMIME.LOG_TAG, "error acquiring FileInforamtion in handleFileScheme", e);
    }

    return null;
}

From source file:de.fau.cs.mad.smile.android.encryption.PathConverter.java

private static FileInformation handleFileScheme(Context context, Uri uri) {
    FileInputStream fileInputStream = null;
    final String filePath = uri.getPath();
    try {/*from   ww  w .  ja va2  s .co m*/
        File srcFile = new File(filePath);
        fileInputStream = new FileInputStream(srcFile);
        File tmpFile = copyToTempFile(context, fileInputStream);
        FileNameMap fileNameMap = URLConnection.getFileNameMap();
        String mimeType = fileNameMap.getContentTypeFor(filePath);
        String fileName = FilenameUtils.getName(filePath);

        return new FileInformation(tmpFile, fileName, mimeType);
    } catch (IOException e) {
        Log.e(SMileCrypto.LOG_TAG, "error acquiring FileInforamtion in handleFileScheme", e);
    }

    return null;
}

From source file:org.jbpm.bpel.tools.ant.RegistrationTask.java

protected void writeRequest(PostMethod post) throws IOException {
    // base location
    StringPart locationPart = new StringPart("baseLocation", baseLocation);

    // description file
    String contentType = descriptionFile != null
            ? URLConnection.getFileNameMap().getContentTypeFor(descriptionFile.getName())
            : null;/* w  w w . j  a  v  a2  s .  com*/
    FilePart descriptionPart = new FilePart("descriptionFile", descriptionFile, contentType,
            FileUtil.DEFAULT_CHARSET.name());

    // multipart request
    post.setRequestEntity(
            new MultipartRequestEntity(new Part[] { locationPart, descriptionPart }, post.getParams()));

    log("registering description: " + (descriptionFile != null ? descriptionFile.getName() : baseLocation));
}

From source file:com.atlassian.connector.eclipse.team.ui.AbstractTeamUiConnector.java

protected String getContentType(IFile file) {
    String mimeType = file != null ? URLConnection.getFileNameMap().getContentTypeFor(file.getName()) : null;
    return mimeType == null ? UploadItem.DEFAULT_CONTENT_TYPE : mimeType;
}

From source file:com.qmetry.qaf.automation.util.FileUtil.java

public static String getContentType(File f) {
    MimetypesFileTypeMap fileTypeMap = new MimetypesFileTypeMap();
    FileNameMap fileNameMap = URLConnection.getFileNameMap();
    String ct = fileNameMap.getContentTypeFor(f.getName());
    return StringUtil.isBlank(ct) ? fileTypeMap.getContentType(f) : ct;
}

From source file:org.jboss.dashboard.ui.controller.responses.SendStreamResponse.java

/**
 * Send a file as response.//from  w  w  w  .  j  a  v a 2s  .co  m
 *
 * @param f File to send
 */
public SendStreamResponse(File f) {
    try {
        init(new FileInputStream(f), "inline; filename=" + URLEncoder.encode(f.getName()) + ";");
        FileNameMap fileNameMap = URLConnection.getFileNameMap();
        contentType = fileNameMap.getContentTypeFor(f.getName());
    } catch (FileNotFoundException e) {
        log.error("Error:", e);
        errorCode = HttpServletResponse.SC_NOT_FOUND;
    }
}

From source file:org.ow2.proactive_grid_cloud_portal.cli.cmd.sched.SubmitJobCommand.java

@Override
public void execute(ApplicationContext currentContext) throws CLIException {

    try {//from  ww w  .  ja va2s .com
        validateFilePath(currentContext);
        File jobFile = new File(pathname);
        String contentType = URLConnection.getFileNameMap().getContentTypeFor(pathname);
        JobIdData jobId;
        if (APPLICATION_XML.getMimeType().equals(contentType)) {
            jobId = currentContext.getRestClient().submitXml(currentContext.getSessionId(),
                    new FileInputStream(jobFile), map(this.variables));
        } else {
            jobId = currentContext.getRestClient().submitJobArchive(currentContext.getSessionId(),
                    new FileInputStream(jobFile), map(this.variables));
        }
        writeLine(currentContext, "Job('%s') successfully submitted: job('%d')", pathname, jobId.getId());
        resultStack(currentContext).push(jobId);
    } catch (Exception e) {
        handleError(String.format("An error occurred while attempting to submit job('%s'):", pathname), e,
                currentContext);
    }

}

From source file:com.discovery.darchrow.io.MimeTypeUtil.java

/**
 *  content type by file name.<br>
 * //from w w  w .java2  s  .  c om
 * //TODO
 * <b>
 * Very incomplete function. As of Java 7, html, pdf and jpeg extensions return the correct mime-type but js and css return null! </b> <br>
 * 
 * <p>
 * I tried Apache Tika but it is huge with tons of dependencies, <br>
 * URLConnection doesn't use the bytes of the files, <br>
 * MimetypesFileTypeMap also just looks at files names,<br>
 * and I couldn't move to Java 7.
 * </p>
 * 
 * @param fileName
 *            the file name
 * @return the content type by file name
 * @see java.net.URLConnection#getFileNameMap()
 * @see javax.activation.MimetypesFileTypeMap#getContentType(java.io.File)
 * @see java.net.FileNameMap#getContentTypeFor(String)
 * @see org.apache.commons.io.FilenameUtils#getExtension(String)
 * @see java.net.URLConnection#guessContentTypeFromName(String)
 * @see java.net.URLConnection#guessContentTypeFromStream(java.io.InputStream)
 * @see "java.nio.file.Files#probeContentType(java.nio.file.Path)"
 */
public static String getContentTypeByFileName(String fileName) {

    String extension = FilenameUtils.getExtension(fileName);
    if (Validator.isNullOrEmpty(extension)) {
        return null;
    }

    // 1. first use java's build-in utils
    //??? mimetable? "content.types.user.table" ?? java  lib/content-types.properties 
    FileNameMap fileNameMap = URLConnection.getFileNameMap();
    String contentType = fileNameMap.getContentTypeFor(fileName);

    // 2. nothing found -> lookup our in extension map to find types like ".doc" or ".docx"
    if (Validator.isNullOrEmpty(contentType)) {
        contentType = fileExtensionMap.get(extension.toLowerCase());
    }

    return contentType;
}

From source file:com.sunchenbin.store.feilong.core.io.MimeTypeUtil.java

/**
 *  content type by file name.//from   www .  jav a2s . co  m
 * 
 * <p>
 * <b>Very incomplete function. As of Java 7, html, pdf and jpeg extensions return the correct mime-type but js and css return null!
 * </b>
 * </p>
 * 
 * <p>
 * I tried Apache Tika but it is huge with tons of dependencies, <br>
 * URLConnection doesn't use the bytes of the files, <br>
 * {@link MimetypesFileTypeMap} also just looks at files names,and I couldn't move to Java 7.
 * </p>
 * 
 * @param fileName
 *            the file name
 * @return the content type by file name
 * @see java.net.URLConnection#getFileNameMap()
 * @see javax.activation.MimetypesFileTypeMap#getContentType(java.io.File)
 * @see java.net.FileNameMap#getContentTypeFor(String)
 * @see org.apache.commons.io.FilenameUtils#getExtension(String)
 * @see java.net.URLConnection#guessContentTypeFromName(String)
 * @see java.net.URLConnection#guessContentTypeFromStream(java.io.InputStream)
 * @see "java.nio.file.Files#probeContentType(java.nio.file.Path)"
 */
public static String getContentTypeByFileName(String fileName) {

    String extension = FilenameUtils.getExtension(fileName);
    if (Validator.isNullOrEmpty(extension)) {
        return StringUtils.EMPTY;
    }

    // 1. first use java's build-in utils
    //??? mimetable? "content.types.user.table" ,?? java  lib/content-types.properties 
    FileNameMap fileNameMap = URLConnection.getFileNameMap();
    String contentType = fileNameMap.getContentTypeFor(fileName);

    // 2. nothing found -> lookup our in extension map to find types like ".doc" or ".docx"
    if (Validator.isNullOrEmpty(contentType)) {
        contentType = fileExtensionMap.get(extension.toLowerCase());
    }

    return contentType;
}