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

Springboot如何通過流返回文件

 更新時(shí)間:2022年03月18日 09:43:49   作者:一個(gè)想努力學(xué)技術(shù)的程序員  
這篇文章主要介紹了Springboot如何通過流返回文件,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

如何通過流返回文件

本人的文件是放在resource/templates目錄下,截圖如下

controller類如下

    @GetMapping(value = "/downfile")
    public void download(HttpServletResponse response) throws IOException {
        String fileName = "template.xlsx";
        // 設(shè)置信息給客戶端不解析
        String type = new MimetypesFileTypeMap().getContentType(fileName);
        // 設(shè)置contenttype,即告訴客戶端所發(fā)送的數(shù)據(jù)屬于什么類型
        response.setHeader("Content-type",type);
        // 設(shè)置編碼
        String code = new String(fileName.getBytes("utf-8"), "iso-8859-1");
        // 設(shè)置擴(kuò)展頭,當(dāng)Content-Type 的類型為要下載的類型時(shí) , 這個(gè)信息頭會(huì)告訴瀏覽器這個(gè)文件的名字和類型。
        response.setHeader("Content-Disposition", "attachment;filename=" + code);
        response.setContentType("application/octet-stream;charset=ISO8859-1");
        response.addHeader("Pargam", "no-cache");
        response.addHeader("Cache-Control", "no-cache");
        DownLoadUtils.download(fileName, response);
    }

工具類DownLoadUtils如下

public class DownLoadUtils { 
    public static void download(String filename, HttpServletResponse res) throws IOException {
        // 發(fā)送給客戶端的數(shù)據(jù)
        // 讀取filename
        ClassPathResource classPathResource = new ClassPathResource("templates/"+filename);
        long length = classPathResource.getFile().length();
        res.addHeader("Content-Length",String.valueOf(length));
        OutputStream outputStream = res.getOutputStream();
        byte[] buff = new byte[1024];
        BufferedInputStream bis = null;
        InputStream inputStream =classPathResource.getInputStream();
        bis = new BufferedInputStream(inputStream);
        int i = bis.read(buff);
        while (i != -1) {
            outputStream.write(buff, 0, buff.length);
            outputStream.flush();
            i = bis.read(buff);
        }
        bis.close();
        outputStream.close();
    }
}

注意點(diǎn)

response.addHeader("Content-Length",String.valueOf(file.length()));

如果不加這句代碼,下載下來的文件會(huì) 在打開前提示修復(fù),文件格式或文件擴(kuò)展名無效。請(qǐng)確定文件未損壞,并且文件擴(kuò)展名與文件的格式匹配

以流的方式直接返回

import java.io.FileReader;
import java.io.InputStream;
import java.util.Properties;
/*
 */
public class Reflect {
    public static void main(String[] args) throws Exception{
       //獲取一個(gè)文件的絕對(duì)路徑?。?!
        // 這種是先獲得絕對(duì)路徑然后在轉(zhuǎn)換成流。
//        String path = Thread.currentThread().getContextClassLoader()
//                .getResource("classinfo2.properties").getPath();
//        FileReader reader = new FileReader(path);
        //下面這種是直接用流的方式返回。
        InputStream reader = Thread.currentThread().getContextClassLoader()
                .getResourceAsStream("classinfo2.properties");
        Properties pro = new Properties();
        pro.load(reader);
        reader.close();
        //通過key獲取value。
        String className = pro.getProperty("className");
        System.out.println(className);
    }
}

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。 

相關(guān)文章

最新評(píng)論

伊川县| 彭阳县| 仁怀市| 桂阳县| 志丹县| 武乡县| 体育| 诸暨市| 如东县| 灵石县| 麦盖提县| 温州市| 涡阳县| 金门县| 公主岭市| 安岳县| 社旗县| 辛集市| 南华县| 隆安县| 阿尔山市| 寿宁县| 拉萨市| 桑日县| 南投市| 仁怀市| 淳安县| 石渠县| 东至县| 平果县| 康马县| 大丰市| 蓬溪县| 东丽区| 淮滨县| 年辖:市辖区| 蕲春县| 揭阳市| 丰原市| 余江县| 甘洛县|