如何下载Java中通过HTTP请求处理的文件



我正在编写一个程序,该程序在GUI中构建内容(等等…不相关的细节(,用户可以将数据导出为.tex文件,该文件可以编译为PDF。由于我不想假设他们安装了TeX环境,所以我使用了API(latexonline.cc(。这样,我就可以构建一个HTTPGET请求,将其发送到API,然后(希望!(以字节流的形式返回PDF。然而,问题是,当我提交请求时,我只从请求中获取页面数据,而不是从PDF中获取数据。我不确定这是否是因为我如何处理我的请求。。。

这是代码:

... // preceding code
DataOutputStream dos = new DataOutputStream(new FileOutputStream("test.pdf"));
StringBuilder httpTex = new StringBuilder();
httpTex.append(this.getTexCode(...)); // This appends the TeX code (nothing wrong here)
// Build the URL and HTTP request.
String texURL = "https://latexonline.cc/compile?text=";
String paramURL = URLEncoder.encode(httpTex.toString(), "UTF-8");
URL url = new URL(texURL + paramURL);
byte[] buffer = new byte[1024];
try {
InputStream is = url.openStream();
int bufferLen = -1;
while ((bufferLen = is.read(buffer)) > -1) {
this.getOutputStream().write(buffer, 0, bufferLen);
}
dos.close();
is.close();
} catch (IOException ex) {
ex.printStackTrace();
}

编辑:以下是我从GET请求中获得的数据:https://pastebin.com/qYtGXUsd

已解决!我使用了一个不同的API,它运行得很好。

https://github.com/YtoTech/latex-on-http

最新更新