用Servlet實現文件下載

byte[] fileBuf = getbyte();//獲得輸出的數據
  long len = fileBuf.length;
  int k = 0;
 
  byte[] buf = new byte[65535];
  response.setContentType("aplication/txt; charset=MS932");
  response.setContentLength((int) len);
  response.setHeader(
  "Content-Disposition",
  "attachment;" + " filename=" + fileName);
 
  OutputStream out = response.getOutputStream();
  try {
  while ((long) k < len) {
  int j = 0;
  for (int i = 0; i < Constants.MAX_BUF_SIZE; i++) {
  if ((long) k < len) {
  buf[i] = fileBuf[k];
  } else {
  break;
  }
  k++;
  j++;
  }
  out.write(buf, 0, j);
  }
  } finally {
  out.close();
  }
 
  return null;get

  }

it

相關文章
相關標籤/搜索