美文网首页
restTemplate请求返回文件

restTemplate请求返回文件

作者: 何佳阳 | 来源:发表于2020-02-10 10:24 被阅读0次

    public void getShopDataExportNoReturn(JSONObject param, HttpServletResponse httpResponse) {

    InputStream inputStream =null;

            OutputStream outputStream =null;

            try {

    String url ="http://url?customUsername=" + param.getString("customUsername") +"&date=" + param.getString("date");

                HttpHeaders headers =new HttpHeaders();

                List mediaTypeList =new ArrayList<>();

                mediaTypeList.add(MediaType.APPLICATION_OCTET_STREAM);

                headers.setAccept(mediaTypeList);

                HttpEntity httpEntity =new HttpEntity(null, headers);

                ResponseEntity response =restTemplate.exchange(url, HttpMethod.GET, httpEntity, byte[].class);

                byte[] result = response.getBody();

                inputStream =new ByteArrayInputStream(result);

                outputStream = httpResponse.getOutputStream();

                int len =0;

                byte[] buf =new byte[1024];

                while ((len = inputStream.read(buf, 0, 1024)) != -1) {

    outputStream.write(buf, 0, len);

                }

    outputStream.flush();

            }catch (Exception ex) {

    log.error("downloadFile error!", ex);

            }finally {

    try {

    if (inputStream !=null) {

    inputStream.close();

                    }

    if (outputStream !=null) {

    outputStream.close();

                    }

    }catch (Exception ex) {

    }

    }

        }

    相关文章

      网友评论

          本文标题:restTemplate请求返回文件

          本文链接:https://www.haomeiwen.com/subject/fitaxhtx.html