解析Spring中的multipart/form-data响应。

huangapple go评论95阅读模式
英文:

parse multipart/form-data response in spring

问题

我需要接收multipart/form-data响应,
但我对如何解析这种类型的响应毫无头绪。

举个例子:

--mf8sckatxs4PpMnOLF6ltSv26ZJc5qxy9qq
Content-Disposition: form-data; name="arguments"
Content-Type: text/plain;charset=UTF-8
Content-Length: 311
[{"code":200,"message":"123"}]

Content-Disposition: form-data; name="_0"; filename="0_BODY_feature"
Content-Type: application/octet-stream
Content-Length: 407
binarydata

英文:

I need to receive multipart/form-data response
but i have no idea on how to parse this kind of response

For example

--mf8sckatxs4PpMnOLF6ltSv26ZJc5qxy9qq
Content-Disposition: form-data; name="arguments"
Content-Type: text/plain;charset=UTF-8
Content-Length: 311
[{"code":200,"message":"123"}]

Content-Disposition: form-data; name="_0"; filename="0_BODY_feature"
Content-Type: application/octet-stream
Content-Length: 407 
binarydata

答案1

得分: 1

由于您正在使用Spring,您已经可以使用Apache的fileupload和http类。 MultipartStream 被设计为附加到传入的字节流,并可以提供数据到达时的进度更新。

这个简单的示例演示了一个非流式的情景,其中您已经缓冲了整个传入主体。

byte[] yourResponse = ... // 整个响应作为字节数组
String yourContentType = ... // Content-Type 头字符串

ContentType contentType = ContentType.parse(yourContentType);

MultipartStream multipartStream = new MultipartStream(
  new ByteArrayInputStream(yourResponse),
  contentType.getParameter("boundary").getBytes(),
  1024,    // 内部缓冲区大小(您可以选择)
  null);   // 进度指示器(无)

boolean nextPart = multipartStream.skipPreamble();
while (nextPart) {

  ByteArrayOutputStream output = new ByteArrayOutputStream();
  String partHeaders = multipartStream.readHeaders();
  multipartStream.readBodyData(output);

  // 处理多行部分头部
  // 处理部分 'output' 字节数组

  nextPart = multipartStream.readBoundary();
}

根据需要添加异常处理。

英文:

Since you're using Spring you already have the Apache fileupload and http classes available. MultipartStream is designed to be attached to an incoming byte stream and can provide progress updates as data arrives.

This simple example illustrates a non-streaming scenario where you've already buffered the whole incoming body.

byte[] yourResponse = ... // the whole response as a byte array
String yourContentType = ... // the Content-Type header string

ContentType contentType = ContentType.parse(yourContentType);

MultipartStream multipartStream = new MultipartStream(
  new ByteArrayInputStream(yourResponse),
  contentType.getParameter("boundary").getBytes(), 
  1024,    // internal buffer size (you choose)
  null);   // progress indicator (none)

boolean nextPart = multipartStream.skipPreamble();
while (nextPart) {

  ByteArrayOutputStream output = new ByteArrayOutputStream();
  String partHeaders = multipartStream.readHeaders();
  multipartStream.readBodyData(output);

  // do something with the multi-line part headers
  // do something with the part 'output' byte array

  nextPart = multipartStream.readBoundary();
}

Add exception handling as required.

答案2

得分: 0

你可以将这些值存储并分隔在一个字符串数组中:

String[] array = "allyourinputtext".split(";");

这将在分号后分隔这些值。然后,你可以通过以下方式访问每个值:

String content = array[0];
String name = array[1];
...

这并不能解决整个问题(因为并不是所有的值都是由分号分隔的),但你可以根据传递给 split() 的参数来调整分隔你的值。

注意:如果你想将字符串解析为整数(例如长度值),你可以使用:

int length = Integer.parseInt(array[index]);
英文:

You can store and separate those values in an Array of Strings:

String[] array = "allyourinputtext".split(";");

This will separate the values after a semicolon. Then, you can access each value by doing this:

String content = array[0];
String name = array[1];
...

This doesn't solve the WHOLE problem(since not all values are separated by semicolons), but you can play with the arguments you pass to split() to separate your values.

Note: If you want to parse a String to int (the length value for example) you can use:

int length = Integer.parseInt(array[index];

答案3

得分: 0

// 我的情况:我知道我正在接收的多部分内容是一个类似这样的多值映射:

MultiValueMap<String, Resource>;

首先您需要找出边界

String contentTypeHeader = response.getHeaders().getFirst(HttpHeaders.CONTENT_TYPE);
String boundary = null;

if (contentTypeHeader != null) {
    MediaType mediaType = MediaType.parseMediaType(contentTypeHeader);
    boundary = mediaType.getParameter("boundary");
}

然后创建多部分流

MultipartStream multipartStream = new MultipartStream(
        new ByteArrayInputStream(body),
        boundary.getBytes(),
        1024,
        null);

从这个 MultipartStream 创建一个多值映射

boolean nextPart = multipartStream.skipPreamble();
while (nextPart) {

    ByteArrayOutputStream output = new ByteArrayOutputStream();
    // 读取当前部分的头信息。
    // 在 .readHeaders() 之后,流的位置将位于部分正文的开头,可以准备读取部分的实际内容。
    multipartStream.readHeaders();
    multipartStream.readBodyData(output);

    ByteArrayResource resource = new ByteArrayResource(output.toByteArray());
    multiValueMap.add("file", resource);
    nextPart = multipartStream.readBoundary();
}
return multiValueMap;
英文:

My situation: I knew the multipart that I'm receiving is a multivaluemap like this:

> MultiValueMap<String, Resource>

First of all, you need to figure out the boundary.

    String contentTypeHeader = response.getHeaders().getFirst(HttpHeaders.CONTENT_TYPE);
    String boundary = null;

    if (contentTypeHeader != null) {
        MediaType mediaType = MediaType.parseMediaType(contentTypeHeader);
        boundary = mediaType.getParameter(&quot;boundary&quot;);
    }

Then, make the multipart stream.

MultipartStream multipartStream = new MultipartStream(
            new ByteArrayInputStream(body),
            boundary.getBytes(),
            1024,
            null);

Make a multivaluemap from this multiPartStream

boolean nextPart = multipartStream.skipPreamble();
    while (nextPart) {

        ByteArrayOutputStream output = new ByteArrayOutputStream();
        // Read the headers of the current part.
        // After .readHeaders(), the stream&#39;s position will be at the start of the part&#39;s body, ready for you to read the part&#39;s actual content.
        multipartStream.readHeaders();
        multipartStream.readBodyData(output);

        ByteArrayResource resource = new ByteArrayResource(output.toByteArray());
        multiValueMap.add(&quot;file&quot;, resource);
        nextPart = multipartStream.readBoundary();
    }
    return multiValueMap;

huangapple
  • 本文由 发表于 2020年8月14日 21:37:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63413862.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定