java通过httpclient携带请求头参数获取第三方文件流接口并实现实现文件下载

创建httpclient 请求,并在header携带指定key,也可根据实际需要携带token等信息。获取第三方接口返回的文件输入流并写到本地response中,实现返回文件流,前端通过js的a标签进行下载。

代码如下:

@Override
    public void YKDownload(String fileId, HttpServletResponse response, HttpServletRequest request) {
        logger.info("下载盈科附件, 参数:附件id = " + fileId);
        // 创建默认的httpClient实例.
        CloseableHttpClient httpclient = HttpClients.createDefault();
        // 创建httpGet
        HttpGet httpGet = new HttpGet("xxxxx" + fileId);
        try {
			// 设置请求头参数
            httpGet.setHeader("xxxx-Key", "xxxxxx");
            CloseableHttpResponse httpResponse = httpclient.execute(httpGet);
			// 获取第三方接口放在Response中的文件名
            Header[] headers = httpResponse.getHeaders("content-disposition");
            if (headers != null && headers.length > 0) {
                String contentDispositionValue = headers[0].getValue();
				// 设置文件名,这里没用加密文件名,可以自己设置
                response.addHeader("Content-Disposition", contentDispositionValue);
            }
            response.setContentType("application/octet-stream;charset=utf-8");
            HttpEntity entity = httpResponse.getEntity();
            InputStream inputStream = entity.getContent(); //获取输入流
			
            //  保存流 从这里到重置游标
			//  附件第一次下载没问题再下载有时候就报错,然后参照其他博主的方法,将流先保存下来
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            BufferedInputStream br = new BufferedInputStream(inputStream);
            byte[] b = new byte[1024];
            for (int c = 0; (c = br.read(b)) != -1;)
            {
                bos.write(b, 0, c);
            }
            b = null;
            br.close();
            inputStream = new ByteArrayInputStream(bos.toByteArray());
            // 第一次读流
            StringBuffer out = new StringBuffer();
            byte[] b1 = new byte[1024];
            for (int n; (n = inputStream.read(b1)) != -1;) {
                out.append(new String(b1, 0, n));  //这个可以用来读取文件内容 并且文件内容有中文读取出来也不会乱码
            }
            // 判断文件是否存在
            String resultHtml = out.toString();
            int firstIndex = resultHtml.indexOf("\n");
            if(firstIndex < 0){
                logger.info("文件不存在或异常"+resultHtml);
            }
            // 重置游标
            inputStream.reset();


            // 输出文件
            ServletOutputStream outputStream = response.getOutputStream();
            try {
                byte[] oBuff = new byte[1024];
                int iSize;
                while (-1 != (iSize = inputStream.read(oBuff))) {
                    outputStream.write(oBuff, 0, iSize);
                }
                outputStream.flush();
            } finally {
                IOUtils.close(outputStream);
                IOUtils.close(inputStream);
            }
            outputStream.close();
        } catch (Exception e) {
            logger.info("下载盈科附件, 原因:" + e.getMessage());
            e.printStackTrace();
        } finally {
            try {
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

前端js代码

function fileYKDownload(fileId, fileName) {
    //创建XMLHttpRequest对象
    var httpRequest = new XMLHttpRequest();
    //打开连接,将请求参数拼在url后面
    httpRequest.open('GET', __baseUrl + '/v3/query/accessories/download?fileId=' + fileId, true);
    //设置期望的返回值类型
    httpRequest.responseType = "blob";
    //设置请求头,将认证信息放入请求头中
    httpRequest.setRequestHeader("PRIVATETOKEN",$.getCookie(__token));

    //请求成功回调函数
    httpRequest.onload = function (oEvent) {
        if (httpRequest.status === 200) {
			// 这里文件名可以从response中获取,为了方便我直接js方法传的文件名
            // var fileName = decodeURI(httpRequest.getResponseHeader("Content-Disposition"));
            console.log(fileName);
            var response = httpRequest.response;

            //数据转换为文件下载
            var elink = document.createElement('a');
            elink.download = fileName;
            elink.style.display = 'none';
            var blob = new Blob([response]);
            elink.href = URL.createObjectURL(blob);
            document.body.appendChild(elink);
            elink.click();
            document.body.removeChild(elink);
        }
    }
    //发送请求
    httpRequest.send();
}

你可能感兴趣的:(java,开发语言)