java實現通過瀏覽器下載文件


 

OutputStream output;
        try {
            output = response.getOutputStream();
            //清空緩存
            response.reset();
            //定義瀏覽器響應表頭,順帶定義下載名,比如students(中文名需要轉義)
            String s = file.getName() + file.getBirthday();
            response.setHeader("Content-disposition", "attachment;filename=" + new String(s.getBytes(), "iso-8859-1") + ".xls");
            //定義下載的類型,標明是excel文件
            response.setContentType("application/vnd.ms-excel");
            //這時候把創建好的excel寫入到輸出流
            wb.write(output);
            //養成好習慣,出門記得隨手關門
            output.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM