分享

文件下载的其中之一种

 凤舞天煌 2007-07-12
<%
   
 try
 { 
      String path = request.getRealPath("/"); //取ROOT的当前目录
      String filename=request.getParameter("filename");//取上一个页面传过来的文件名称
      String str=CONV.toStr(request.getParameter("filePath"),"");//取上一个页面传过来的路径
      str="F:/WEBROOT/EMS/"+str.substring(6);//组合出要下载文件的路径
      
      String gMimetype = "";
      Hashtable Ht = new Hashtable();
      String name = "";
      Ht.put(".doc", "application/msword");
      Ht.put(".asf", "application/vnd.ms-asf");
      Ht.put(".xls", "application/vnd.ms-excel");
      Ht.put(".ppt", "application/vnd.ms-powerpoint");
      Ht.put(".mmp", "application/vnd.ms-project");
      Ht.put(".csv", "text/comma-separated-values");
      Ht.put(".zip", "application/zip");
      Ht.put(".rar", "application/rar");
     
      String suffix = filename.substring(filename.lastIndexOf("."));
     
      gMimetype = (String)Ht.get(suffix);
      if(gMimetype == null || gMimetype.equals("") || gMimetype.equals("null"))
      {
         gMimetype = "application/octet-stream";
      }
FileInputStream fileinputstream = new FileInputStream(str);//读取模块文件的内容
  int lenght = fileinputstream.available();
  byte bytes[] = new byte[lenght];
  fileinputstream.read(bytes);
  fileinputstream.close();
  String templateContent = new String(bytes);
 
  response.setContentType(gMimetype);
  response.setHeader("Content-Disposition", "attachment; filename=" + java.net.URLEncoder.encode(filename, "UTF-8"));
      //注意, 上面的"java.net.URLEncoder.encode(filename, "UTF-8")"代码,能使中文名称的附件名不显示乱码!
        
         out.write(templateContent);
         out.flush();
         out.close();
  
   }
   catch(Exception e)
   {
       out.print("异常");
       out.print(e.toString());
   }            
              
%>

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多