0

我在读取来自服务器的响应时遇到问题。如果我使用 StoreFile 参数并通过 URL 访问 PDF 看起来很棒,但是通过我自己的服务器提供它总是会使页面变为空白。可能是编码问题?

def convertPdf(document) {
    File f = new File(document)
    RestBuilder rest = new RestBuilder()
    def resp = rest.post("https://do.convertapi.com/Word2Pdf") {
        contentType "multipart/form-data"
        ApiKey = "CONFIDENTIAL"
        file = f
    }
    InputStream istream = new ByteArrayInputStream(resp.getBody().getBytes());
    File file = new File("123123.pdf");
    FileOutputStream ostream = new FileOutputStream(file);
    byte[] b = new byte[1024];
    int num = 0;
    while ((num = istream.read(b)) != -1) {
        ostream.write(b, 0, num);
    }
    istream.close();
    ostream.flush();
    ostream.close();
}
4

1 回答 1

1

通过使用“mashape”上的方法并使用他们的 Unirest 库来修复。

def downloadPdf() {
    HttpResponse<InputStream> response = Unirest.post("https://do.convertapi.com/Word2Pdf")
    .field("ApiKey", "CONFIDENTIAL")
    .field("File", new File(params.document))
    .field("OutputFormat", "pdf")
    .field("Timeout", 300)
    .field("OutputFileName", params.fileName)
    .asBinary();
    render(file: response.getBody(), contentType: "application/octet-stream", fileName: params.fileName+".pdf")
}
于 2015-12-17T16:50:24.747 回答