在与 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 (将#修改为@)