亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

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

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

項目結(jié)構(gòu)

下載工具類

/**
 * @program: myutil
 * @description: 從本地項目(本地磁盤上)下載靜態(tài)文件
 * @author: lsy
 * @create: 2020-08-13 16:58
 **/
public class LocalFileUtils {
    /**
     * @param response
     * @param fileName
     * @description 根據(jù)指定項目路徑下的某個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ū)(一般寫字符時要用,因為字符時先進入緩沖區(qū)),然后將內(nèi)存中的數(shù)據(jù)立刻寫出(因為緩沖區(qū)是裝滿之后才會寫出
                     ,用flush()就不必等到緩沖區(qū)滿,立刻寫出,流對象還可以繼續(xù)使用) */
                    outputStream.flush();
                    /**close():關(guān)閉流對象. 也會先刷新一次緩沖區(qū),再關(guān)閉. 關(guān)閉之后,流對象不可以繼續(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)資源文件下載到本地

場景 

springboot項目中下載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é)

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

相關(guān)文章

最新評論