使用HttpURLConnection.setChunkedStreamingMode()获取IOException

时间:2013-11-29 01:40:14

标签: http file-upload io httpurlconnection java-io

我写过HttpUrlConnection包装器。它支持文件上传,所以我使用HttpUrlConnection.setChunkedStreamingMode()来防止OutOfMemoryException。

但是在将所有数据写入OutputStream后,我得到了IOException。没有这种方法,一切正常。

这里是代码:

HttpURLConnection connection = getConnection();
    if(!isSent) {
        OutputStream output;
        if(!files.isEmpty()) {
            connection.setRequestProperty(HEADER_CONTENT_TYPE, CONTENT_TYPE_MULTIPART);
            // Tried to add this line, but still not working
            connection.setRequestProperty("Transfer-Encoding","chunked");
            connection.setChunkedStreamingMode(chunkSize);
            output = new BufferedOutputStream(connection.getOutputStream());
            writeParamsMultipart(output);
            writeFilesMultipart(output);
            output.write((PREFIX + BOUNDARY + PREFIX + LINEND).getBytes());
        } else {
            connection.setRequestProperty(HEADER_CONTENT_TYPE, CONTENT_TYPE_FORM);
            output = new BufferedOutputStream(connection.getOutputStream());
            output.write(paramsToUrlencoded(post).getBytes());
        }
        output.flush();
        output.close();
        isSent = true;
    }
    // Getting exception on this line
    InputStream input = new BufferedInputStream(connection.getInputStream());
    if(ENCODING_GZIP.equalsIgnoreCase(connection.getHeaderField(HEADER_CONTENT_ENCODING))) {
        input = new GZIPInputStream(input);
    }
    return input;

chunkSize默认为1024字节。有什么问题?

1 个答案:

答案 0 :(得分:-2)

  

它是FileNotFoundException

这意味着网址不正确。它相当于HTTP 403。

与分块传输模式无关。