public void receiveFile ( final File file ) throws SmackException , IOException { if ( file == null ) { throw new IllegalArgumentException ( "File cannot be null" ) ; } if ( ! file . exists ( ) ) { file . createNewFile ( ) ; } if ( ! file . canWrite ( ) ) { throw new IllegalArgumentException ( "Cannot write to provided file" ) ; } Thread transferThread = new Thread ( new Runnable ( ) { @ Override public void run ( ) { try { inputStream = negotiateStream ( ) ; } catch ( Exception e ) { setStatus ( FileTransfer . Status . error ) ; setException ( e ) ; return ; } OutputStream outputStream = null ; try { outputStream = new FileOutputStream ( file ) ; setStatus ( Status . in_progress ) ; writeToStream ( inputStream , outputStream ) ; } catch ( FileNotFoundException e ) { setStatus ( Status . error ) ; setError ( Error . bad_file ) ; setException ( e ) ; } catch ( IOException e ) { setStatus ( Status . error ) ; setError ( Error . stream ) ; setException ( e ) ; } if ( getStatus ( ) . equals ( Status . in_progress ) ) { setStatus ( Status . complete ) ; } CloseableUtil . maybeClose ( inputStream , LOGGER ) ; CloseableUtil . maybeClose ( outputStream , LOGGER ) ; } } , "File Transfer " + streamID ) ; transferThread . start ( ) ; }
This method negotiates the stream and then transfer s the file over the negotiated stream . The transferred file will be saved at the provided location .