如何从Spring处理的POST请求中获取原始二进制数据? [英] How to get raw binary data from a POST request processed by Spring?

查看:79
本文介绍了如何从Spring处理的POST请求中获取原始二进制数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个能够处理CUrl发送的二进制数据的应用程序,例如:

I need to write an application which would be able to process binary data sent by CUrl, such as:

curl localhost:8080/data --data-binary @ZYSF15A46K1.txt

我创建了一种POST处理方法,如下所示:

I've created a POST processing method as follows:

@RequestMapping(method = RequestMethod.POST, value = "/data")
    public void acceptData(HttpEntity<byte[]> requestEntity) throws Exception {
        process(requestEntity.getBody());
    }

但是,它似乎没有返回原始二进制数据.我已经尝试发送GZip文件,并且在经历了Spring之后,它现在可解压缩的时间更长了,这使我相信我获取的数据过多或过少.

However it doesn't seem to be returning raw binary data. I've tried sending a GZip file and after going through Spring it is now longer decompressible, which leads me to believe I'm either getting too much data or too little data.

如何解决此问题并获取原始二进制数据?

How do I solve this issue and get raw binary data?

推荐答案

就像在控制器方法的参数中声明InputStream一样简单:

It's as easy as declaring an InputStream in your controller method's parameters:

@RequestMapping(method = RequestMethod.POST, value = "/data")
public void acceptData(InputStream dataStream) throws Exception {
    processText(dataStream);
}

您不需要禁用HiddenHttpMethodFilter,如果这样做,则可能是您的请求在某种程度上是错误的.参见 https://github.com/spring-projects/spring-boot/issues/5676 .

You shouldn't need to disable HiddenHttpMethodFilter, if you do it's probably that your request is wrong in some way. See https://github.com/spring-projects/spring-boot/issues/5676.

这篇关于如何从Spring处理的POST请求中获取原始二进制数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆