<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %><%@page import="cn.js.fan.util.*"%><%@page import="cn.js.fan.web.*"%><%@page import="cn.js.fan.module.cms.*"%><%@page import="java.util.*"%><%@page import="java.io.*"%><%@page import="java.net.*"%><%
  int id = ParamUtil.getInt(request, "id");
  int attId = ParamUtil.getInt(request, "attachId");

  Document mmd = new Document();
  mmd = mmd.getDocument(id);
  Attachment att = mmd.getAttachment(1, attId);
  
  String s = Global.getRealPath() + att.getVisualPath() + "/" + att.getDiskName();
  java.io.File f = new java.io.File(s);
  java.io.FileInputStream fis = new java.io.FileInputStream(f);

  response.reset();
  //Accept-Ranges: bytes
  response.setHeader("Accept-Ranges", "bytes");

  long p = 0;
  long l = 0;
  l = f.length();

  if (request.getHeader("Range") != null) 
  {
   //HTTP/1.1 206 Partial Content
   response.setStatus(javax.servlet.http.HttpServletResponse.SC_PARTIAL_CONTENT);//206

   p = Long.parseLong(request.getHeader("Range").replaceAll("bytes=","").replaceAll("-",""));
  }

  response.setHeader("Content-Length", new Long(l - p).toString()); 

  if (p != 0)
  {
   response.setHeader("Content-Range","bytes " + new Long(p).toString() + "-" + new Long(l -1).toString() + "/" + new Long(l).toString());
  }
  //Content-Type: application/octet-stream
  response.setContentType("application/octet-stream");
  response.setHeader("Content-Disposition", "attachment;filename=\"" + StrUtil.GBToUnicode(att.getName()) + "\"");

  //raf.seek(p);
  fis.skip(p);

  byte[] b = new byte[1024]; 
  int i;
  while ( (i = fis.read(b)) != -1 )
  {
   response.getOutputStream().write(b,0,i);
  }
  fis.close();
  
  att.setDownloadCount(att.getDownloadCount() + 1);
  att.save();  
%>