linux - 如何找出文件的 MIME 类型(Content-Type)?

标签 linux bash content-type mime mime-types

有没有办法在 Linux bash 脚本中找出文件的 MIME 类型(或者称为“Content-Type”?)?

我需要它的原因是因为 ImageShack 似乎需要它来上传文件,因为某种原因它将 .png 文件检测为 application/octet-stream 文件。

我检查了文件,确实是 PNG 图像:

$ cat /1.png 
?PNG
(with a heap load of random characters)

这给了我错误:

$ curl -F "fileupload=@/1.png" http://www.imageshack.us/upload_api.php
<links>
<error id="wrong_file_type">Wrong file type detected for file 1.png:application/octet-stream</error>
</links>

这可行,但我需要指定 MIME-TYPE。

$ curl -F "fileupload=@/1.png;type=image/png" http://www.imageshack.us/upload_api.php

最佳答案

使用文件。例子:

> file --mime-type image.png
image.png: image/png

> file -b --mime-type image.png
image/png

> file -i FILE_NAME
image.png: image/png; charset=binary

关于linux - 如何找出文件的 MIME 类型(Content-Type)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2227182/

相关文章:

linux - 为什么我的 crontab 不工作?

arrays - 在groovy中以数组的形式显示字段值

java - 仅在内容类型 === JSON 时更改 Java 过滤器中的内容类型或字符编码

node.js - 遇到错误找不到服务器上的npm和node./usr/bin/env : node: No such file or directory

linux - 如何通过 bash 脚本中的管道从命令中提取 2 条信息输出?

java - OpenJDK 64 位服务器 VM 警告 : ignoring option MaxPermSize=350m;

bash - Docker exec - 将文本写入容器中的文件

mysql - 无法从终端执行 mysql 命令

在 Apache CXF JAX-RS 中映射扩展内容类型

linux - 将 UBIFS 分区复制到 NAND 设备