最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

springboot實(shí)現(xiàn)返回文件流

 更新時間:2022年03月18日 09:10:59   作者:han1396735592  
這篇文章主要介紹了springboot實(shí)現(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返回二進(jìn)制文件流

    @GetMapping("/getTemplateFile")
    @ApiOperation("數(shù)據(jù)模板下載")
    public ResponseEntity<byte[]> downFile(HttpServletRequest request) throws IOException {
        File file = new File("C/AA");
        filename = getFilename(request, filename);
        //設(shè)置響應(yīng)頭
        HttpHeaders headers = new HttpHeaders();
        //通知瀏覽器以下載的方式打開文件
        headers.setContentDispositionFormData("attachment", filename);
        //定義以流的形式下載返回文件數(shù)據(jù)
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        //使用springmvc框架的ResponseEntity對象封裝返回?cái)?shù)據(jù)
        return new ResponseEntity<>(FileUtils.readFileToByteArray(file), headers, HttpStatus.OK);
    }

    /**
     * 根據(jù)瀏覽器的不同進(jìn)行編碼設(shè)置
     *
     * @param request  請求對象
     * @param filename 需要轉(zhuǎn)碼的文件名
     * @return 返回編碼后的文件名
     * @throws IOException
     */
    public String getFilename(HttpServletRequest request, String filename) throws IOException {

        //IE不同版本User-Agent中出現(xiàn)的關(guā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)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

宜黄县| 鲁山县| 云霄县| 沂南县| 始兴县| 连城县| 仙桃市| 洛川县| 巴里| 高平市| 广平县| 凤冈县| 南漳县| 双鸭山市| 株洲市| 大田县| 都安| 元阳县| 建德市| 两当县| 上林县| 高阳县| 耒阳市| 霍城县| 兰溪市| 皋兰县| 长垣县| 卓尼县| 扬中市| 津市市| 峨边| 浏阳市| 高青县| 治多县| 额敏县| 滨州市| 土默特右旗| 汉沽区| 盐源县| 元谋县| 巨鹿县|