首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

java以源的形式显示文件

2013-11-25 
java以流的形式显示文件java以流的形式显示文件/*** 在客户端打开附件*/public voiddoGet(HttpServletRequ

java以流的形式显示文件
java以流的形式显示文件
  /**
   * 在客户端打开附件
   */
  public void  doGet(HttpServletRequest req, HttpServletResponse rep) throws ServletException,IOException{
      HttpServletRequest request=(HttpServletRequest)req;
      javax.servlet.http.HttpServletResponse response=(HttpServletResponse)rep;
      ServletOutputStream outw=response.getOutputStream();
      try{
         String fileId=request.getParameter("fileId");
         attachment att=attachmentManager.getAttachment(fileId);
         response.setContentType(att.getMimeType());
         String title=att.getTitle();
         if(title.endsWith(".gw")){
           title = title.substring(0, title.lastIndexOf(".")) + ".gd";
         }
         response.setHeader("Content-Disposition","attachment;filename="" +new String(title.getBytes(),"iso-8859-1") + """);
         String filePath=att.getFilePath();
         if(filePath.endsWith(".gw")){
           filePath=filePath.substring(0,filePath.lastIndexOf("."))+".gd";
         }
         FileInputStream inp = new FileInputStream(getUploadPath() + filePath);
         int i;
         byte [] buffer = new byte[1024];
         while ( (i = inp.read(buffer)) != -1) {
               outw.write(buffer, 0, i);
         }
         inp.close();
         outw.flush();
      }
      catch (Exception e){
         e.printStackTrace();
      } finally {
         outw.close();
      }
  }
50校招生网  http://www.50xiao.com

热点排行