当前位置: 代码迷 >> Web前端 >> java以源的形式显示文件
  详细解决方案

java以源的形式显示文件

热度:365   发布时间:2013-11-25 13:22:27.0
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
  相关解决方案