場景
直接展示外部系統(tǒng)返回的獲取文件流時(shí)出現(xiàn)了跨域問題:文章來源:http://www.zghlxwxcb.cn/news/detail-650501.html
解決辦法
1. 外部系統(tǒng)返回的請求頭中調(diào)整(但是其他系統(tǒng)不會(huì)給你改的)
文章來源地址http://www.zghlxwxcb.cn/news/detail-650501.html
2. 我們系統(tǒng)后臺(tái)獲取文件流并轉(zhuǎn)為新的文件流提供給前端
/** 獲取傳入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()));
}
到了這里,關(guān)于移動(dòng)端預(yù)覽指定鏈接的pdf文件流的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!