场景
直接展示外部系统返回的获取文件流时出现了跨域问题:
解决办法
1. 外部系统返回的请求头中调整(但是其他系统不会给你改的)
2. 我们系统后台获取文件流并转为新的文件流提供给前端
/** 获取传入url文件流 */
@GetMapping("/getFileStream")
public ResponseEntity<org.springframework.core.io.Resource> getFileStream(@RequestParam("url") String url,@RequestParam(value = "download", required = false) boolean download)throws UnsupportedEncodingException, BusinessException {// 返回流ByteArrayOutputStream outputStream = OkHttpClientUtil.get(url);if (null == outputStream) {throw new BusinessException("文件流为空", ServiceResponseStatus.SERVICE_ERROR);}String baseName = FilenameUtils.getBaseName(url);String extension = FilenameUtils.getExtension(url);String filename =URLEncoder.encode(baseName, "UTF-8") + FilenameUtils.EXTENSION_SEPARATOR + extension;MediaType mediaType = MediaType.APPLICATION_OCTET_STREAM;if (FxCommFileType.PDF.getSuffix().toLowerCase().equals(extension)) {mediaType = MediaType.APPLICATION_PDF;}StringBuilder headerValues = new StringBuilder();if (download) {headerValues.append("attachment;");}headerValues.append("filename=").append(filename);return ResponseEntity.ok().contentType(mediaType).header(HttpHeaders.CONTENT_DISPOSITION, headerValues.toString()).body(new ByteArrayResource(outputStream.toByteArray()));
}