struts

private void getFile(String filePath){
try {
File file = new File(filePath);
String fileName = file.getName();
response.setContentType("application/vnd.ms-excel"); // 设置头,浏览器不认识就提示下载
response.setHeader("Content-Disposition", "attachment;filename="
+ new String(fileName.getBytes("GB2312"), "ISO8859-1"));
// 以流的形式写出文件
FileInputStream bis = new FileInputStream(file);// 以流的形式写出文件
OutputStream bos = response.getOutputStream();
byte[] buff = new byte[1024];
int readCount = 0;
readCount = bis.read(buff);
while (readCount != -1) {
bos.write(buff, 0, readCount);
readCount = bis.read(buff);
}
if (bis != null)
bis.close();
if (bos != null)
bos.close();
// 下载完毕,给浏览器发给完毕的头
response.setStatus(HttpServletResponse.SC_OK);
response.flushBuffer();
} catch (Exception e) {
e.toString();
}
}

你可能感兴趣的:(文件的下载)