`
huxiuliang
  • 浏览: 119779 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

实现文件下载代码

    博客分类:
  • Java
F# 
阅读更多
response.setContentType("application/x-download");
      String filedownload="f:/test.ppt";
      String filedisplay = "测试.ppt";
      String filenamedisplay = URLEncoder.encode(filedisplay,"UTF-8");
      response.addHeader("Content-Disposition","attachment;filename=" + filenamedisplay);
      OutputStream outp = null;
      FileInputStream in = null;
      try
      {
          outp = response.getOutputStream();
          in = new FileInputStream(filedownload);
          byte[] b = new byte[1024];
          int i = 0;
          while((i = in.read(b)) > 0)
          {
              outp.write(b, 0, i);
          }
          outp.flush();
      }
      catch(Exception e)
      {
          System.out.println("Error!");
          e.printStackTrace();
      }
      finally
      {
          if(in != null)
          {
              in.close();
              in = null;
          }
          if(outp != null)
          {
              outp.close();
              outp = null;
          }
      }
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics