当前位置: 移动技术网 > IT编程>开发语言>Java > Java获取文件ContentType案例

Java获取文件ContentType案例

2020年08月25日  | 移动技术网IT编程  | 我要评论
源码如下:package com.oysept; import java.io.file;import java.io.ioexception;import java.net.filenamemap;

源码如下:

package com.oysept;
 
import java.io.file;
import java.io.ioexception;
import java.net.filenamemap;
import java.net.urlconnection;
import java.nio.file.files;
import java.nio.file.path;
import java.nio.file.paths; 
import javax.activation.mimetypesfiletypemap;
 
/**
 * java获取文件contenttype
 * @author ouyangjun
 */
public class contenttypeutils {
 
  public static void main(string[] args) {
    // 文件路径
    string fileurl = "c:\\users\\admin\\desktop\\tttt.rar";
    // 方式一
    getcontenttypebylocal(fileurl);
    
    // 方式二,推荐使用
    getcontenttype(fileurl);
		
    // 方式三
    getcontenttypebytype(fileurl);
  }
 
  /**
   * 方式一
   * 该方式只支持本地文件,有时候会存在获取为null的情况
   * @param fileurl
   */
  public static string getcontenttypebylocal(string fileurl) {
    string contenttype = null;
    path path = paths.get(fileurl);
    try {
      contenttype = files.probecontenttype(path);
    } catch (ioexception e) { 
      e.printstacktrace();
    }
    system.out.println("getcontenttypebylocal, file contenttype is : " + contenttype);
    return contenttype;
  }
	
  /**
   * 方式二
   * 该方式支持本地文件,也支持http/https远程文件
   * @param fileurl
   */
  public static string getcontenttype(string fileurl) {
    string contenttype = null;
    try {
      contenttype = new mimetypesfiletypemap().getcontenttype(new file(fileurl));
    } catch (exception e) {
      e.printstacktrace();
    }
    system.out.println("getcontenttype, file contenttype is : " + contenttype);
    return contenttype;
  }
	
  /**
   * 方式三
   * @param fileurl,有时候会存在获取为null的情况
   */
  public static string getcontenttypebytype(string fileurl) {
    string contenttype = null;
    try {
      filenamemap filenamemap = urlconnection.getfilenamemap();
      contenttype = filenamemap.getcontenttypefor(fileurl);
    } catch (exception e) {
      e.printstacktrace();
    }
    system.out.println("getcontenttypebytype, file contenttype is : " + contenttype);
    return contenttype;
  }
}

打印效果图:

补充知识:imagetypeutil工具类:java获取url对应的文件类型及其后缀

java获取url对应的文件类型及其后缀的主流方法有三种:

1、根据文件头部数据来判断。

通常需要先下载再判断,但是如果想要在下载的时候确定文件后缀,就做不到了,而且获取的文件类型不是很准确。

2、使用lastindexof去解析url字符串。

这种方法最简单高效。

3、urlconnection获取contenttype的类型推测出文件的类型。

这里我封装了一个工具类,将第二种方法和第三种方法结合,但是不是用lastindexof,而是判断url字符串是否包含图片的后缀。

package johny.utils; 
import java.net.urlconnection; 
/**
 * @author johny 林子豪 
 */
public enum imagetypeutil {
 
  png(".png", "image/png"),
  jpg(".jpg", "image/jpeg"),
  bmp(".bmp", "image/bmp"),
  jpeg(".jpeg", "image/jpeg"),
  gif(".gif", "image/gif"),
  tif(".tif", "image/tiff"),//标签图像文件格式(tagged image file format,简写为tiff)是一种主要用来存储包括照片和艺术图在内的图像的文件格式。它最初由aldus公司与微软公司一起为postscript打印开发。
  tiff(".tiff", "image/tiff"),
  fax(".fax", "image/fax"),
  ico(".ico", "image/x-icon"),
  jfif(".jfif", "image/jpeg"),
  jpe(".jpe", "image/jpeg"),
  net(".net", "image/pnetvue"),
  wbmp(".wbmp", "image/vnd.wap.wbmp");
  //如果有其他的mime类型,
 
  /**
   * 后缀名
   */
  final string msuffix;
  final string mmime;
 
  imagetypeutil(string suffix, string mime) {
    this.msuffix = suffix;
    this.mmime = mime;
  }
 
  public static string getsuffixfromurl(string url) {
 
    for (imagetypeutil filetype : values()) {
      if (url.contains(filetype.suffix())) {
        return filetype.suffix();
      }
    }
    string contenttype = getmimetypefromurl(url);
    if (contenttype == null) return null;
    return mimemapingsuffix(contenttype);
  }
 
  public static string getmimetypefromurl(string url) {
    if (url == null || url.isempty()) {
      return null;
    }
    return urlconnection.guesscontenttypefromname(url);
  }
 
  /**
   * mime类型对应的后缀名
   */
  public static string mimemapingsuffix(string mime) {
    for (imagetypeutil filetype : values()) {
      if (filetype.mime().equals(mime)) {
        return filetype.suffix();
      }
    }
    return null;
  }
 
  public string mime() {
    return mmime;
  }
 
  /**
   * 获取后缀名 * * @return 指定类型的后缀名,如'.mp4'
   */
  public string suffix() {
    return this.msuffix;
  } 
}

以上这篇java获取文件contenttype案例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网