使用Http Client for Request返回内容类型为“application / vnd.ms-excel”的jsp页面

时间:2013-04-04 04:58:31

标签: java spring spring-mvc

我有一个请求处理程序,它返回内容类型为“application / vnd.ms-excel”id的jsp页面。

我有另一个控制器,我使用HttpClient调用了上面的控制器但是我无法下载文件

代码如下:

返回具有excel作为内容类型的jsp文件的控制器

@RequestMapping(value = {"InvoiceGeneration/generatePDF"}, method=RequestMethod.GET)
                public String generatePDF(Model model)
                {

                }

使用Http Client调用控制器上方的控制器

  @RequestMapping(value = {"InvoiceGeneration/generateMonthPDF"}, method=RequestMethod.GET)
                    @ResponseBody
                public  HttpEntity generateMonthPDF(Model model){


                        DefaultHttpClient httpClient = new DefaultHttpClient();
                        HttpGet getRequest = new HttpGet("http://localhost:8080/BillingInvoice/InvoiceGeneration/generatePDF?invoiceNumber=10");
                        getRequest.addHeader("Content-Type", "application/vnd.ms-excel");
                        getRequest.addHeader("Content-Disposition","attachment; filename=\""+"kedar.xls"+"\"");

                            HttpResponse response = httpClient.execute(getRequest);

                        return response.getEntity();

                    }

如何获得excel文件下载的响应????提前致谢

1 个答案:

答案 0 :(得分:0)

必须将以下内容设置为响应

         response.setContentType("application/octet-stream");
         or 
         response.setContentType("application/vnd.ms-excel");

         response.setHeader("Content-disposition", "inline;filename=" + fileName);