Example usage for java.net URLConnection guessContentTypeFromName

List of usage examples for java.net URLConnection guessContentTypeFromName

Introduction

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

Prototype

public static String guessContentTypeFromName(String fname) 

Source Link

Document

Tries to determine the content type of an object, based on the specified "file" component of a URL.

Usage

From source file:Main.java

@NonNull
public static Intent openContent(@NonNull Uri uri) {
    final Intent intent = new Intent(Intent.ACTION_VIEW, uri);
    final String mime = URLConnection.guessContentTypeFromName(uri.toString());
    if (!TextUtils.isEmpty(mime)) {
        intent.setType(mime);//  www  .java 2 s .c om
    }
    return intent;
}

From source file:Main.java

public static boolean isCallable(Context context, String url) {
    String mimeTypeExtension = URLConnection.guessContentTypeFromName(url);

    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.parse(url), mimeTypeExtension);
    List<ResolveInfo> list = context.getPackageManager().queryIntentActivities(intent,
            PackageManager.MATCH_DEFAULT_ONLY);
    return list.size() > 0;
}

From source file:us.physion.ovation.ui.editor.ContentTypes.java

public static String getContentType(File file) throws IOException {
    String contentType = URLConnection.guessContentTypeFromName(file.getName());
    if (contentType == null) {
        Map<String, String> customContentTypes = customTypes();

        final String extension = FilenameUtils.getExtension(file.getName());
        if (customContentTypes.containsKey(extension)) {
            contentType = customContentTypes.get(extension);
        } else {/*from w  w  w.j av  a 2 s . co m*/
            contentType = "application/octet-stream"; // fallback to binary
        }
    }

    return contentType;
}

From source file:poisondog.net.FileEntity.java

@Override
public String getContentType() {
    return URLConnection.guessContentTypeFromName(mFile.getName());
}

From source file:org.shareok.data.webserv.WebUtil.java

public static void setupFileDownload(HttpServletResponse response, String downloadPath) {
    try {// w  ww .  j a  v  a2s  .  c  o  m
        File file = new File(downloadPath);
        if (!file.exists()) {
            String errorMessage = "Sorry. The file you are looking for does not exist";
            System.out.println(errorMessage);
            OutputStream outputStream = response.getOutputStream();
            outputStream.write(errorMessage.getBytes(Charset.forName("UTF-8")));
            outputStream.close();
            return;
        }

        String mimeType = URLConnection.guessContentTypeFromName(file.getName());
        if (mimeType == null) {
            System.out.println("mimetype is not detectable, will take default");
            mimeType = "application/octet-stream";
        }

        response.setContentType(mimeType);
        /* "Content-Disposition : inline" will show viewable types [like images/text/pdf/anything viewable by browser] right on browser 
        while others(zip e.g) will be directly downloaded [may provide save as popup, based on your browser setting.]*/
        response.setHeader("Content-Disposition",
                String.format("attachment; filename=\"" + file.getName() + "\""));

        /* "Content-Disposition : attachment" will be directly download, may provide save as popup, based on your browser setting*/
        //response.setHeader("Content-Disposition", String.format("attachment; filename=\"%s\"", file.getName()));

        response.setContentLength((int) file.length());

        InputStream inputStream = new BufferedInputStream(new FileInputStream(file));

        //Copy bytes from source to destination(outputstream in this example), closes both streams.
        FileCopyUtils.copy(inputStream, response.getOutputStream());
    } catch (IOException ioex) {
        logger.error("Cannot set up a file download.", ioex);
    }
}

From source file:org.openeos.services.ui.vaadin.internal.URLResource.java

public URLResource(URL url, Application application) {
    this.url = url;
    this.application = application;
    mimeType = URLConnection.guessContentTypeFromName(url.getFile());
    fileName = DigestUtils.md5Hex(url.toString()) + url.toString().substring(url.toString().lastIndexOf("."));
    application.addResource(this);
}

From source file:de.hska.ld.content.persistence.domain.Attachment.java

public void setNewValues(InputStream inputStream, String name) {
    try {//from   w  w w  . java  2s.c  o  m
        this.mimeType = URLConnection.guessContentTypeFromName(name);
        this.source = IOUtils.toByteArray(inputStream);
        this.name = name;
    } catch (IOException e) {
        LOGGER.warn("Unable to set source or mime type from input stream.", e);
    }
}

From source file:de.rwthaachen.rz.rwthapp.plugins.fileopener.FileOpener.java

private boolean openFile(String url) throws IOException {
    // Create URI
    Uri uri = Uri.parse(url);//from   www .  j  ava  2s. c o  m

    Intent intent;
    // Check what kind of file you are trying to open, by comparing the url with extensions.
    // When the if condition is matched, plugin sets the correct intent (mime) type,
    // so Android knew what application to use to open the file

    String mimeType = URLConnection.guessContentTypeFromName(url);
    intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(uri, mimeType);

    try {
        this.cordova.getActivity().startActivity(intent);
        return true;
    } catch (Exception e) {
        return false;
    }
}

From source file:de.laeubisoft.tools.ant.validation.Tools.java

/**
 * Creates a {@link RequestEntity} that can be used for submitting a file
 * //w ww .  j  a  va2 s  .c o m
 * @param params
 *            the params to use
 * @param methodParams
 *            the {@link HttpMethodParams} of the requesting method
 * @return {@link RequestEntity} that can be used for submitting the given
 *         file via Multipart
 * @throws IOException
 *             if something is wrong with the file...
 */
public static RequestEntity createFileUpload(File file, String filePartName, String charset,
        List<NameValuePair> params, HttpMethodParams methodParams) throws IOException {
    if (file == null) {
        throw new FileNotFoundException("file not present!");
    }
    List<Part> parts = nvToParts(params);
    FilePart fp = new FilePart(filePartName, file);
    fp.setContentType(URLConnection.guessContentTypeFromName(file.getName()));
    if (charset != null) {
        fp.setCharSet(charset);
    }
    parts.add(fp);
    return new MultipartRequestEntity(parts.toArray(new Part[0]), methodParams);
}

From source file:de.hska.ld.content.persistence.domain.Attachment.java

public void setNewValues(String name) {
    this.mimeType = URLConnection.guessContentTypeFromName(name);
    this.name = name;
}