图片传输(文件内)分两次传输。

package newyears;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.RandomAccessFile;

public class Demo5 {
	public static void main(String[] args) {
		try {

			RandomAccessFile ra = new RandomAccessFile("C:\\Users\\mcfeng\\Videos\\Captures\\your baby.mp4", "rw");
			OutputStream out = new FileOutputStream("D:\\sub.mp4");

			byte[] b = new byte[1024];//字节传输

			int len = 20 * 1024 * 1024;//第一次20M
			int len1 = 0;
			int bb = 0;
			long aa = ra.length() - len;//第二次余下的
			ra.seek(0);
			len1 = ra.read(b);
			while (len1 > 0 && bb < len) {//先判断有内容没并且小于20M
				bb += len1;
				out.write(b, 0, len1);
				len1 = ra.read(b);
			}

			ra.seek(len);//从20M后读取
			byte[] c = new byte[1024];
			len1 = ra.read(c);
			while (aa > 0 && len1 >= 0) {//判断余下的是否传输完
				aa -= 1024;
				out.write(c, 0, len1);
				len1 = ra.read(c);
				out.close();
			    ra.close();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}

你可能感兴趣的:(Java,基础)