2012-12-13 2 views
0

파일이 12MB보다 클 경우 클라이언트 - 서버간에 파일을 전송하려고 시도하는데 그렇지 않은 경우 블록이 전송합니다. 그렇지 않으면 정상적으로 전송됩니다. 내 주요 문제는 매번 내가 원래의 것보다 더 많은 바이트를 가져 오는 뭔가를 전송하는 것입니다 그리고 내가 그것을 완료 한 후 다이제스트 메시지를 사용해야하므로 그것은 작동하지 않을 것이다. 그리고 다른 하나는 파일을 보내려고 할 때이다. 네트워크가 클라이언트가 파일을 읽으면 서버가 파일을 읽는 것보다 빨리 보내서 클라이언트가 프로그램을 닫아 연결을 닫으면 파일이 손상됩니다. 내 전송 코드는 우는 소리입니다 :소켓을 통해 클라이언트 - 서버를 전송하는 자바

if(fSize>maxfileSize){ 
     totbLidos = 0; 
     byte[] fBytes = new byte[fBsize]; 
     while(totbLidos < fSize){ 
      int bRemain = (int) f.length() - totbLidos; 
      if(bRemain < fBsize){ 
       fBsize = bRemain; 
      } 
      int bRead = tFile.read(fBytes, 0, fBsize); 
      tServidor.write(fBytes, 0, fBsize); 
      tServidor.flush(); 
      if(bRead>0){ 
       totbLidos+=bRead; 
      } 

      System.out.println("Total Bytes Lidos: " + totbLidos); 

     } 

     tFile.close(); 
     System.out.println("Ficheiro enviado"); 
     cliente.close(); 
    } 
    else{ 
     totbLidos = 0; 
     byte[] fBytes = new byte[fSize]; 
     while(totbLidos < fSize){ 
      int bRead = tFile.read(fBytes,0,fSize); 
      if(bRead>0){ 
       totbLidos+=bRead; 
      } 
      tServidor.write(fBytes, 0, fSize); 
      System.out.println("Total Bytes Lidos: " + totbLidos); 
      tServidor.flush(); 
     } 
     tFile.close(); 
     System.out.println("Ficheiro enviado"); 
     cliente.close(); 
    } 
} 

서버 전송 코드 : 당신은 당신이 여기 읽은 바이트 같은 번호를 작성하지 않는

if(fSize > maxfileSize){ 
      totbLidos = 0; 
      DataInputStream tFile = new DataInputStream(cliente.getInputStream()); 
      BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(fName)); 
      byte[] fBytes = new byte[fBsize]; 
      while(totbLidos < fSize){ 
       int bRemain = size - totbLidos; 
       if(bRemain < fBsize){ 
        fBsize = bRemain; 
       } 
       int bRead = tFile.read(fBytes, 0, fBsize); 
       fos.write(fBytes); 
       fos.flush(); 
       if(bRead>0){ 
        totbLidos+=bRead; 
       } 
       System.out.println("Bytes lidos: " + bRead); 
       System.out.println("Total Bytes Escritos: " + totbLidos); 
      } 
      System.out.println("Ficheiro recebido"); 

      fos.close(); 
      tFile.close(); 
      cliente.close(); 
      servidor.close(); 
     } 
     else if(fSize < maxfileSize){ 
      totbLidos = 0; 
      DataInputStream tFile = new DataInputStream(cliente.getInputStream()); 
      BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(fName)); 
      byte[] fBytes = new byte[fSize]; 
      while(totbLidos < fSize){ 
       int bRead = tFile.read(fBytes,0,fSize); 
       fos.write(fBytes); 
       fos.flush(); 
       if(bRead>0){ 
        totbLidos+=bRead; 
       } 
       System.out.println("Total Bytes Escritos: " + totbLidos); 
      } 
      System.out.println("Ficheiro recebido"); 
      fos.close(); 
      tFile.close(); 
      cliente.close(); 
      servidor.close(); 
     } 
} 

답변

2

이 클라이언트 전송 코드입니다.

fos.write(fBytes); 

시도 @ 피터 Lawrey의 대답에 추가

fos.write(fBytes, 0, bRead); 
+0

감사 바이트 문제 –

0

를 사용하여 당신이 미래에 버그의이 종류를 방지하기 위해 아파치 코 몬즈 IO API를 다시 사용하는 것이 좋습니다 : 해결

IOUtils.copyLarge(InputStream input, OutputStream output, long inputOffset, long length) 
관련 문제