java—如何防止resttemplate为多部分/表单数据添加头

jhkqcmku  于 2021-07-24  发布在  Java
关注(0)|答案(0)|浏览(264)

我正在使用spring的restemplate将我的请求发布到一些api。我需要发送一个请求参数和文件。但api不接受带有“content-type:text/plain”参数的请求。我试着用拦截器这个解决方案:另一个答案
它工作,api接收文件,但当我用拦截器清除请求时,文件中断。例如,它是15406字节,“清理”后是25772字节。
标准要求是:

MIME Multipart Media Encapsulation, Type: multipart/form-data, Boundary: "bT6YPPUZZOp18RN7eJAGvjHEULLi7mqP"
    [Type: multipart/form-data]
    First boundary: --bT6YPPUZZOp18RN7eJAGvjHEULLi7mqP\r\n
    Encapsulated multipart part:  (text/plain)
        Content-Disposition: form-data; name="api"\r\n
        Content-Type: text/plain;charset=UTF-8\r\n
        Content-Length: 25\r\n\r\n
        Line-based text data: text/plain (1 lines)
            API_NAME
    Boundary: \r\n--bT6YPPUZZOp18RN7eJAGvjHEULLi7mqP\r\n
    Encapsulated multipart part:  (text/plain)
        Content-Disposition: form-data; name="version"\r\n
        Content-Type: text/plain;charset=UTF-8\r\n
        Content-Length: 1\r\n\r\n
        Line-based text data: text/plain (1 lines)
            3
    Boundary: \r\n--bT6YPPUZZOp18RN7eJAGvjHEULLi7mqP\r\n
    Encapsulated multipart part:  (text/plain)
        Content-Disposition: form-data; name="method"\r\n
        Content-Type: text/plain;charset=UTF-8\r\n
        Content-Length: 6\r\n\r\n
        Line-based text data: text/plain (1 lines)
            create
    Boundary: \r\n--bT6YPPUZZOp18RN7eJAGvjHEULLi7mqP\r\n
    Encapsulated multipart part:  (text/plain)
        Content-Disposition: form-data; name="_sid"\r\n
        Content-Type: text/plain;charset=UTF-8\r\n
        Content-Length: 86\r\n\r\n
        Line-based text data: text/plain (1 lines)
            SID
    Boundary: \r\n--bT6YPPUZZOp18RN7eJAGvjHEULLi7mqP\r\n
    Encapsulated multipart part:  (application/x-bittorrent)
        Content-Disposition: form-data; name="file"; filename="test.torrent"\r\n
        Content-Type: application/x-bittorrent\r\n
        Content-Length: 15406\r\n\r\n
        Media Type
            Media type: application/x-bittorrent (15406 bytes)
    Last boundary: \r\n--bT6YPPUZZOp18RN7eJAGvjHEULLi7mqP--\r\n

我需要这样的东西:

MIME Multipart Media Encapsulation, Type: multipart/form-data, Boundary: "--bT6YPPUZZOp18RN7eJAGvjHEULLi7mqP"
    [Type: multipart/form-data]
    First boundary: --bT6YPPUZZOp18RN7eJAGvjHEULLi7mqP\r\n
    Encapsulated multipart part: 
        Content-Disposition: form-data; name="api"\r\n\r\n
        Data (25 bytes)
            Data: 53594e4f2e446f776e6c6f616453746174696f6e2e5461736b
            [Length: 25]
    Boundary: \r\n--bT6YPPUZZOp18RN7eJAGvjHEULLi7mqP\r\n
    Encapsulated multipart part: 
        Content-Disposition: form-data; name="version"\r\n\r\n
        Data (1 byte)
            Data: 33
            [Length: 1]
    Boundary: \r\n--bT6YPPUZZOp18RN7eJAGvjHEULLi7mqP\r\n
    Encapsulated multipart part: 
        Content-Disposition: form-data; name="method"\r\n\r\n
        Data (6 bytes)
            Data: 637265617465
            [Length: 6]
    Boundary: \r\n--bT6YPPUZZOp18RN7eJAGvjHEULLi7mqP\r\n
    Encapsulated multipart part: 
        Content-Disposition: form-data; name="_sid"\r\n\r\n
        Data (86 bytes)
            Data: 58496f4179574552676632354166324c576e7565587338484279476b2d575644376a2d70…
            [Length: 86]
    Boundary: \r\n--bT6YPPUZZOp18RN7eJAGvjHEULLi7mqP\r\n
    Encapsulated multipart part:  (application/x-bittorrent)
         [truncated]Content-Disposition: form-data; name="file"; filename="test.torrent"
        Content-Type: application/x-bittorrent\r\n\r\n
        Media Type
            Media type: application/x-bittorrent (15406 bytes)
    Last boundary: \r\n--bT6YPPUZZOp18RN7eJAGvjHEULLi7mqP\r\n

如何清除多值Map中的每个标题?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题