FeignClient调用内部服务下载文件错误打开方式
2023-12-22 11:56:34
文件服务器提供的接口
controller层
/**
* 绝对路径-文件下载
*/
@GetMapping("/absolutePathDownload")
public void absolutePathDownload(String fileName, HttpServletResponse response) {
sysFileService.absolutePathDownload(fileName, response);
}
service接口层?
/**
* 绝对路径-文件下载
*
* @param fileName 文件路径名称
* @param response 返回值
*/
void absolutePathDownload(String fileName, HttpServletResponse response);
impl实现层
@Override
public void absolutePathDownload(String fileName, HttpServletResponse response) {
InputStream inputStream = null;
OutputStream outputStream = null;
try {
outputStream = response.getOutputStream();
// 获取文件对象
inputStream = XXX
byte buf[] = new byte[1024];
int length = 0;
response.reset();
response.setHeader("Content-Disposition", "attachment;filename=" +
URLEncoder.encode(fileName.substring(fileName.lastIndexOf("/") + 1), "UTF-8"));
response.setContentType("application/octet-stream");
response.setCharacterEncoding("UTF-8");
// 输出文件
while ((length = inputStream.read(buf)) > 0) {
outputStream.write(buf, 0, length);
}
logger.info("下载成功");
inputStream.close();
} catch (Throwable ex) {
logger.error("下载文件失败:{}", ex);
response.setHeader("Content-type", "text/html;charset=UTF-8");
String data = "文件下载失败";
try {
OutputStream ps = response.getOutputStream();
ps.write(data.getBytes("UTF-8"));
} catch (IOException e) {
e.printStackTrace();
}
} finally {
try {
outputStream.close();
if (inputStream != null) {
inputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
FeignClient接口错误定义
/**
* 绝对路径-下载文件
*
* @param
* @return 结果
*/
@GetMapping(value = "/absolutePathDownload")
void absolutePathDownload(@RequestParam("fileName") String fileName, @RequestHeader("response") HttpServletResponse response);
错误调用
@PostMapping("/download")
public void download(@RequestBody ChuanId chuanId, HttpServletResponse response) {
if (chuanId != null && StringUtils.isNotEmpty(chuanId.getId())) {
QualityRequire qualityRequire = qualityRequireService.selectQualityRequireByDemandNo(chuanId.getId());
String downloadPath = qualityRequire.getAnnexPath();
String replace = downloadPath.replace(domain + prefix, path);
fileService.absolutePathDownload(replace, response);
}
}
这种调用是得不到下载数据的,但也不会报错
文章来源:https://blog.csdn.net/m18030221734_1/article/details/135135802
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!