restTemplate接受文件流

2023-01-10 07:55:14

目的

为了将pdf留存下来进行存档,使用的积木报表pdf接口返回的文件流,进行保存到本地。

积木报表版本

		<dependency>
			<groupId>org.jeecgframework.jimureport</groupId>
			<artifactId>jimureport-spring-boot-starter</artifactId>
			<version>1.5.0</version>
		</dependency>

代码

@GetMapping("/exportPdf")
	public void exportPdf(HttpServletResponse response){
		RestTemplate restTemplate = new RestTemplate();
		//创建url路径
		String url = "http://worsoft-gateway:9999/jimu/jmreport/exportPdfStream";
		Map<String,Object> map = new HashMap<>();
		JSONObject object = new JSONObject();
		//此处为示例,需要传递api和sql的正确参数值
		object.put("id","1514190139158454273");
		map.put("excelConfigId","697244136430743552");
		map.put("queryParam",object);
		HttpHeaders headers = new HttpHeaders();
		headers.add("Content-Type","application/json;charset=UTF-8");
		headers.add("Authorization", WebUtils.getRequest().getHeader("Authorization"));
		HttpEntity<Map<String,Object>> httpEntity = new HttpEntity<>(map,headers);
		try {
			Resource apiResult = restTemplate.postForObject(url, httpEntity, Resource.class);
			InputStream inputStream = apiResult.getInputStream();
			BufferedOutputStream out = FileUtil.getOutputStream("D:/test2.pdf");
			long copySize = IoUtil.copy(inputStream, out, IoUtil.DEFAULT_BUFFER_SIZE);
			IoUtil.close(inputStream);
			IoUtil.close(out);
		} catch (RestClientException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

功能特色

restTemplate接受文件流

心得记录

今天尝试了好多次,多多debug,有时候换种思路百度也可以,不要专牛角尖,比如昨天给同事安装mysql8.0,报错,内心抗拒命令安装,最后还是依靠命令安装成功,多看看,多学习。

  • 作者:山西白衣
  • 原文链接:https://blog.csdn.net/qq_44956333/article/details/125388421
    更新时间:2023-01-10 07:55:14