// 路由注解可添加consumes参数指定Content-Type类型,如application/octet-stream
    @PostMapping(value = "/test")
    public void testBinary(HttpServletRequest request) throws IOException {
        File targetFile = new File("d:/test333.png");
        ServletInputStream inputStream = request.getInputStream();
        // org.apache.commons.io.FileUtils
        FileUtils.copyInputStreamToFile(inputStream, targetFile);
    }
    @PostMapping(value = "/test")
    public String testText(@RequestBody String body) {
        // 接收文本
        return body;
    }

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <body>
        <input type="file" id="file" />
        <button onclick="upload()">提交</button>
        <script>
            function upload() {
                var myHeaders = new Headers();
                myHeaders.append('Content-Type', 'image/jpeg');

                var file = document.getElementById('file').files[0];

                var requestOptions = {
                    method: 'POST',
                    headers: myHeaders,
                    body: file,
                    redirect: 'follow'
                };

                fetch('http://ty/rest/api/user/test', requestOptions)
                    .then(response => response.text())
                    .then(result => console.log(result))
                    .catch(error => console.log('error', error));
            }
        </script>
    </body>
</html>

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐