一种用的是 
http://localhost:8080/myFile.rar
这样的话,用户在浏览器打开这个链接就自动要下载了。第二个方法是
http://localhost:8080/download?id=8989
我想知道,第二种方法怎么实现的?

解决方案 »

  1.   

    可以更加Id找到文件 用流的输出文件流 response.setContentType("application/x-download");//设置为下载application/x-download
           String filenamedownload = filePath;
           String filenamedisplay = fileName;
           filenamedisplay = URLEncoder.encode(filenamedisplay,"UTF-8");
           response.addHeader("Content-Disposition","attachment;filename=" + filenamedisplay);       OutputStream output = null;
           FileInputStream fis = null;
           try
           {
               output  = response.getOutputStream();
               fis = new FileInputStream(filenamedownload);           byte[] b = new byte[1024];
               int i = 0;
               while((i = fis.read(b)) > 0)
               {
                   output.write(b, 0, i);
               }
               output.flush();
           }
           catch(Exception e)
           {
            //not throw ClientAbortException
           }finally
           {
               if(fis != null)
               {
                   fis.close();
                   fis = null;
               }
               if(output != null)
               {
                   output.close();
                   output = null;
               }
           }
      

  2.   

    同意eidolon_warrior(精灵_战士) ,重定向,建立中转页
      

  3.   

    response出去一个流就可以下载了