日期:2014-05-18  浏览次数:20897 次

jsp文件下载后,内容是空的
代码如下
<%@page import="java.io.FileInputStream"%>
<%@page import="java.io.OutputStream"%><%@ page
  contentType="text/html; charset=utf-8"%>
<%
  String fileName = "freebsd.iso";
  String filePath = "e:\\";

  out.clear();
  response.reset();
  response.setContentType("application/x-download");
  response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
  OutputStream os = response.getOutputStream();
  try {
  FileInputStream fis = new FileInputStream(filePath + fileName);
  try {
  byte[] buffer = new byte[1024 * 10];
  for (int read; (read = fis.read(buffer)) != -1;) {
  os.write(buffer, 0, read);
  }
  } finally {
  fis.close();
  }
  } finally {
  os.close();
  }
%>

执行后,文件已经下载过来了,但是内容是空的,请教是什么原因啊?


------解决方案--------------------
没有读取到内容吧。。
byte[] buffer = new byte[1024 * 10];
while(read = fis.read( buffer,0 ,1024 * 10 )) != -1;) {
os.write(buffer, 0, read);
}看看行么

------解决方案--------------------
os.write(buffer, 0, buffer.length);
------解决方案--------------------
为什么用for循环呢?
个人觉得用while循环好一些
int length=0;
while((read = fis.read(buffer)) != -1)
{
os.write(buffer, 0, length);
}
------解决方案--------------------
探讨

为什么用for循环呢?
个人觉得用while循环好一些
int length=0;
while((read = fis.read(buffer)) != -1)
{
os.write(buffer, 0, length);
}