正在Grails中下载Docusign PDF,文件已损坏

正在Grails中下载Docusign PDF,文件已损坏,grails,docusignapi,Grails,Docusignapi,使用Groovy1.8.6和Grails2.1.0 使用嵌入式API,在用户签署文档后,浏览器被重定向回我的应用程序。使用“获取信封文档和证书”API将文档下载到服务器。URL格式: “${baseUrl}/envelopes/${envelopeId}/documents/combined” 代码段(已删除次要详细信息): private void getDocument(requestUrl){ def connection=urlConnect(requestUrl,null,“GET”)

使用Groovy1.8.6和Grails2.1.0

使用嵌入式API,在用户签署文档后,浏览器被重定向回我的应用程序。使用
“获取信封文档和证书”
API将文档下载到服务器。URL格式:

“${baseUrl}/envelopes/${envelopeId}/documents/combined”

代码段(已删除次要详细信息):

private void getDocument(requestUrl){
def connection=urlConnect(requestUrl,null,“GET”)
if(connection.responseCode==200){
savePDF(信封ID,connection.inputStream)
}
}
私有void savePDF(信封ID,输入流){
String filePath=getSavedPDFPath(信封ID)
def pdfWriter=新文件(filePath).newWriter()

pdfWriter根据问题的注释线程,问题是由文件的保存方式引起的。改为使用此代码,文件可以正确保存/打开:

private void savePDF(envelopeId, connection) 
{ 
    FileOutputStream fop = null; 
    File file; 
    String filePath = getSavedPDFPath(envelopeId); 
    try { 
        file = new File(filePath); 
        fop = new FileOutputStream(file); 
        byte[] buffer = new byte[1024]; 
        int numRead; 
        while((numRead = connection.getInputStream().read(buffer)) > 0) 
        { 
            fop.write(buffer, 0, numRead); 
        } 
        fop.flush(); 
        fop.close(); 
    } 
    catch (Exception e) 
    { 
        throw new RuntimeException(e); 
    } 
}

如果您(手动)登录到DocuSign web控制台并尝试在那里打开/查看信封(文档),会发生什么情况?并且--您是否一直看到此问题(对于所有信封),是零星问题(仅对于某些信封),还是一次性问题(仅针对一个特定信封)?如果我登录到控制台,我可以查看文档。如果我从控制台下载文档,它会在Adobe Reader中正常打开。如果我将手动下载的文件与应用程序下载的文件进行二进制比较,则会出现数千次错误比较。对于随应用程序下载的所有文件,都会发生这种情况。FWIW,我从未见过体验过您描述的问题——即,我通过API下载的文件总是毫无问题地打开。因此,我怀疑这是DocuSign API响应的内容问题——更有可能是下载过程中访问/保存字节流的方式问题。您是否尝试过简单地编写b将数据流(从API响应)直接传输到浏览器?如果成功,则会将问题缩小到“保存文件”过程中出现的问题。我用这个(非groovy)版本替换了savePDF方法,现在一切正常:私有void savePDF(信封ID,连接){FileOutputStream fop=null;File File;String filePath=getSavedPDFPath(envelopeId)try{File=new File(filePath);fop=new FileOutputStream(File);byte[]buffer=new byte[1024];int numRead;while((numRead=connection.getInputStream().read(buffer))>0){fop.write(buffer,0,numRead);}fop.flush();fop.close();}catch(Exception e){throw new RuntimeException(e);}}}很高兴听到这个消息。我将添加您评论中的代码作为此线程的“答案”,以便其他人将来可以从这些信息中受益。如果您不介意,请将其标记为“已接受”回答,以便其他人知道答案正确/有效…谢谢!
private void savePDF(envelopeId, connection) 
{ 
    FileOutputStream fop = null; 
    File file; 
    String filePath = getSavedPDFPath(envelopeId); 
    try { 
        file = new File(filePath); 
        fop = new FileOutputStream(file); 
        byte[] buffer = new byte[1024]; 
        int numRead; 
        while((numRead = connection.getInputStream().read(buffer)) > 0) 
        { 
            fop.write(buffer, 0, numRead); 
        } 
        fop.flush(); 
        fop.close(); 
    } 
    catch (Exception e) 
    { 
        throw new RuntimeException(e); 
    } 
}