【问题标题】:Create a plain HTTP PUT multipart request with a jpg attached创建一个带有 jpg 的普通 HTTP PUT 多部分请求
【发布时间】:2013-12-16 18:38:17
【问题描述】:

我正在手动创建一个 HTTP PUT 请求。我有以下格式

POST http://server.com/id/55/push HTTP/1.0
Content-type: multipart/form-data, boundary=AaB03x
Content-Length: 168

--AaB03x
Content-Disposition: form-data; name="image"; filename="small.jpg"
Content-Type: image/jpeg
Content-Transfer-Encoding: binary

<file content>
--AaB03x--

我的问题是,我应该如何填写“文件内容”区域?如果我使用 TexMate 或 cat 命令行应用程序打开 jpeg 并粘贴 ASCII 输出,则请求不起作用。

更新

我正在使用微处理器,我无法使用 C 或高级语言,我需要手动执行原始请求。我需要用空格分隔从文件中读取的每个二进制字节吗?

如果将jpg保存到服务器端的文件中,是否必须将二进制流转换为ASCII?

我使用简单的 php conde 从硬盘读取 JPG 的二进制代码:

$filename = "pic.jpg";
$handle = fopen($filename, "rb");
$fsize = filesize($filename);
$contents = fread($handle, filesize($filename));
fclose($handle);
//echo $contents;
for($i = 0; $i < $fsize; $i++)
{ 
  // get the current ASCII character representation of the current byte
   $asciiCharacter = $contents[$i];
   // get the base 10 value of the current characer
   $base10value = ord($asciiCharacter);
   // now convert that byte from base 10 to base 2 (i.e 01001010...)
   $base2representation = base_convert($base10value, 10, 2);
   // print the 0s and 1s
   echo($base2representation);
}

使用这段代码,我得到一个 1 和 0 的流。我可以将它包括 101010101 的字符串发送到我手动 http 请求的标签“文件内容”所在的位置,但在服务器端我不能将 JPG 可视化...¿我应该再次将其转换为 ASCII 吗?

解决方案

好的,解决方法很简单,我只是将 ASCII 码转储到 http 请求的标签“文件内容”中。尽管我使用的是微控制器,但我还是用 PHP 打开了一个套接字并进行了测试。解决方案是从文件中读取 ASCII,而不是直接将 ASCII 粘贴到代码中。

这里是解决方案的一个工作示例:

<?php

//We read the file from the hard drive
$filename = "pic.jpg";
$handle = fopen($filename, "rb");
$fsize = filesize($filename);
$contents = fread($handle, filesize($filename));
fclose($handle);

$mesage = $contents;

//A trick to calculate the length of the HTTP body
$len = strlen('--AaB03x
Content-Disposition: form-data; name="image"; filename="small.jpg"
Content-Type: image/jpeg
Content-Transfer-Encoding: binary

'.$mesage.'
--AaB03x--');


//We create the HTTP request
$out = "POST /temp/test.php HTTP/1.0\r\n";
$out .= "Content-type: multipart/form-data boundary=AaB03x\r\n";
$out .= "Content-Length: $len\r\n\r\n";

$out .= "--AaB03x\r\n";
$out .= "Content-Disposition: form-data; name=\"image\"; filename=\"small.jpg\"\r\n";
$out .= "Content-Type: image/jpeg\r\n";
$out .= "Content-Transfer-Encoding: binary\r\n\r\n";

$out .= "$mesage\r\n";
$out .= "--AaB03x--\r\n\r\n";

//Open the socket 
$fp = fsockopen("127.0.0.1", 8888, $errno, $errstr, 30);
if (!$fp) {
    echo "$errstr ($errno)<br />\n";
} else {
    
    //we send the message thought the opened socket
    fwrite($fp, $out);
    while (!feof($fp)) {
        echo fgets($fp, 128);
    }
    fclose($fp);
}

//Visualize the query sent
echo nl2br($out);
?>

在实际的实现中,我将像在 php 中那样直接从微控制器的内存中读取数据

【问题讨论】:

    标签: http post multipartform-data put


    【解决方案1】:

    您需要有一个输出连接的 OutputStream 并使用此 Stream 来写入您从文件中读取的所有字节。

    如果您使用 C#。你可以检查这个:Sending Files using HTTP POST in c#

    对于 Java:

    【讨论】:

      【解决方案2】:

      你打错了最后一个边界,应该是:

      --AaB03x--
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-20
        • 2011-02-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多