我将文件发布到REST API时遇到问题。 documentInputStream
来自上一个函数,用于获取文件的InputStream,它可以工作(它将文件写入我的文件系统。
我还在调试时检查过,documentInputStream
的文件大小(35Kb)为count = 35125
但是服务器未获取任何文件。 (这不是服务器问题,因为它适用于邮递员。)
有帮助吗?
// Send document
HttpURLConnection submitConnection = (HttpURLConnection) new URL(submitURL).openConnection();
submitConnection.setRequestMethod("POST");
submitConnection.setDoOutput(true);
submitConnection.setUseCaches(false);
// creates a unique boundary based on time stamp
String boundary = "===" + System.currentTimeMillis() + "===";
submitConnection.setRequestProperty("Content-Type","multipart/form-data; boundary=" + boundary);
submitConnection.setRequestProperty("User-Agent", "Test Agent");
// Get output stream
DataOutputStream dataStream = new DataOutputStream(submitConnection .getOutputStream());
// Write file input stream to the connection stream
IOUtils.copy(documentInputStream, dataStream);
// Flush
dataStream.flush();
// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(submitConnection.getInputStream()));