2012-05-07 117 views
-1
private static void fileChunking() throws IOException { 
    InputStream is = null; 
    OutputStream os = null; 
    byte[] buffer; 
    int maxBufferSize = 102400; 
    int bytesRead, bytesAvailable, bufferSize; 
    int responsecode = 0; 
    String responsemessage = null; 
    bufferSize = Math.min(content.length, maxBufferSize); 
    buffer = new byte[bufferSize]; 
    bytesRead = is.read(buffer, 0, bufferSize); 
    while (bytesRead > 0) { 
     os.write(buffer, 0, bufferSize); 
     bytesAvailable = is.available(); 
     bufferSize = Math.min(bytesAvailable, maxBufferSize); 
     bytesRead = is.read(buffer, 0, bufferSize); 
    } 

    is.close(); 
    os.flush(); 
    responsecode = con.getResponseCode(); 
    responsemessage = con.getResponseMessage(); 
    System.out.print(responsemessage); 

} 

代碼在讀取時拋出異常'流已打開' bytesRead = is.read(buffer,0,bufferSize);流已經打開 - 例外

+1

該代碼無法執行,因爲您從未將「is」設置爲非null。什麼是真正的代碼?注意你不需要那個'Math.min()'東西:'read()'已經有這樣的表現。 – EJP

+0

該代碼無法編譯,因爲沒有聲明名稱爲「content」的變量。 –

回答

1

您可能已經在其他地方叫getReader(),它與getInputStream()互斥。很難從這段代碼中說出來,這個答案有點不太可能。

更有可能從servlet字段或某個公共變量中獲取InputStream,並且有兩個請求會讀取。