서버에서 pdf 파일을 다운로드 할 수있는 응용 프로그램을 프로그래밍 중입니다. 내 서버 측에서는 pdfview 라이브러리 덕분에 내 pdf 파일의 bytebuffer를 얻을 수 있습니다. 바이트 배열을 내 바이트 버퍼로 채운 다음 DataOutputStream을 사용하여 바이트 배열을 보냅니다.Java 소켓으로 바이트 배열 보내기
대부분 클라이언트 측에서는 좋은 데이터를 얻지 만, 때로는 임의의 숫자로 채워진 배열이 생겨서 PDF 파일을 다시 작성할 수 없습니다. 보통 다음과 같은 오류가 있습니다 : "java.io.IOException : 이것은 PDF 파일이 아닐 수 있습니다"
그래서받은 데이터와 보낸 데이터를 비교할 때 완전히 다릅니다. 내가 할 수 서버 부분에 데이터가 항상 올바른 것으로 나타났습니다
어떤 도움에 감사드립니다//Server side
this.in = new ObjectInputStream(this.socket.getInputStream());
this.out = new DataOutputStream(this.socket.getOutputStream());
this.outObject = new ObjectOutputStream(this.socket.getOutputStream());
this.res = this.in.readObject().toString();//I read the client order(GET home page, next page...)
//I get the bytebuffer from the pdf file------
this.file = new File (name+this.numFile+".pdf");
RandomAccessFile raf;
raf = new RandomAccessFile(this.file, "r");
FileChannel channel = raf.getChannel();
this.buf = channel.map(FileChannel.MapMode.READ_ONLY,0, channel.size());
//--------------------------------------------
int size = this.buf.capacity();
this.out.writeInt(size);//I send the size of my bytebuffer to the server
int size_array = 1000;
this.pack = new byte[size_array];
this.pack = clean_array();//I replace my variable by an array filled with zeros
for(long i=0;i<(size/size_array);i++){
buf.get(this.pack);
this.out.write(this.pack);
this.pack = clean_array();//I replace my variable by an array filled with zeros
}
//I have not sent the whole bytebuffer, the last byte array could have a different size
//I work out this size, I create the new bytearray and I send it---------------------
int byteLeft = size%size_array;
if(byteLeft>0){
this.pack = new byte[byteLeft];
buf.get(this.pack);
this.out.write(this.pack);
this.pack = clean_array();//I replace my variable by an array filled with zeros
}
//-------------------------------------------------
//Client side
int size_array = 1000;
pack =new byte[size_array];
pack = clean_array();
for(int i=0;i<((size/size_array));i++){
in.read(pack);
buf.put(pack);
pack = clean_array();
}
if(size%size_array>0){
//for the last loop, the number of bytes sent by the server is not equal to 1000
//So I create a byte array with the good size
pack = new byte[size%size_array];
in.read(pack);
buf.put(pack);
pack = clean_array();
}
동일한 기본 스트림에있는 ObjectOutputStream과 DataOutputStream이 어떻게 충돌 할 수 있습니까? –
바이트 배열의 일부를 써내려면 write (byte [] b, int off, int len)를 사용해야합니다. 그것이 그 방법의 목적입니다. – Zeki