流下载

本文介绍了一个Java方法用于导出指定前缀的数据文件,并提供了处理文件未找到及读取失败的异常逻辑。此外,还实现了一个通用的弹窗提示方法,用于向用户展示消息并关闭窗口。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


private ActionForward doExportPrefixData(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
{
String fileName=request.getParameter("fileName"); //页面传来的文件名
String filePath=ServerInfo.getDownloadPath()+fileName;//路径+文件名
try
{
InputStream in=new FileInputStream(new File(filePath));
BufferedInputStream bfin=new BufferedInputStream(in);
response.setBufferSize(5*1024*1024);//5M大小
response.setContentType("APPLICATION/OCTET-STREAM");
response.setHeader("Content-Disposition","attachment;filename="+fileName);//设置文件头
OutputStream out=response.getOutputStream();
BufferedOutputStream bfout=new BufferedOutputStream(out);
int len=0;
byte[] b=new byte[1024*1024];
while((len=bfin.read(b))!=-1)
{
bfout.write(b, 0, len);
bfout.flush();
}
bfout.close();
out.close();
bfin.close();
in.close();
}
catch (FileNotFoundException e)
{
showInfoDialog(response,"文件没找到");
}
catch (IOException e)
{
showInfoDialog(response,"文件读取失败");
}
return null;
}
/**
*
* 方法描述:公用的弹出提示框
* @param response
* @param msg 要提示的消息
*/
private void showInfoDialog(HttpServletResponse response,String msg)
{
try
{
response.setContentType("text/html;charset=gb2312");
PrintWriter pw=response.getWriter();
pw.print("<script>");
pw.print("alert('"+msg+"');window.close();");
pw.print("</script>");
pw.flush();
pw.close();
}
catch (IOException e1)
{
e1.printStackTrace();
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值