2011-04-18 11 views
1

서버에서 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(); 
    } 
+0

동일한 기본 스트림에있는 ObjectOutputStream과 DataOutputStream이 어떻게 충돌 할 수 있습니까? –

+0

바이트 배열의 일부를 써내려면 write (byte [] b, int off, int len)를 사용해야합니다. 그것이 그 방법의 목적입니다. – Zeki

답변

1
this.in = new ObjectInputStream(this.socket.getInputStream()); 
this.out = new DataOutputStream(this.socket.getOutputStream()); 
this.outObject = new ObjectOutputStream(this.socket.getOutputStream()); 

당신은 여기 DataOutputStream 정렬을 필요로하지 않으며, 당신이 전과 ObjectOutputStream로 를 작성해야합니다 ObjectInputStream 그렇지 않으면 교착 상태가 발생합니다.

this.res = this.in.readObject().toString();//I read the client order(GET home page, next page...) 

Bzzt. 다음 객체가 String 인 경우이 코드 행은 작동하지만 toString()이 아닌 (String) 캐스트를 사용해야합니다. 다음 객체 이 아니면 문자열을 다른 것으로 손상 시켰을 것입니다.

this.pack = new byte[size_array]; 
this.pack = clean_array();//I replace my variable by an array filled with zeros 

무의미한. (a) 이미 0으로 가득차 있었고 (b) 두 번째 과제를 주장한다면 첫 번째 과제의 요점은 무엇입니까?

코드의 나머지 부분은 오랫동안 잘못되어 있으며 실수로 파일을 소켓에 보내는 방법입니다. 쉬운 방법은 다음과 같습니다.

FileInputStream fin = new FileInputStream(file); 
int count; 
byte[] buffer = new byte[8192]; 
while ((count = fin.read(buffer)) > 0) 
    out.write(buffer, 0, count); // here 'out' is the socket output stream or whatever you want to wrap around it.