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

有没有办法在 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

https://stackoverflow.com/questions/2227182/

相关文章:

linux - 如何通过将其内存存储到磁盘并稍后恢复它来在 Linux 中 "hibernate"进

linux - 内核栈和用户空间栈

linux - gettimeofday() 是否保证为微秒级分辨率?

python - 在 Python 字符串中的最后一个分隔符上拆分?

python - 使用 argparse 需要两个参数之一

python - 如何让 python 的 pprint 返回一个字符串而不是打印?

linux - 使用 unix 命令 "watch"的颜色?

linux - Ubuntu - 在启动时使用 "sudo"运行命令

python - 如何在 Windows 上运行多个 Python 版本

python - 随机种子() : What does it do?