将 Java 代理/JAR 文件中的值返回到 LotusScript

问题描述 投票:0回答:1

有这个Java代码:

public class PdfByteReader {

    /**
     * @param args the command line arguments
     */
   public static void main(String[] args) {
        String filePath = args[0];
        try {
            String pdfContent = readPdfFileByteByByte(filePath);
            System.out.println(pdfContent);
        } catch (IOException e) {
            System.err.println("ERROR" + e.getMessage());
        }
    }

    public static String readPdfFileByteByByte(String filePath) throws IOException {
        File pdfFile = new File(filePath);
        StringBuilder content = new StringBuilder();

        try (FileInputStream fis = new FileInputStream(pdfFile)) {
            int byteRead;
            while ((byteRead = fis.read()) != -1) {
                content.append((char) byteRead);
            }
        }

        return content.toString();
    }
    
}

我可以在 Lotus Notes 应用程序中构建 JAR 文件或创建 Java 代理,但是:我如何从 Lotus 脚本接收此(字符串)的返回值?

我正在尝试将 pdf 文件解析为字符串以便上传到 API

java lotus-notes lotusscript
1个回答
0
投票

如果可能,不要在 LotusScript 中这样做。

如果您有 Java 代理,请添加如下内容:

public static void postPdfString(String pdfString) {
    String endpointUrl = "https://api.myexample.com/pdfuploadendpoint";
    
    try {
        URL url = new URL(endpointUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "application/pdf");
        connection.setRequestProperty("Accept", "application/json");
        connection.setDoOutput(true);
        byte[] out = pdfString.getBytes(StandardCharsets.UTF_8);
        connection.setFixedLengthStreamingMode(out.length);
        try (OutputStream os = connection.getOutputStream()) {
            os.write(out);
        }
        int responseCode = connection.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK) {
            System.out.println("PDF uploaded successfully.");
        } else {
            System.out.println("Failed to upload PDF. Response code: " + responseCode);
        }
        connection.disconnect();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

然后您可以传递 pdfString 并将其发布到终点。

© www.soinside.com 2019 - 2024. All rights reserved.