简体   繁体   English

上传内容时,HTTP-Request中的内容类型值是什么?

[英]Whats Content-Type value within a HTTP-Request when uploading content?

I need to extract uploads from http-trafic. 我需要从http-trafic中提取上传内容。 How could do that? 怎么可能呢? First of all, the request-method will be POST. 首先,请求方法将是POST。 Secondly, there will be a Content-Type header-field. 其次,将有一个Content-Type标头字段。 I do not want to extract formular-data, but uploads like mail-attachements. 我不想提取公式数据,而是上传像邮件附件。

The content type is per specification multipart/form-data . 内容类型是每个规范 multipart/form-data

This is a special content type which can be visualized as multiple sub-requests in one big request. 这是一种特殊的内容类型,可以在一个大请求中显示为多个子请求。 Each of those sub-requests (one form-data element) has their own set of headers. 每个子请求(一个表单数据元素)都有自己的标题集。 The content type of the actual data is in there. 实际数据的内容类型在那里。

Here's an example how it look like with 1 normal field and 1 file field (in HTML terms, when using <input name="textfield"><input type="file" name="filefield"> ): 下面是一个示例,它是如何使用1个普通字段和1个文件字段(在HTML术语中,当使用<input name="textfield"><input type="file" name="filefield"> )时:

Content-Type: multipart/form-data;boundary=SOME_BOUNDARY

--SOME_BOUNDARY
content-disposition: form-data;name="textfield"
content-type: text/plain;charset=UTF-8

value of textfield here
--SOME_BOUNDARY
content-disposition: form-data;name="filefield";filename="some.ext"
content-type: application/octet-stream

binary file content here

--SOME_BOUNDARY--

As to parsing and extracting this data, practically every programming language has builtin/3rd party APIs for this. 至于解析和提取这些数据,实际上每种编程语言都有内置/第三方API。 As you didn't tell anything about which one you're using, it's impossible to give a targeted answer. 由于您没有说明您正在使用哪一个,因此无法给出有针对性的答案。 In case of for example Java, that would be either the 3rd party library Apache Commons FileUpload or when you're using Servlet 3.0, the API-provided request.getPart() method. 在例如Java的情况下,这可能是第三方库Apache Commons FileUpload,或者当您使用Servlet 3.0时,API提供的request.getPart()方法。

如果(我并不是说这是正确的方法)你只想保存字节数组中的数据,你应该看看如何读取POST主体: 读取POST体与bottle.py读取数据然后创建一个新文件应该可以解决问题。

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

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