public static void downloadLocal(HttpServletRequest request,HttpServletResponse response,
String fileName,String title,String suffix)
throws FileNotFoundException {
title = UtilFunction.convert(title, "gb2312", "ISO8859_1");
response.reset();
//告诉浏览器输出内容为流
response.setContentType("application/octet-stream");
/*
* attachment是以附件下载的形式,inline是以线上浏览的形式。
* 当点击“保存”的时候都可以下载,
* 当点击“打开”的时候attachment是在本地机里打开,
* inline是在浏览器里打开。*/
response.addHeader("Content-Disposition", "inline;filename=" + title + suffix);
OutputStream os = null;
BufferedInputStream bis = null;
try
{
InputStream is = new FileInputStream(fileName);
bis = new BufferedInputStream(is);
response.setContentLength(bis.available());
byte[] buf = new byte[1024];
int bytes = 0;
os = response.getOutputStream();
while ((bytes = bis.read(buf)) != -1)
{
os.write(buf, 0, bytes);
}
} catch (Exception e)
{
log.error(e.getMessage(), e);
} finally
{
try
{
if (bis != null)
{
bis.close();
}
if (os != null)
{
os.close();
}
} catch (IOException e)
{
log.error("down file error", e);
}
}
}
java 实现文件下载时,弹出文件保存窗口(IE9)
最新推荐文章于 2021-02-25 16:31:03 发布