微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在与 Azure Blob 存储的连接上设置 setChunkedStreamingMode(0)

如何解决在与 Azure Blob 存储的连接上设置 setChunkedStreamingMode(0)

在将 blob 操作放入 Azure blob 存储时,如果我设置 HttpURLConnection.setChunkedStreamingMode(0) 会抛出 IOException 并显示消息“未指定此请求所必需的 HTTP 标头”。到 Azure 的输出流在 Wrapper 类撰写本文时已被加密,因此我无法获得内容长度。如果我没有设置 chunkedStreamMod 或 FixedLengthStreamingMod,连接会抛出 java.lang.OutOfMemoryError: Java heap space....

public void uploadBlob(InputStream inputStream,OutputStream outputStream,HttpURLConnection connection) throws IOException {

    HttpURLConnection connection = getCreateResourceConnection(blobPath);
    OutputStream outputStream = getCreateResourceOutputStream(HOT_TIER,connection);
    OutputStream encryptedOS = getEncryptedOutputStream(outputStr)

    // If I write to this encryptedOS by 
    // connection.setChunkedStreamingMode(0) set,an IOException with "An 
    // HTTP header that's mandatory for this request is not specified." 
    // message is thrown,otherwise the operation is successful.
}

private HttpURLConnection getCreateResourceConnection(String blobPath,long size) throws KeyManagementException,NoSuchAlgorithmException,IOException {

    URL url = getoperationsOnBlobUrl(blobPath);
    Proxy proxy = Proxy.NO_PROXY;
    HttpURLConnection connection = (HttpURLConnection)         
        url.openConnection(proxy);
    connection.setConnectTimeout((int)TimeUnit.SECONDS.toMillis(60));
    connection.setReadTimeout((int)TimeUnit.SECONDS.toMillis(60));
    connection.setChunkedStreamingMode(0);
}

public OutputStream getCreateResourceOutputStream(String accesstier,HttpURLConnection connection) throws IOException {

    OutputStream outputStream = null;
    connection.setRequestMethod("PUT");
    connection.setDoOutput(true);
    connection.setRequestProperty("Host",mSASURL.getHost());
    connection.setRequestProperty("x-ms-blob-type","BlockBlob");
    connection.setRequestProperty("x-ms-access-tier",accesstier);

    outputStream = connection.getoutputStream();

    return outputStream;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。