输入流到输出流

时间:2018-10-31 10:44:23

标签: java

我将文件发布到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()));

0 个答案:

没有答案