欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java下載項(xiàng)目中靜態(tài)文件方式

 更新時(shí)間:2023年08月14日 16:33:44   作者:竹秋千道  
這篇文章主要介紹了Java下載項(xiàng)目中靜態(tài)文件方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

Java下載項(xiàng)目中靜態(tài)文件

廢話不多說,直接上代碼,拷貝即可用~~~

項(xiàng)目結(jié)構(gòu)

下載工具類

/**
 * @program: myutil
 * @description: 從本地項(xiàng)目(本地磁盤上)下載靜態(tài)文件
 * @author: lsy
 * @create: 2020-08-13 16:58
 **/
public class LocalFileUtils {
    /**
     * @param response
     * @param fileName
     * @description 根據(jù)指定項(xiàng)目路徑下的某個(gè)excel, 下載文件
     */
    public static void exportFile(HttpServletResponse response, String fileName) {
    	// 第一種獲取靜態(tài)資源
    	ClassPathResource classPathResource = new ClassPathResource("static/excleTemplate/" + fileName);// "static/excleTemplate/ImportModel.xlsx"
    	// 第二種獲取靜態(tài)資源
        // InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("static/excleTemplate/" + fileName);
        // 第三種獲取靜態(tài)資源
        // InputStream inputStream = this.getClass().getResourceAsStream("static/excleTemplate/" + fileName);
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            inputStream = classPathResource.getInputStream();
            outputStream = response.getOutputStream();
            int BUFFER_SIZE = 1024 * 4;
            byte[] buffer = new byte[BUFFER_SIZE];
            int reader = 0;
            while ((reader = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, reader);
            }
            response.setContentType("application/octet-stream");
            response.setCharacterEncoding("utf-8");
            String newFileName = URLEncoder.encode(classPathResource.getFilename(), "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + newFileName);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (outputStream != null) {
                    /**flush():僅僅刷新緩沖區(qū)(一般寫字符時(shí)要用,因?yàn)樽址麜r(shí)先進(jìn)入緩沖區(qū)),然后將內(nèi)存中的數(shù)據(jù)立刻寫出(因?yàn)榫彌_區(qū)是裝滿之后才會(huì)寫出
                     ,用flush()就不必等到緩沖區(qū)滿,立刻寫出,流對(duì)象還可以繼續(xù)使用) */
                    outputStream.flush();
                    /**close():關(guān)閉流對(duì)象. 也會(huì)先刷新一次緩沖區(qū),再關(guān)閉. 關(guān)閉之后,流對(duì)象不可以繼續(xù)使用 */
                    outputStream.close();
                    inputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

控制器

 	@ApiOperation(value = "獲取resource下附件")
    @GetMapping(value = "/exportFile")
    public void exportFile(String fileName, HttpServletResponse response) {
        // fileName = "ImportModel.xlsx";
        fileName = "labixiaoxin.jpg";
        LocalFileUtils.exportFile(response, fileName);
    }

Java把靜態(tài)資源文件下載到本地

場(chǎng)景 

springboot項(xiàng)目中下載resources/static 下面的靜態(tài)文件(或者本地文件)

@RequestMapping("/doLoad")
    public void doLoad(HttpServletRequest request, HttpServletResponse response){
        String filename = "×××模版";
        try {
            // 清空輸出流
            response.reset();
            String resultFileName = filename + ".xlsx";
            resultFileName = URLEncoder.encode(resultFileName,"UTF-8");
            response.setCharacterEncoding("UTF-8");
            response.setHeader("Content-disposition", "attachment; filename=" + resultFileName);// 設(shè)定輸出文件頭
            response.setContentType("application/msexcel");// 定義輸出類型
            //輸入流:文件路徑   // 本地路徑:E:\\java\\demo\\導(dǎo)入模板.xlsx
            DataInputStream in = new DataInputStream(
                    new FileInputStream(new File("src/main/resources/static/file/導(dǎo)入模版.xlsx")));
            //輸出流
            OutputStream out = response.getOutputStream();
            //輸出文件
            int bytes = 0;
            byte[] bufferOut = new byte[1024];
            while ((bytes = in.read(bufferOut)) != -1) {
                out.write(bufferOut, 0, bytes);
            }
            out.close();
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
            // 清空輸出流
            response.reset();
        }
    }

效果:

總結(jié)

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

相關(guān)文章

最新評(píng)論