paradrop.xlsx 存在 src/main/resource/paradrop.xlsx @RequestMapping(value = "/downloadExcel") public void downloadExcel(HttpServletResponse response, HttpServletRequest request) throws IOException { String fileName = "paradrop.xlsx"; InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName); //強制下載不打開 response.setContentType("application/binary;charset=ISO8859-1"); OutputStream out = response.getOutputStream(); //使用URLEncoder來防止文件名亂碼或者讀取錯誤 response.setHeader("Content-Disposition", "attachment; filename="+ URLEncoder.encode(fileName, "UTF-8")); int b = 0; byte[] buffer = new byte[1000000]; while (b != -1) { b = inputStream.read(buffer); if(b!=-1) out.write(buffer, 0, b); } inputStream.close(); out.close(); out.flush(); }