Java 中的 Convertapi Word2PDF

Convertapi Word2PDF in Java

我在读取服务器响应时遇到问题。如果我使用 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();
}

已通过 "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")
}