//實現文件下載(若是是中文文件名的話,在輸出給客戶機下載時,要記得url編碼) public class ResponseDemo3 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String path = this.getServletContext().getRealPath("/download/中國人.jpg"); String filename = path.substring(path.lastIndexOf("\\")+1); //設置下載頭信息,若是文件是中文,必需要進行URL編碼 response.setHeader("content-disposition","attachment;filename=" + URLEncoder.encode(filename, "UTF-8")); FileInputStream in = new FileInputStream(path); int len = 0; byte buffer[] = new byte[1024]; OutputStream out = response.getOutputStream(); while((len=in.read(buffer))>0){ out.write(buffer, 0, len); } in.close(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }