当前位置:网站首页>[feignclient] feignclient downloads files across services
[feignclient] feignclient downloads files across services
2022-04-21 22:37:00 【Bu Juyi】
background : The server provides a download service , use FeignClient An error occurred while calling the service .
The error message is :getOutputStream() has already been called for this response
The reason for this error is response Already called
resolvent : Need to use Feign Provided Request To do a relay operation
Code :
Server side :
/**
* Download the file
* @param filePath File path
* @param downloadName The name of the downloaded file (1.txt)
* @param response
*/
public static void downloadFile(String filePath, String downloadName, HttpServletResponse response) throws UnsupportedEncodingException {
response.reset();
response.setHeader("Content-Disposition", "attachment;filename="+ new String((downloadName).getBytes(), "iso-8859-1"));
response.setContentType("text/plain;charset=utf-8");
File file = new File(filePath);
InputStream in = null;
if(file.exists()){
try {
OutputStream out = response.getOutputStream();
in = new FileInputStream(file);
byte buffer[] = new byte[1024];
int length = 0;
while ((length = in.read(buffer)) >= 0){
out.write(buffer,0,length);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(in != null){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
@GetMapping("download")
public void downloadYjya(HttpServletResponse response) throws IOException {
downloadFile(?, ?, response);
}
client :
public interface RtYjClient {
@GetMapping("download")
Response downloadYjya();
}
@GetMapping("download")
public void downloadYjya(HttpServletResponse servletResponse) throws IOException {
Response response = rtYjClient.downloadYjya();
Response.Body body = response.body();
for (Object key : response.headers().keySet()) {
List<String> kList = (List<String>) response.headers().get(key);
for (String val : kList) {
servletResponse.setHeader(key.toString(), val);
}
}
InputStream inputStream = null;
OutputStream outputStream = null;
try {
inputStream = body.asInputStream();
outputStream = servletResponse.getOutputStream();
byte[] b = new byte[inputStream.available()];
inputStream.read(b);
outputStream.write(b);
outputStream.flush();
} catch (IOException e) {
System.out.println(" failed ");
}finally {
inputStream.close();
outputStream.close();
}
}
Reference article :
版权声明
本文为[Bu Juyi]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204212132215702.html
边栏推荐
- 君禾股份:2021年度营收增长稳健,受益产品出口业绩再创新高
- 2022 Intermediate Accounting Title Financial Management exercises and answers
- CC00012.ZABBIX———————————————
- D:MATLAB.N个实用技巧-MATLAB中文论坛精华总结
- mysql事务和隔离级别底层原理浅析
- 专精特新代办机构及专精特新代办政策分析解读,补贴20-100万
- L1-058 6 turned over (15 points)
- [RL] deeply understand the use of gradient descent in tabular learning (MC / TD)
- [Central South University of forestry science and technology] [Chen] sixth week innovation question prime
- PyQt5+OpenCV操作本地摄像头
猜你喜欢

Exercise questions and answers of basic theories and relevant laws and regulations in 2022 supervision engineer examination

Review questions and answers of building materials and structures in 2022 first-class registered architect examination

Wechat applet custom tabbar

【pytorch】常用函数

CC00012. MySQL———————————————

Logic control (example explanation)

We sincerely invite you to sign up for the first openharmony developer growth plan sharing day

CC00000. ZABBIX———————————————

Module 3: Outsourcing student management system - architecture design document

Unity3d import tilt model, etc
随机推荐
L1-055 who is the winner (10 points)
CV战神常用代码-----kj15
2022 intermediate accounting title examination economic law practice questions and answers
Get user information - wechat applet
UVM First Steps with UVM - Register Layer
OS实验三【进程通信】
Industry analysis | development of Internet medicine
[Central South University of forestry science and technology] [Chen] week 7 innovation maze
2022 Chongqing's latest architectural eight members (Civil Engineering) simulation question bank and answers
It is revealed that Xiaomi has new machines this month, and many of its products are ready to go
L1-059 ringing stupid bell (20 minutes)
UVM First Steps with UVM - Register Layer
D:MATLAB.N个实用技巧-MATLAB中文论坛精华总结
Point cloud bilateral filtering
Fundamentals of Power Electronics
将模型训练外包真的安全吗?新研究:外包商可能植入后门,控制银行放款
读书笔记《秋园》《浮木》《我本芬芳》
OpenCV中的Core组件——输入输出XML, YAML(12)
Start debugging the dynamic linker of C Library-2 -- musl-c
Oracle database 22c insight:_ kgl_ Large_ heap_ assert_ Threshold automatic and manual adjustment