网关服务器下载文件

  /**
     * 下载文件
     *
     * @param
     * @return
     */
    public void downLoad(HttpServletResponse hsr, String fileName, String uniquekey) {
        Map<String, String> map = new HashMap();
        map.put("uniquekey", uniquekey);
        CloseableHttpClient httpClient = HttpClients.createDefault();
        InputStream in = null;
        OutputStream out = null;
        //进行调用
        try {
            //设置请求时间
            RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000).
                    setConnectionRequestTimeout(35000).setSocketTimeout(60000).build();
            //构建一个build用来封装请求参数
            URIBuilder uriBuilder = new URIBuilder(downloadUrl);
            //设置参数
            setHttpGetParams(map, uriBuilder);
            //构建get请求
            HttpGet httpGet = new HttpGet(uriBuilder.build());
            httpGet.setConfig(requestConfig);
            httpGet.setHeader("DataEncoding", "UTF-8");
            CloseableHttpResponse response = httpClient.execute(httpGet);
            HttpEntity entity = response.getEntity();
            in = entity.getContent();
            //获取状态
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode != HttpStatus.SC_OK) {
                String content = EntityUtils.toString(entity, StandardCharsets.UTF_8);
            }
            hsr.setCharacterEncoding("utf-8");
            hsr.setContentType("application/msword");
            String name = "1111.doc";
            hsr.setHeader("Content-Disposition", "attachment;filename=" + java.net.URLEncoder.encode(name, "UTF-8"));
            //设置存储路径
            out = new BufferedOutputStream(hsr.getOutputStream());
            byte[] buffer = new byte[4096];
            int len = 0;
            while ((len = in.read(buffer)) > 0) {
                out.write(buffer, 0, len);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.flush();
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 设置参数
     *
     * @param paramMap
     * @param uriBuilder
     */
    private static void setHttpGetParams(Map<String, String> paramMap, URIBuilder uriBuilder) {
        if (!paramMap.isEmpty()) {
            List<NameValuePair> paramList = paramMap.entrySet().stream().map(entry
                    -> new BasicNameValuePair(entry.getKey(), entry.getValue())).collect(Collectors.toList());
            uriBuilder.addParameters(paramList);
        }
    }
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值