springboot實現(xiàn)返回文件流
更新時間:2022年03月18日 09:10:59 作者:han1396735592
這篇文章主要介紹了springboot實現(xiàn)返回文件流方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
springboot返回文件流
@GetMapping(value = "/file/{fileName}")
public ResponseEntity<FileSystemResource> getFile(@PathVariable("fileName") String fileName) throws FileNotFoundException {
File file = new File(filePath, fileName);
if (file.exists()) {
return export(file);
}
System.out.println(file);
return null;
}
public ResponseEntity<FileSystemResource> export(File file) {
if (file == null) {
return null;
}
HttpHeaders headers = new HttpHeaders();
headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
headers.add("Content-Disposition", "attachment; filename=" + file.getName());
headers.add("Pragma", "no-cache");
headers.add("Expires", "0");
headers.add("Last-Modified", new Date().toString());
headers.add("ETag", String.valueOf(System.currentTimeMillis()));
return ResponseEntity.ok().headers(headers).contentLength(file.length()).contentType(MediaType.parseMediaType("application/octet-stream")).body(new FileSystemResource(file));
}
springboot返回二進制文件流
@GetMapping("/getTemplateFile")
@ApiOperation("數(shù)據(jù)模板下載")
public ResponseEntity<byte[]> downFile(HttpServletRequest request) throws IOException {
File file = new File("C/AA");
filename = getFilename(request, filename);
//設置響應頭
HttpHeaders headers = new HttpHeaders();
//通知瀏覽器以下載的方式打開文件
headers.setContentDispositionFormData("attachment", filename);
//定義以流的形式下載返回文件數(shù)據(jù)
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
//使用springmvc框架的ResponseEntity對象封裝返回數(shù)據(jù)
return new ResponseEntity<>(FileUtils.readFileToByteArray(file), headers, HttpStatus.OK);
}
/**
* 根據(jù)瀏覽器的不同進行編碼設置
*
* @param request 請求對象
* @param filename 需要轉碼的文件名
* @return 返回編碼后的文件名
* @throws IOException
*/
public String getFilename(HttpServletRequest request, String filename) throws IOException {
//IE不同版本User-Agent中出現(xiàn)的關鍵詞
String[] IEBrowserKeyWords = {"MSIE", "Trident", "Edge"};
//獲取請求頭代理信息
String userAgent = request.getHeader("User-Agent");
for (String keyWord : IEBrowserKeyWords) {
if (userAgent.contains(keyWord)) {
//IE內(nèi)核瀏覽器,統(tǒng)一為utf-8編碼顯示
return URLEncoder.encode(filename, "UTF-8");
}
}
//火狐等其他瀏覽器統(tǒng)一為ISO-8859-1編碼顯示
return new String(filename.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
}
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
MyBatisPlus PaginationInterceptor分頁插件的使用詳解
這篇文章主要介紹了MyBatisPlus PaginationInterceptor分頁插件的使用詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-03-03
Elasticsearch Join字段類型簡單快速上手教程
這篇文章主要為大家介紹了Elasticsearch Join字段類型簡單快速上手教程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-09-09
Eclipse中導入Maven Web項目并配置其在Tomcat中運行圖文詳解
這篇文章主要介紹了Eclipse中導入Maven Web項目并配置其在Tomcat中運行圖文詳解,需要的朋友可以參考下2017-12-12
java生成csv文件亂碼的解決方法示例 java導出csv亂碼
這篇文章主要介紹了java生成csv文件亂碼的解決方法,大家可以直接看下面的示例2014-01-01
Java讀取resources目錄下文件路徑的九種代碼示例教程
在Java開發(fā)中經(jīng)常需要讀取項目中resources目錄下的文件或獲取資源路徑,這篇文章主要給大家介紹了關于Java讀取resources目錄下文件路徑的九種代碼示例教程,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2024-07-07
SpringCloud_Sleuth分布式鏈路請求跟蹤的示例代碼
Spring Cloud Sleuth是一款針對Spring Cloud的分布式跟蹤工具,本文通過實例代碼介紹了SpringCloud_Sleuth分布式鏈路請求跟蹤,感興趣的朋友跟隨小編一起看看吧2023-02-02
SpringBoot中mapper.xml文件存放的兩種實現(xiàn)位置
這篇文章主要介紹了SpringBoot中mapper.xml文件存放的兩種實現(xiàn)位置,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01

