当前位置:网站首页>【FeignClient】feignClient跨服务下载文件
【FeignClient】feignClient跨服务下载文件
2022-04-21 21:32:00 【卜居易】
背景:服务端提供了一个下载服务,用FeignClient调用该服务时发生错误。
错误信息为:getOutputStream() has already been called for this response
此错误原因是response已经被调用
解决方法:需要用到Feign提供的Request来做一次中继操作
代码:
服务端:
/**
* 下载文件
* @param filePath 文件路径
* @param downloadName 下载后的文件名(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);
}
客户端:
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("失败了");
}finally {
inputStream.close();
outputStream.close();
}
}
参考文章:
1,https://www.jb51.net/article/160275.htm
2,FeignClient 跨服务上传文件、导出Excel_赵丙双的博客-CSDN博客_feign 导出excel
3,Feign跨服务下载文件; getOutputStream() has already been called for this response_在你之后的博客-CSDN博客
版权声明
本文为[卜居易]所创,转载请带上原文链接,感谢
https://blog.csdn.net/u013517229/article/details/124326943
边栏推荐
- Unity tips
- User manual for visual view of data source of easybi reporting system
- Behind flight safety, the indispensable wireless communication sensor equipment of Internet of things
- 841. 字符串哈希 (字符串哈希模板)
- 可视化图表告诉你,《人世间》《余生,请多指教》到底爆没爆?
- 12 SQL tips you may not know
- 微服务,中台,RPA和低代码火热背后的一些冷思考
- 奇安信冬奥“零事故”终端安全分享会:成功解决6641次攻击事件
- AI应用说-生产制造专场(配件安装质检)
- (valid for personal test) Oracle can't use backspace key to backspace the command line under Linux, and can't use up and down keys to switch the historical command. There will be garbled code
猜你喜欢

Neural network learning small record 56 -- the principle and function of batch normalization layer

Keil Package常用芯片高速下载地址,含历史版本,快关注我速速收藏

还清90万安居贷才能拿到离职证明?腾讯回应:与实际情况不符

憨批的语义分割重制版6——Pytorch 搭建自己的Unet语义分割平台

Common SQL optimization

【迷人的爪哇】——数据类型和变量

Operation instructions of training management system

#Reflex WMS研习心得#上架原理

Others - interface test by postman

Tgip-cn 038 registration | in depth analysis of Apache pulsar source code reading correct posture (I)
随机推荐
JVM 从入门到放弃之 ZGC 垃圾收集器精讲
分析师认为三星Galaxy Z Fold 4和Z Flip 4可能比其前代产品更便宜
Common data type conversion methods in crypto
Blog recommendation | explain the new feature of Apache pulsar 2.10 - automatic cluster failover
Sword finger offer 15 Number of 1 in binary
[SQL] sql32 sets the last of all employees in the employees table_ Name and first_ Name splicing
Red sun shooting range -- intranet penetration practice
Redis+Caffeine两级缓存,让访问速度纵享丝滑
尿素期货怎么做才安全?尿素期货套期保值会有什么好处?
Various DNS: Baidu DNS / Alibaba DNS / 114dns / Tencent DNS / Google DNS / OpenDNS comparison evaluation
AI+大健康, 是超能机器人的正确打开方式?
Jmeter(十六) - 从入门到精通 - JMeter前置处理器(详解教程)
Anshui Cup 2021 Crypto Réapparition
32-bit and 64 bit computers
【SeMask】Semantically Masked Transformers for Semantic Segmentation
Other - use of Supervisor
841. String hash (string hash template)
02.GCC编译器的使用
模块三作业 架构设计文档
清洁机器人--红外下视传感器,检测是否存在地面距离突变的情形