首页 > 解决方案 > Java 发出 POST 请求以表达 js 服务器并下载 PDF 文件

问题描述

我有 express js 服务器作为 API。我需要向该服务器发送 POST 请求,并且我的网站需要将 PDF 文件下载到客户端计算机。

这是我的快速服务器代码:

const pdf = await generatePDF(data.originURL, data.url, data.pageOrientation); //pdf generation

// send pdf to client
res.writeHead(200, {
  'Content-Type': 'application/pdf',
  'Content-Disposition': 'attachment; filename=file.pdf',
  'Content-Length': pdf.length
});
res.end(pdf);

在我的网站中,我有 commandButton 调用 POST 请求的操作;我做错了什么,我的网站没有下载任何文件?

发出POST请求的方法:

public void makeRequest() {
  HttpURLConnection connection = null;
    try {
        String imageExportServer = "url....";
        URL url = new URL(imageExportServer);
        connection = (HttpURLConnection)url.openConnection();


            Stopwatch stopwatch = Stopwatch.createStarted();
            try {

                connection.setDoOutput(true);
                connection.setRequestMethod("POST");
                connection.setRequestProperty("Content-Type", "application/json");

                try (OutputStream stream = connection.getOutputStream()) {

                        JsonObject configJson = new JsonObject();
                        configJson.addProperty("originURL", "url");
                        configJson.addProperty("url", "url...");
                        configJson.addProperty("pageOrientation", "someText");
                        stream.write(Builder().create().toJson(configJson).getBytes());

                }

                int responseCode = connection.getResponseCode();
                if (responseCode != 200) {
                    //log.warn("Error response: ", responseCode);


                }


                    String fileName = "";
                    String disposition = connection.getHeaderField("Content-Disposition");
                    String contentType = connection.getContentType();
                    int contentLength = connection.getContentLength();

                    System.out.println("Content-Type = " + contentType);
                    System.out.println("Content-Disposition = " + disposition);
                    System.out.println("Content-Length = " + contentLength);
                    System.out.println("fileName = " + fileName);

                    // opens input stream from the HTTP connection
                    InputStream inputStream = connection.getInputStream();

                    // opens an output stream to save into file
                    FileOutputStream outputStream = new FileOutputStream("neki.pdf");

                    int bytesRead = -1;
                    byte[] buffer = new byte[contentLength];
                    while ((bytesRead = inputStream.read(buffer)) != -1) {
                        outputStream.write(buffer, 0, bytesRead);
                    }

                    outputStream.close();
                    inputStream.close();

                    FileOutputStream fos = new FileOutputStream("neki.pdf");
                    fos.write(buffer);
                    fos.close();

                    System.out.println("File downloaded");


                }
            } catch (IOException e) {
                //log.warn(e.getMessage(), e);
            } finally {
                //log.info("Exporting chart of type '%s' took %sms.", "tyoe", stopwatch.elapsed(TimeUnit.MILLISECONDS));
            }

    } catch (IOException e) {
        //log.warn(e.getMessage(), e);
    } finally {
        if (connection != null) {
            try {
                connection.disconnect();
            } catch (Exception e) {
                //log.warn(e.getMessage(), e);
            }
        }
    }
}

标签: javaexpress

解决方案


推荐阅读