文件服务器提供的接口
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);
}
}
这种调用是得不到下载数据的,但也不会报错