java download

@RequestMapping (value = "Down", Method = RequestMethod.GET) 
    @ResponseBody 
    public  void Down (the HttpServletRequest Request, the HttpServletResponse Response) throws IOException {
         // Get root 
        String realPath = request.getSession (). GetServletContext (). GetRealPath ( "/" ); 
     // file name 
        String filename = "com.association.kingsuper_1.1.1_25.apk" ;
         // GET request, the parameter contains the Chinese need to transform yourself.
        // Of course, if you use the "Global Code Filter", this would not deal with the 
        filename = new new String (filename.getBytes ( "ISO-8859-1"), "UTF-8" ); 
      // file Address 
        String filepathRealpath + = filename; 
        File File = new new File (filepath);
         IF (! File.Exists ()) { 
            response.getWriter () Print (. "You want to download the file does not exist!" );
             Return ; 
        } 
        // all the browser will use the local encoding, that the operating system uses Chinese GBK
         // after the browser receives the file name, iso-8859-1 will be used to decode the 
        filename = new new String (filename.getBytes ( "GBK"), "ISO- 8859-1 " ); 
        Response.AddHeader ( " Content-Disposition "," Attachment; filename = "+ filename); 
        IOUtils.copy ( new new the FileInputStream (File), Response.getOutputStream());
    }

 

Guess you like

Origin www.cnblogs.com/SeaWxx/p/10943428.html