public ActionForward loadPlacings(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
{
HkFincNewsecReserver placings=(HkFincNewsecReserver)request.getSession().getAttribute("placings");
Locale locale=(Locale) request.getSession().getAttribute(Globals.LOCALE_KEY);
if(placings!=null)
{
loadService=new LoadPlacingsService();
loadService.printResponseInfo(placings, response,locale);
}
return null;
}
public void printResponseInfo(Object object,HttpServletResponse response,Locale local)
{
response.setContentType("text/plain;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment; filename=news.txt");
HkFincNewsecReserver cashDividend=(HkFincNewsecReserver)object;
StringBuffer loadInfo=new StringBuffer();
MessageResources message=MessageResources.getMessageResources("ApplicationStock");
//标题
loadInfo.append(message.getMessage(local,"bochk.message.code.placings"));
loadInfo.append("/r/n/t");
//内容
loadInfo.append(message.getMessage(local,"bochk.market.dynamic.announcedate"));
loadInfo.append(" ");
loadInfo.append(cashDividend.getAnnounceDate());
loadInfo.append("/r/n/t");
try
{
response.getWriter().write(loadInfo.toString());
}
catch (IOException e)
{
// TODO Auto-generated catch block
log.error(e.getMessage(),e);
}
}
关键的是红色标记部分的http头中的参数设置,一般根据实际的操作要求来设置。
比如下载为Excel文件,并在IE中直接打开文件:
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "inline; filename=test.xls");
如果将inline改为attachment,再选打开的话,将会用office工具打开文件。