【问题标题】:Best way to get java.io.File MIME Type in java 8在 java 8 中获取 java.io.File MIME 类型的最佳方法
【发布时间】:2018-06-13 11:18:27
【问题描述】:

要获取我正在尝试使用的文件 mime 类型:

String mimeType = new MimetypesFileTypeMap().getContentType(document);

但它总是返回application octet stream

当我尝试使用时

String mimeType = Files.probeContentType(document.toPath()); 它总是返回null

我试图获取其 mime 类型的文档类型是:java.io.File

关于我该怎么做的任何想法? 我已经在使用 Spring,spring 中是否有任何东西可以处理文件 mime 类型?

谢谢

【问题讨论】:

  • 文件是什么,您期望结果是什么,为什么?
  • 该文件是一个 java.io.File 我希望得到这个 java.io.File 的 mime 类型,因为我可以允许或不上传这个文件谢谢

标签: java spring file java-8 mime-types


【解决方案1】:

最后我用了一个对象“org.apache.cxf.jaxrs.ext.multipart.Attachment”而不是java.io.File

您可以轻松处理附件

要获取 mimeType,您可以使用

public String getMimeType(Attachment document){
    String mimeType = document.getDataHandler().getContentType();
    return mimeType;
}

要获取文档大小,您可以使用:

public int getSize(Attachment document){
    byte[] documentInByte;
    try {
        documentInByte = 
        IOUtils.toByteArray(document.getDataHandler().getInputStream());
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }

    return data.length;
}

【讨论】:

    猜你喜欢
    • 2011-11-22
    • 2012-07-04
    • 2011-04-04
    • 1970-01-01
    • 2021-12-31
    • 1970-01-01
    • 2014-08-06
    • 2013-07-07
    • 2012-04-21
    相关资源
    最近更新 更多