1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| @RequestMapping (value="/download") public String downloads (HttpServletResponse response ,HttpServletRequest request) throws Exception { String path = request.getServletContext ().getRealPath ("/upload"); String fileName = "基础语法.jpg";
response.reset (); response.setCharacterEncoding ("UTF-8"); response.setContentType ("multipart/form-data"); response.setHeader ("Content-Disposition", "attachment;fileName="+URLEncoder.encode (fileName, "UTF-8"));
File file = new File (path,fileName); InputStream input = new FileInputStream (file); OutputStream out = response.getOutputStream ();
byte [] buff =new byte [1024]; int index=0; while ((index= input.read (buff))!= -1){ out.write (buff, 0, index); out.flush (); } out.close (); input.close (); return null; }
|