我正在做一个项目,我需要将一个大的 pdf 文件(10 兆位)发送到另一台计算机,但首先我需要分成 32000 位的较小字节数组以适合数据包。然后另一台计算机将需要重新制作文件。我已经设法打破它,但我很难重新创建它。
这就是我到目前为止所做的:
class Example {
public static byte [] buffer = new byte [8];
public static byte [] ArrayCheck = { 0,0,0,0,0,0,0,0};
public static int count =0;
public static void main (String [] args) throws Exception{
int n = 32000;
int temp = 4000;
int counter =0;
byte [] chunk = new byte [n] ;
System.out.println("it worked" +chunk.length);
RandomAccessFile f = new RandomAccessFile("SimpleExample.pdf", "r");
byte[] b = new byte[(int)f.length()];
f.read(b);
System.out.println("it worked" +b.length);
for (int pos = 0; pos < b.length; pos+=n ) {
if ((pos+n)>b.length){
temp =(((pos+n)-b.length)/8+((pos+n)-b.length)%8);
System.arraycopy(b, pos, chunk , 0, temp); // this is the last packets
CreateFile(chunk);
}else {
System.arraycopy(b, pos, chunk , 0, temp);
CreateFile(chunk);
}
counter ++;
}
System.out.println(counter);
}
public static void CreateFile (byte [] data ) throws Exception{
FileOutputStream sigfil = new FileOutputStream("example.pdf", true);
System.arraycopy(data, 3998, buffer , 0, 1);
sigfil.write(data);
if (Arrays.equals(buffer,ArrayCheck)) {
sigfil.close();
count++;
System.out.println(count);
}
}
}
我怎么做?