Call the third-party Api interface operation instructions and return the stream file (PDF file)

Instructions for calling the third-party Api interface and returning the stream file (PDF file)

first look at the demand

Requirements: Call third-party Api interface, Get request and Post request, and download face sheet (PDF format file)
Api interface description: Get request, Post request (request header and request parameters)

Instructions

The http request uses the HuTool tool class library.
If you don’t know this tool class yet, it’s okay, read on; (I’ll show you this is a favor)
If you don’t know it after reading it, it’s okay, please go to the official website to check;( If you read it, you won’t know it’s an accident)
If you read it, you will, it’s the sophistication of the world
;

Talent show

Two dependencies are required:
1. HuTool
2. Apache's commons-io (no file streaming or import)

<!--Hutool工具类-->
<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.7.3</version>
</dependency>


<!--Apache的commons-io-->
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.4</version>
</dependency>

Get request

url:第三方Api接口地址
//不带请求头			
String body = HttpUtil.createGet(url).execute().body();

-----------------------------------------------------------------------------------

//RequestHearder 请求头参数
Map<String, String> headers = new HashMap<>();
headers.put("TOKEN", token);

String body = HttpUtil.createGet(url).addHeaders(headers).execute().body();

-----------------------------------------------------------------------------------
返回结果为Json格式 这里是String类型 其实返回的是Json
如果要获取其中某个结果,如下:
String val =(JSON.parseObject(body).getString("code"));
String msg =(JSON.parseObject(body).getString("msg"));
String data =(JSON.parseObject(body).getString("data"));

如果结果里有反斜杠 需要进行反序列化 消除反斜杠
其实这个操作就是得到一个Object类型
Object parse = JSON.parse(data);

Post request

//RequestBody请求参数
JSONObject jsons = new JSONObject();
jsons.put("prdCode", Order.getPrdCode());

//RequestHearder 请求头参数
Map<String, String> headers = new HashMap<>();
headers.put("TOKEN", token);


带请求头和参数
String body = HttpUtil.createPost(Url).addHeaders(headers).body(jsons).execute().body();

--------------------------------------------------------------------------------------
所以看懂了吧 
如果要发送带请求头的 使用.addHeaders()
如果要发送body参数  使用.body()

File download for Post request

The focus here is to explain
this problem for a day,
and I also used the HttpUtils tool class to try, but the result was unsuccessful.

这里的请求参数是list 所以使用JSONArray 
//请求参数
JSONArray jsonArray = new JSONArray();
jsonArray.add(commodityCode);

// 请求头参数
Map<String, String> headers = new HashMap<>();
headers.put("TOKEN", token);

这里是重点 细心的人会发现这次没有去.body() 因为我们要获取的是流 不是json串;
HttpResponse execute = HttpUtil.createPost(url).addHeaders(headers).body(jsonArray).execute();

//设置页面不缓存 这一步一定要在流的写入前进行操作 不然程序会报错
response.reset();

InputStream inputStream = null;
try {
    
    
	//设置输出的文件名
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
    String format = sdf.format(new Date());
    String fileName = format+".pdf";

	//获取输入流
    inputStream = execute.bodyStream();
    
    //设置文件名(这一步要在流的写入前完成 否则设置不生效)
    response.setHeader("Content-Disposition", "attachment;filename="+fileName);  
          
	//输出流
    OutputStream output = response.getOutputStream();
            
    //这里使用了Apache的commons-io
    //IOUtils.copy(输入流,输出流) copy这个流就ok
    IOUtils.copy(inputStream,output);

	//这里要设置application为pdf格式
    response.setContentType("application/pdf;charset=UTF-8");
    }catch (Exception e) {
    
    
            log.error(e.getMessage());
            log.error("****************面单下载失败****************");
    }finally {
    
    
    		//关闭输入流  输出流可以不用关闭 不归咱们管
            inputStream.close();
    }

Summarize


It's over when I see this,
I don't understand the rest

Guess you like

Origin blog.csdn.net/qq_42990433/article/details/118611618