java - 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);
}
}
}
}
解决方案
推荐阅读
- python - Python/Kivy 10 秒长的音频问题,无故停止
- reactjs - 在 React Typescript 中重新加载事件更改按钮
- jsonata - 在 JSONata 中将月或年添加到毫秒
- javascript - 使用 vanilla js append,vue.js 失去了引用
- javascript - TypeSCript 错误:无法将某些类型分配给变量
- php - 重命名“有促销代码?” 在 Woocommerce 中
- typescript - TypeScript 类泛型实例从不推断
- sharepoint - PowerApps 中的集合导出到 Sharepoint 列表问题
- mongodb - 术语“mongo”未被识别为 cmdlet 的名称
- button - 如何使用 ZStack 将元素放在 ForEach 前面而不使其重叠