Java分块传输的思路

Java分块传输的思路

假设块大小为BLOCK_SIZE

1.读取大小为BLOCK_SIZE的源文件

2.将数据传输过去

3.再读大小为BLOCK_SIZE的源文件

 

            int MAX_USBFS_BUFFER_SIZE = 2; 

            byte[] bytes = new byte[MAX_USBFS_BUFFER_SIZE];

            BufferedInputStream bufferInputStream = new BufferedInputStream(new FileInputStream("/sdcard/HelloWorld"));

            int bytesRead = 0;

            //从文件中按字节读取内容,到文件尾部时read方法将返回-1

            while ((bytesRead = bufferInputStream.read(bytes)) != -1) {

 

                //将读取的字节转为字符串对象

                String chunk = new String(bytes, 0, bytesRead);

                Log.i("LOG_TAG", chunk);

            }

 比较好的一个:

		    int MAX_USBFS_BUFFER_SIZE = 4096; 
	            byte[] bytes = new byte[MAX_USBFS_BUFFER_SIZE];
	            BufferedInputStream bufferInputStream = new BufferedInputStream(new FileInputStream(rasterPath));
	            int i = 0;
	            while (bufferInputStream.available() > 0) {
	            	Log.i(LOG_TAG, "bufferInputStream.available():" + bufferInputStream.available());
	            	// memset, there will be some ASCII at the end of byte[]
	            	// printer will print it out so need memset
	            	Arrays.fill( bytes, (byte) 0 );
	            	bufferInputStream.read(bytes, i*MAX_USBFS_BUFFER_SIZE, MAX_USBFS_BUFFER_SIZE);
	            	mIPrintService.write(bytes, 0);
	            }



参考:java使用BufferedInputStream读取文本文件

你可能感兴趣的:(Java分块传输的思路)