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

Springboot如何通過流返回文件

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

如何通過流返回文件

本人的文件是放在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è)置擴展頭,當(dāng)Content-Type 的類型為要下載的類型時 , 這個信息頭會告訴瀏覽器這個文件的名字和類型。
        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();
    }
}

注意點

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

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

以流的方式直接返回

import java.io.FileReader;
import java.io.InputStream;
import java.util.Properties;
/*
 */
public class Reflect {
    public static void main(String[] args) throws Exception{
       //獲取一個文件的絕對路徑!??!
        // 這種是先獲得絕對路徑然后在轉(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);
    }
}

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

相關(guān)文章

最新評論

兴仁县| 五华县| 宾川县| 重庆市| 吉林省| 峨眉山市| 西乡县| 石柱| 兴文县| 惠州市| 海阳市| 江达县| 江源县| 金山区| 远安县| 田林县| 汝阳县| 鹤庆县| 沁水县| 葫芦岛市| 集安市| 特克斯县| 阿城市| 襄垣县| 屏东县| 余江县| 邵阳市| 汉寿县| 建瓯市| 永靖县| 新巴尔虎左旗| 肥东县| 科尔| 遵义县| 北辰区| 简阳市| 咸宁市| 宜城市| 东兴市| 平乡县| 济源市|