简体   繁体   English

如何将新创建的文件的 MIME 类型设置为 application/json? C++

[英]How to set the mime-type of a new created file to application/json? C++

I am creating a json string in C++ and save it to a file using fstream .我正在 C++ 中创建一个 json 字符串并使用fstream将其保存到文件中。

Here is the code for creating the file:下面是创建文件的代码:

  string json="{ \"a\"= 1 }";

  fstream datei1("jsonfile.json",ios::out);
  file1 << json << endl;
  file1.close();

How could one set the mime-type to 'application/json'??如何将 mime-type 设置为“application/json”?

file -i jsonfile.json in linux shell gives me: jsonfile.json: text/plain; charset=utf-8 linux shell 中的file -i jsonfile.json给了我: jsonfile.json: text/plain; charset=utf-8 jsonfile.json: text/plain; charset=utf-8

file command try to guess the type of your file by reading it. file 命令尝试通过读取文件来猜测文件的类型。

And read your file again: it is a plain text file.并再次阅读您的文件:它一个纯文本文件。 There is only a simple object stored, nothing that can lead to the application answer.只有一个简单的对象存储,没有什么可以导致应用程序的答案。

So without changing your file data, there is nothing you can do from your code to change file command answer.因此,在不更改文件数据的情况下,您无法从代码中更改文件命令答案。

From the documentation of file command :文件命令的文档中:

Causes the file command to output mime type strings rather than the more traditional human readable ones.使 file 命令输出 mime 类型的字符串,而不是更传统的人类可读的字符串。 Thus it may say 'text/plain;因此它可能会说“文本/纯文本; charset=us-ascii' rather than 'ASCII text'. charset=us-ascii' 而不是 'ASCII text'。 In order for this option to work , file changes the way it handles files recognized by the command itself (such as many of the text file types, directories etc), and makes use of an alternative 'magic' file .为了使这个选项起作用,文件改变了它处理由命令本身识别的文件的方式(例如许多文本文件类型、目录等),并使用替代的“魔法”文件 (See the FILES section, below). (请参阅下面的文件部分)。

/usr/share/file/magic.mgc Default compiled list of magic. /usr/share/file/magic.mgc默认编译的魔法列表。
/usr/share/file/magic Directory containing default magic files. /usr/share/file/magic包含默认魔法文件的目录。

You can read about magic files on the wiki .您可以在wiki上阅读有关魔法文件的信息。
Also you can add your own signatures in /etc/magic .您也可以在/etc/magic添加自己的签名。

But *.json is a plain text file, without any signatures, thus, probably, it's impossible to make OS think, that some file has application/json mime type without any hacks.但是*.json是一个纯文本文件,没有任何签名,因此,可能无法让操作系统认为某些文件具有application/json mime 类型而没有任何黑客攻击。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM