Advertisement
wieruowq

Sending file size or file (part of ServerRecieve.java)

Nov 1st, 2017
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.70 KB | None | 0 0
  1.                     String[] parts = response.split("`");
  2.                     String part1 = parts[0];
  3.                     //part1 contains file name....now send the file or file size
  4.  
  5.                     File file= new File(part1);
  6.                     System.out.println("File exists: " +  file.exists());
  7.                     System.out.println("File can be read: " +  file.canRead());
  8.                     System.out.println("File file.canWrite(): " +  file.canWrite());
  9.                     if ( file.exists() && file.canRead() )
  10.                     {
  11.                         // return the number of bytes in the file as a long int
  12.                         long file_size= file.length();
  13.                         System.out.println("File size is: " + file_size);
  14.                         if ( file_size > 0 )
  15.                         {
  16.                             System.out.println("432211");
  17.                             output.writeLong( file_size );
  18.                             System.out.println("File size sent.");
  19.                         }
  20.  
  21.                         else
  22.                         {
  23.                             output.writeLong( 0L );
  24.  
  25.                         }
  26.                     }
  27.                     else
  28.                     {
  29.                         output.writeLong( 0L );        
  30.  
  31.                     }
  32.                     FileInputStream file_input= new FileInputStream(file);
  33.                     System.out.println( "Transmitting file: " + part1);
  34.                     byte[] file_buffer= new byte[1500];
  35.                     int number_read;
  36.                     while( (number_read= file_input.read( file_buffer )) != -1 )
  37.                         output.write( file_buffer, 0, number_read );
  38.                     System.out.println("File " + part1 + " sent with " + number_read + " bytes read.");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement