如何在AEM页面上显示来自Webservice的Excel,PDf文件

时间:2015-07-22 16:04:20

标签: aem sling

我尝试执行以下操作:Web服务的响应是excel(单独调用pdf)文件。我需要将此文件显示为aem页面上的链接,当用户单击该链接时,浏览器将打开(或下载)该文件。

使用案例:在客户页面上,有一个部分包含订单历史记录(Excel文件),发票(PDF文件),产品目录(Excel文件)的链接。单击每个链接,调用webservice并获取相应的文件。

如何实现这个目标?

1 个答案:

答案 0 :(得分:0)

在Scott的帮助下: http://help-forums.adobe.com/content/adobeforums/en/experience-manager-forum/adobe-experience-manager.topic.html/forum__xhh5-objective_therespo.html

这是我的解决方案:

  1. 从UI中,将操作提交给Sling Servlet

    <form name="importFileForm" method="get" action="/services/getData">
    <input type="submit" title="Submit" value="Submit" name="bttnAction">
    </form>
    
  2. 您的Servlet类

    public class TTIGetServlet extends SlingAllMethodsServlet {
      @Override
      protected void doGet(SlingHttpServletRequest request,SlingHttpServletResponse response) throws ServletException,IOException { 
        ...
        ...
        String serviceurl = <<< your webservice url>>>
        HttpClient httpclient = HttpClients.custom().build();
        generateFile(serviceurl, httpclient, request, response);
    
        RequestDispatcher dispatcher = request.getRequestDispatcher("/content/ttii/en/importfiletest.html");
        dispatcher.forward(request, response);        
      }
    }
    
  3. 生成在浏览器上弹出文件下载的文件方法

    public static void generateFile(String serviceurl,
                                HttpClient httpclient,
                                SlingHttpServletRequest httpRequest, 
                                SlingHttpServletResponse httpResponse) throws ClientProtocolException, IOException {
    
       HttpResponse response;
       HttpGet httpGet = new HttpGet(serviceURL);
    
       // Makes the call to WebService
       response = httpclient.execute(httpGet);
    
       // CORE LOGIC
       if (response!=null) {
         ContentType contentType = ContentType.getOrDefault(response.getEntity());
        String mimeType = contentType.getMimeType();
    
        if (mimeType.equals(MIMETYPE_JSON)) {
            // Out of context here... 
        } else {
            // SHOW THE FILE
            ServletOutputStream sos = httpResponse.getOutputStream();                
            httpResponse.setContentType("application/vnd.ms-excel");
            httpResponse.setHeader("Content-Disposition", "attachment;filename=test.xls");
    
            BufferedHttpEntity buf = new BufferedHttpEntity(response.getEntity());
            InputStream istream = buf.getContent();   
    
            sos.write(FileHelper.writeFiles(istream));                
            sos.flush();     
        }
    }
    

    }