a标签通过别人的接口,下载流文件

该文章描述了一个Java控制器方法,用于处理从远程服务器通过HTTP请求获取文件流,然后转换为字节数组并发送回客户端作为文件下载。主要涉及的技术包括HTTP请求、文件流处理和响应头设置。
摘要由CSDN通过智能技术生成

HTML 标签

'<a href="/utils/fileStream/exportKeHuFaPiaoZhangDan?fileId=666">' 

JAVA Controller

	/**
     * 导出其他服务的文件
     */
	@RequestMapping(value = "/export")
    @ResponseBody
    public void export(HttpServletRequest request, HttpServletResponse response, String fileId) {
        try {
            InputStream is = getInputStream(kehuApiConfig.getUrl() , fileId); // 获取其他服务器文件流
            
            /* 流文件转字节 */
            ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
            byte[] buff = new byte[100]; //buff用于存放循环读取的临时数据
            int rc = 0;
            while ((rc = is.read(buff, 0, 100)) > 0) {
            	swapStream.write(buff, 0, rc);
            }
            byte[] in_b = swapStream.toByteArray();
            
            is.close(); // 关闭输入流
            swapStream.close();
            
            /* 浏览器响应 */
            response.reset();
            response.addHeader("Content-Disposition", "attachment;filename="+ fileId +".xlsx" ); // 文件名
//            response.addHeader("Content-Length", "" + file.length()); // 文件长度
            OutputStream os = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");
            os.write(in_b);
            os.flush();
            os.close(); // 关闭输出流
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        
    }

    /**
     * 组装请求参数
     * @return
     */
    public static InputStream getInputStream(String fileUrl, String fileId) {
    	/* 组装参数 */
        JSONObject paramJson = new JSONObject();
        paramJson.put("fileId", fileId);
		return sendPostByRawRetInputStream(fileUrl, paramJson.toJSONString(),  ContentType.APPLICATION_JSON); // 请求
    }
	
    /**
	 * 向指定 URL 发送POST方法的请求 ByRaw
	 *
	 * @param url
	 *            发送请求的 URL
	 * @param param 请求参数
	 * 	JSONObject param = new JSONObject();
     * 	   param.put("ids", ids);
	 * 	   param.toJSONString()
	 * @return 所代表远程资源的响应结果
	 */
	public static InputStream sendPostByRawRetInputStream(String url, String param, ContentType contentType) {
	    CloseableHttpClient httpClient = null;
	    HttpPost httpPost = null;
	    InputStream inputStream = null;
	    try {
	        httpClient = HttpClients.createDefault();
	        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(8000).setConnectTimeout(8000).build();
	        httpPost = new HttpPost(url);
	        httpPost.setConfig(requestConfig);
	        httpPost.setEntity(new StringEntity(param, contentType));
	        CloseableHttpResponse response = httpClient.execute(httpPost);
	        HttpEntity temp = null;
	        HttpEntity entity = response.getEntity();
	        temp = entity;
	        inputStream = temp.getContent();
	        
	    } catch (Exception e) {
	        throw new BusinessException(e.getMessage());
	    }
	    
	    return inputStream;
	}
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值