import java.awt.*; import javax.swing.*; import java.io.*; import WebFlow.RemoteFile.*; public class ReceiveFile implements Runnable { private boolean debug; private String LocalFileName, RemoteFileName; private JTextField tf; private short blksize=8912; private FileOutputStream fos; public ReceiveFile(String LocalFileName, String RemoteFileName, JTextField tf, boolean debug) { this.LocalFileName = LocalFileName; this.RemoteFileName = RemoteFileName; this.tf = tf; this.debug = debug; // Connection.movefile.Init(Connection.remote); if(debug) System.out.println("ReceiveFile constructor completed"); } public void run(){ File local = new File(LocalFileName); // if(local.canWrite()) { try { try { if(debug) System.out.println("can write "+LocalFileName); fos = new FileOutputStream(local); byte arr[] = null; if(debug) System.out.println("fos opened"); Connection.remote.open(RemoteFileName,"r"); if(debug) System.out.println("remote file opened"); long file_length=Connection.remote.getFileSize(); if(debug) System.out.println("length: "+file_length); long pos = 0; tf.setText(pos+ "bytes uploaded of "+file_length); while(true) { if(debug) System.out.println("read next buffer"); arr = Connection.remote.getBlock(); if(debug) System.out.println("buffer read"); if(arr.length > 0 ) { fos.write(arr); pos += arr.length; tf.setText(pos+ "bytes downloaded of "+file_length); if(debug) System.out.println("got "+pos+" bytes"); } else throw new EOFException(); } } catch(EOFException ex) { if(debug) System.out.println("EOF"); fos.flush(); fos.close(); Connection.remote.close(); tf.setText(LocalFileName+" downloaded"); } catch(IOException ex){ Connection.remote.close(); fos.close(); tf.setText("download failed!"); System.out.println("IOException: "+ex); } } catch (Exception e) {System.out.println("exception: "+e);} } public synchronized void waitFor(long howLong){ try { wait(howLong); } catch (Exception e) { System.out.println(e);} } }