Java已知InputStream長度,將其轉換為byte[]

鍾超發表於2011-11-18
/**
	 * Transfer java.io.InpuStream to byte array.
	 * @param inStream, input stream of the uploaded file.
	 * @param fileLength, the length of the file.
	 * @return the byte array transferred from java.io.Inputstream.
	 * @throws IOException occurred by the method read(byte[]) of java.io.InputStream.
	 */
		private byte[] getFileBuffer(InputStream inStream, long fileLength) throws IOException {
		
		byte[] buffer = new byte[256 * 1024];
		byte[] fileBuffer = new byte[(int) fileLength];
	
		int count = 0;
		int length = 0;
    
		while((length = inStream.read(buffer)) != -1){
			for (int i = 0; i < length; ++i)
		    {
		        fileBuffer[count + i] = buffer[i];
		    }
		    count += length;
		}
		return fileBuffer;
	}

相關文章