Java setFixedLengthStreamingMode不适用于对s3的http PUT请求

Java setFixedLengthStreamingMode不适用于对s3的http PUT请求,java,connection,httpurlconnection,Java,Connection,Httpurlconnection,我正在尝试使用HttpURLConnection对S3URL执行http PUT请求。 但由于某些原因,SetFixedLength StreamingMode对我不起作用。有人能帮忙吗 HttpURLConnection connection = (HttpURLConnection) new URL(uploadLocation).openConnection(); connection.setRequestProperty("x-amz-server-side-encryption", C

我正在尝试使用HttpURLConnection对S3URL执行http PUT请求。 但由于某些原因,SetFixedLength StreamingMode对我不起作用。有人能帮忙吗

HttpURLConnection connection = (HttpURLConnection) new URL(uploadLocation).openConnection();
connection.setRequestProperty("x-amz-server-side-encryption", CommonConstants.AMZ_SERVER_SIDE_ENCRYPTION);
connection.setDoOutput(true);
connection.setRequestMethod("PUT");
connection.setFixedLengthStreamingMode((int) new File(localFilePath).length());
最后一句话什么也没做。如果执行
连接。getContentLength()
,则显示-1。有人能帮忙吗

HttpURLConnection connection = (HttpURLConnection) new URL(uploadLocation).openConnection();
connection.setRequestProperty("x-amz-server-side-encryption", CommonConstants.AMZ_SERVER_SIDE_ENCRYPTION);
connection.setDoOutput(true);
connection.setRequestMethod("PUT");
connection.setFixedLengthStreamingMode((int) new File(localFilePath).length());
编辑: 我实际上是在尝试这样做:但它不起作用。获取java io异常:流已关闭

    BufferedOutputStream out = new BufferedOutputStream(connection.getOutputStream());
    FileInputStream input = new FileInputStream(localFilePath);
    int length;
    while ((length = input.read(buffer)) > 0) {
      out.write(buffer, 0, length);
    }
    input.close();
    out.close();

HttpURLConnection.getContentLength()
返回响应的长度,而不是请求的长度。

HttpURLConnection.getContentLength()
返回响应的长度,不属于请求。

不要将
缓冲输出流
设置固定长度流模式
一起使用,因为在固定长度模式下,所有主体必须在请求之前写入输出流。缓冲可能会阻止它


要修复代码,可以直接写入
OutputStream out=connection.getOutputStream()。或者不要调用
setFixedLengthStreamingMode

不要将
BufferedOutputStream
setFixedLengthStreamingMode
一起使用,因为在固定长度模式下,所有主体都必须在请求之前写入输出流。缓冲可能会阻止它


要修复代码,可以直接写入
OutputStream out=connection.getOutputStream()。或者,即使在调试器中也不要调用
setFixedLengthStreamingMode

,我在请求中没有看到setFixedLengthStreamingMode设置fixedContentLength属性。但是,在发送数据时,它是否起作用?你想解决什么实际问题?编辑问题。我正在获取流关闭错误。即使在调试器中,我也没有在请求中看到setFixedLengthStreamingMode设置fixedContentLength属性。但在发送数据时它是否有效?你想解决什么实际问题?编辑问题。我收到流关闭错误。重新编辑,堆栈跟踪是必需的,但这确实是一个新问题。重新编辑,堆栈跟踪是必需的,但这确实是一个新问题。