protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String path = this.getServletContext().getRealPath("/download/a.jpg"); String filename = path.substring(path.lastIndexOf("\\") + 1); response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8")); InputStream in = new FileInputStream(path); OutputStream out = response.getOutputStream(); //注意这里要try..catch int len = 0; byte buffer[] = new byte[1014]; while ((len = in.read(buffer)) != -1) { out.write(buffer, 0, len); } }