[英]How does a Java application server decide what charset to send in the HTTP Content-type header?
[英]Send application/pdf content with http call
如何通过 http 调用将 pdf 上传到服务器。
我目前正在将 pdf 内容加载到一个变量并尝试使用 http 发送它:
byte[] pdfContent;
String output = "";
try {
pdfContent = Files.readAllBytes(Paths.get("C:...//test.pdf"));
url = new URL(url);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "application/pdf");
conn.setConnectTimeout(5000);
conn.setReadTimeout(5000);
conn.setRequestMethod("POST");
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
wr.writeBytes(pdfContent.toString());
wr.flush();
wr.close();
我得到的错误是:
Wrong content! The content of file 'test.pdf' does not match its presumed content type ('application/pdf').
发送 pdf 上传的正确方法是什么? 将二进制内容加载到变量并将其写入连接不正确吗?
你试过 wr.write(pdfContent); ? write() function 将 byteArray 作为参数,因此如果问题是由转换为 String 引起的,它可能会通过这种方式解决
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.