【问题标题】:Get content type from stream in kotlin, Spring从 kotlin,Spring 中的流中获取内容类型
【发布时间】:2020-01-28 13:52:39
【问题描述】:

我正在从文件名中获取 MimeType! 但我需要从文件内容中获取 MimeType。 我该怎么做?

 //**************************
    val file = File(rFile.absoluteFile)
    val mimeType = MediaType.parseMediaType(Files.probeContentType(file.toPath()))
    val isImage = mimeType.isPresentIn(listOf(MediaType.IMAGE_GIF, MediaType.IMAGE_JPEG, MediaType.IMAGE_PNG))
    val isPdf = mimeType.isPresentIn(listOf(MediaType.APPLICATION_PDF))
    val contentDispositionType = if (isImage || isPdf) "inline" else "attachment"
    return ResponseEntity.ok()
        .header(HttpHeaders.CONTENT_DISPOSITION, "$contentDispositionType; filename=${rFile.uploadedFullName}")
        .contentType(mimeType)
        .body(FileUrlResource(file.toURI().toURL()))
 //**************************

【问题讨论】:

    标签: spring-boot kotlin


    【解决方案1】:

    我从this link找到了代码。

    byte[] byteArray = ...
    InputStream is = new BufferedInputStream(new ByteArrayInputStream(byteArray));
    String mimeType = URLConnection.guessContentTypeFromStream(is);
    

    对我有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-25
      • 2014-02-10
      • 2012-09-13
      • 1970-01-01
      • 1970-01-01
      • 2013-07-12
      • 1970-01-01
      相关资源
      最近更新 更多