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();
}
}
流下载
最新推荐文章于 2023-09-22 00:35:12 发布