用Java提取HTTP Request Body中包含的文件

时间:2016-09-01 12:02:02

标签: java http

我有一个HTTP请求正文包含在Buffer

--ec7db176-a2fa-402c-84a5-7e24634be58a
Content-Disposition: form-data; name="data"
Content-Transfer-Encoding: binary
Content-Type: image/jpeg
Content-Length: 54509

������JFIF��    [...more data...]
--ec7db176-a2fa-402c-84a5-7e24634be58a--

我想将图片数据提取到File

我不知道如何处理缓冲区以剪切前6行和最后一行并将数据复制到文件中。

我尝试将数据作为字符串读取,然后将其写入OutputSteam,但某些字符被错误编码。

File tempFile = this.file.get();
OutputStream os = new FileOutputStream(tempFile);

Buffer body = request.getBody();

String multipartBoundary = body.readUtf8Line();
String start = "\r\n\r\n";
String end = "\r\n" + multipartBoundary;
String stringBody = body.readUtf8();

byte[] bytes = StringUtils.substringBetween(stringBody, start, end)
                          .getBytes(Charset.forName("UTF-8"));
os.write(bytes);
os.close();

这是写入的第一个十六进制字节: enter image description here

上升是我得到的,而下行是原始文件

0 个答案:

没有答案