Stoffel

Untitled

Jan 15th, 2012
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.13 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package connection;
  6.  
  7. import java.io.BufferedOutputStream;
  8. import java.io.File;
  9. import java.io.FileOutputStream;
  10. import java.io.IOException;
  11. import java.io.InputStream;
  12. import java.net.Socket;
  13. import java.net.UnknownHostException;
  14. import java.util.logging.Level;
  15. import java.util.logging.Logger;
  16.  
  17. /**
  18.  *
  19.  * @author FourFinger
  20.  */
  21. public class FileOntvanger {
  22.     private static int port = 13267;
  23.     private int filesize = 0;
  24.     private String sourceip = "";
  25.     Socket server = null;
  26.     private InputStream in;
  27.     private FileOutputStream fos;
  28.     private BufferedOutputStream bos;
  29.     private int bytesRead;
  30.     private byte[] byteArray;
  31.    
  32.    
  33.    
  34.    
  35.     public FileOntvanger()
  36.     {
  37.        
  38.     }
  39.     public FileOntvanger(String sourcein, int sizein, int port)
  40.     {
  41.         this.sourceip = sourcein;
  42.         this.filesize = sizein;
  43.         byteArray = new byte[sizein];
  44.         ConnectToFileSender();
  45.     }
  46.     private boolean ConnectToFileSender()
  47.     {
  48.         try {
  49.             server = new Socket(sourceip, 1234);
  50.             in = server.getInputStream();
  51.             fos = new FileOutputStream(new File("abc.txt"));
  52.             bos = new BufferedOutputStream(fos);
  53.             bytesRead = 0;
  54.             bytesRead = in.read(byteArray, 0, byteArray.length);
  55.             int current = bytesRead;
  56.             do
  57.             {
  58.                 bytesRead = in.read(byteArray, current, (byteArray.length-current));
  59.                 if(bytesRead >= 0) current += bytesRead;
  60.             }while(bytesRead > -1);
  61.            
  62.             bos.write(byteArray, 0, current);
  63.             bos.flush();
  64.             bos.close();
  65.             return true;
  66.            
  67.         } catch (UnknownHostException ex) {
  68.             Logger.getLogger(FileOntvanger.class.getName()).log(Level.SEVERE, null, ex);
  69.         } catch (IOException ex) {
  70.             Logger.getLogger(FileOntvanger.class.getName()).log(Level.SEVERE, null, ex);
  71.         }
  72.         return false;
  73.        
  74.     }
  75.    
  76.    
  77.    
  78.    
  79.    
  80.    
  81. }
Advertisement
Add Comment
Please, Sign In to add comment