使用spring controller和ajax在新窗口中打开pdf

时间:2014-01-08 14:01:19

标签: ajax spring pdf

我想在新窗口中显示pdf文件。

这是我的代码。

 <c:if test="${noOfOwners>0 && empty isOwner && licenseBean.businessBean.ownershipType==1}">        
     <c:url value="/auth/newlicense/ownercertification?retailerId=${licenseBean.outletID}" var="ownerCertificationUrl"/>
                <p class="label4"><a href="#" id="ownereligibilityForm">Owner Eligibility Certification</a></p>
                <p>For your license application to be approved, you must provide the lottery with a signed Owner Eligibility Certification form.  Each owner/officer included on the license application must sign the form, accepting the license eligibility standards, terms and conditions.  Instructions on returning the form to the Texas Lottery via mail, fax and email are included on the form.  To open a PDF version of the Owner Eligibility form, click on this link /link to form/.  A PDF version of the form has also been sent to the email address you provided</p>
   </c:if> 
<script>
$(document).ready(function() {
    $("#ownereligibilityForm").click(function(){
        var retailerid = <c:out value='${licenseBean.outletID}'/>;
        $.ajax({
            url : "ownercertification",
            type : "GET",
            data : {
                "retailerId" : retailerid
            },success : function(link){
                window.open(link,"_blank");
                return false;
            },error: function(){

            }
        });
    }); 
});

</script>
  

我的服务器端代码

@RequestMapping(value = "/ownercertification", method = RequestMethod.GET)
public @ResponseBody String openOwnerEligibilityCertificationForm(HttpServletRequest request,HttpServletResponse response){
    File file = new File("/var/lsp/licensing/21/output/"+request.getParameter("retailerId")+"/ownerEligibilityCertificationForm_"+request.getParameter("retailerId")+".pdf");
    response.setContentType("application/pdf");
    response.setHeader("Content-Disposition", "attachment;filename="+file);
    OutputStream out = null;
    try{
        byte[] b = new byte[(int)file.length()];
        out = response.getOutputStream();
        out.write(b);
        out.flush();
    }catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return out.toString();
}
  

但它没有打开pdf文件。我收到以下错误

HTTP状态404 - /lsp/auth/newlicense/org.springframework.security.web.context.SaveContextOnUpdateOrErrorResponseWrapper$SaveContextServletOutputStream[delegate=org.apache.catalina.connector.CoyoteOutputStream@266f266f]

有人可以帮助我吗?

0 个答案:

没有答案