java web 下載本地文件並彈出下載框


1 window.open("/dept/download")
jsp代碼
 1 @RequestMapping(value = "/download", method = RequestMethod.GET)
 2     public void downstudents(HttpServletRequest request, HttpServletResponse response) throws IOException {
 3         String filename = request.getSession().getServletContext().getRealPath("/static/email/");
 4         String filenameZip = filename + "\\dept.xls";
 5 
 6         try {
 7             response.setContentType("application/x-execl");
 8             response.setHeader("Content-Disposition",
 9                     "attachment;filename=" + new String(("dept.xls").getBytes(), "UTF-8"));
10             // 讀取文件
11             InputStream in = new FileInputStream(filenameZip);
12             ServletOutputStream outputStream = response.getOutputStream();
13             // 寫文件
14             int b;
15             while ((b = in.read()) != -1) {
16                 outputStream.write(b);
17             }
18 
19             in.close();
20             outputStream.close();
21         } catch (UnsupportedEncodingException e) {
22             e.printStackTrace();
23         } catch (IOException e) {
24             e.printStackTrace();
25         }
26 
27     }
javaCode

 


免責聲明!

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



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