Stoffel

Untitled

Jan 15th, 2012
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.48 KB | None | 0 0
  1. //In de GUI roept den button deze methode aan
  2.     private void sendFile() throws IOException {
  3.         //dialoogvenster openen
  4.         //pad selecteren
  5.         //pad doorgeven aan clienthandler (Sendfile()
  6.         final JFileChooser fc = new JFileChooser();
  7.         int returnVal = fc.showOpenDialog(this);
  8.        
  9.         if(returnVal == JFileChooser.APPROVE_OPTION)
  10.         {
  11.             byte[] bytearray = new byte[(int)fc.getSelectedFile().length()];
  12.             Inet4Address adres =  (Inet4Address) Inet4Address.getLocalHost();
  13.             //cc.SendMessage("MSG " + ontvanger + " SENDFILE " + bytearray.length + " " +  adres.getHostAddress() + " " + 13267);
  14.             cc.SendMessage("MSG " + ontvanger + " SENDFILE " + bytearray.length + " " +  "127.0.0.1" + " " + 13267);
  15.             SendFile sf = new SendFile(fc.getSelectedFile());
  16.         }
  17.     }
  18.  
  19. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  20.  
  21. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  22.  
  23. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  24.  
  25.  
  26. /*
  27.  * To change this template, choose Tools | Templates
  28.  * and open the template in the editor.
  29.  */
  30. package connection;
  31.  
  32. import java.io.BufferedInputStream;
  33. import java.io.File;
  34. import java.io.FileInputStream;
  35. import java.io.IOException;
  36. import java.io.OutputStream;
  37. import java.net.ServerSocket;
  38. import java.net.Socket;
  39. import java.util.logging.Level;
  40. import java.util.logging.Logger;
  41.  
  42. /**
  43.  *
  44.  * @author FourFinger
  45.  */
  46. public class SendFile {
  47.     private ServerSocket server;
  48.     private Socket client;
  49.     private File toSend;
  50.     private FileInputStream fis;
  51.     private BufferedInputStream bis;
  52.     private OutputStream os;
  53.     private byte[] byteArray;
  54.    
  55.    
  56.     public SendFile()
  57.     {
  58.         connect();
  59.     }
  60.     public SendFile(File input)
  61.     {
  62.         System.out.println("Sendfile constructed");System.out.println("Sendfile constructed");System.out.println("Sendfile constructed");System.out.println("Sendfile constructed");System.out.println("Sendfile constructed");System.out.println("Sendfile constructed");System.out.println("Sendfile constructed");System.out.println("Sendfile constructed");System.out.println("Sendfile constructed");System.out.println("Sendfile constructed");System.out.println("Sendfile constructed");System.out.println("Sendfile constructed");System.out.println("Sendfile constructed");System.out.println("Sendfile constructed");System.out.println("Sendfile constructed");System.out.println("Sendfile constructed");System.out.println("Sendfile constructed");System.out.println("Sendfile constructed");System.out.println("Sendfile constructed");System.out.println("Sendfile constructed");System.out.println("Sendfile constructed");System.out.println("Sendfile constructed");System.out.println("Sendfile constructed");System.out.println("Sendfile constructed");System.out.println("Sendfile constructed");System.out.println("Sendfile constructed");System.out.println("Sendfile constructed");System.out.println("Sendfile constructed");System.out.println("Sendfile constructed");System.out.println("Sendfile constructed");System.out.println("Sendfile constructed");
  63.         toSend = input;
  64.         connect();
  65.     }
  66.  
  67.     private boolean connect() {
  68.         try {
  69.             server = new ServerSocket(1234);
  70.            
  71.         while (true) {
  72.             try {
  73.                 client = server.accept();
  74.                 System.out.println("Server accepted connection!");
  75.                 byteArray = new byte [(int)toSend.length()];
  76.                 System.out.println("De grootte van de tekstfile: " + toSend.length());
  77.                 fis = new FileInputStream(toSend);
  78.                 bis = new BufferedInputStream(fis);
  79.                 bis.read(byteArray,0,byteArray.length);
  80.                 os = client.getOutputStream();
  81.                 os.write(byteArray, 0, byteArray.length);
  82.                 System.out.println("Done Sending Master!");
  83.                 os.flush();
  84.                 client.close();
  85.                 return true;
  86.             } catch (IOException ex) {
  87.                 Logger.getLogger(SendFile.class.getName()).log(Level.SEVERE, null, ex);
  88.             }
  89.  
  90.         }            
  91.         } catch (IOException ex) {
  92.             Logger.getLogger(SendFile.class.getName()).log(Level.SEVERE, null, ex);
  93.         }
  94.         return false;
  95.        
  96.  
  97.     }
  98. }
  99.    
  100.    
  101.    
  102. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  103.  
  104. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  105.  
  106. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  107.  
  108.  
  109. /*
  110.  * To change this template, choose Tools | Templates
  111.  * and open the template in the editor.
  112.  */
  113. package connection;
  114.  
  115. import java.io.BufferedOutputStream;
  116. import java.io.File;
  117. import java.io.FileOutputStream;
  118. import java.io.IOException;
  119. import java.io.InputStream;
  120. import java.net.Socket;
  121. import java.net.UnknownHostException;
  122. import java.util.logging.Level;
  123. import java.util.logging.Logger;
  124.  
  125. /**
  126.  *
  127.  * @author FourFinger
  128.  */
  129. public class FileOntvanger {
  130.     private static int port = 13267;
  131.     private int filesize = 0;
  132.     private String sourceip = "";
  133.     Socket server = null;
  134.     private InputStream in;
  135.     private FileOutputStream fos;
  136.     private BufferedOutputStream bos;
  137.     private int bytesRead;
  138.     private byte[] byteArray;
  139.    
  140.    
  141.    
  142.    
  143.     public FileOntvanger()
  144.     {
  145.        
  146.     }
  147.     public FileOntvanger(String sourcein, int sizein, int port)
  148.     {
  149.         System.out.println("FileOntvanger Constructed!");
  150.         this.sourceip = sourcein;
  151.         this.filesize = sizein;
  152.         byteArray = new byte[sizein];
  153.         ConnectToFileSender();
  154.     }
  155.     private boolean ConnectToFileSender()
  156.     {
  157.         try {
  158.             server = new Socket(sourceip, 1234);
  159.             System.out.println("Connected.");
  160.             in = server.getInputStream();
  161.             fos = new FileOutputStream(new File("abc.txt"));
  162.             bos = new BufferedOutputStream(fos);
  163.             bytesRead = 0;
  164.             bytesRead = in.read(byteArray, 0, byteArray.length);
  165.             int current = bytesRead;
  166.             do
  167.             {
  168.                 System.out.println("Doing something.");
  169.                 bytesRead = in.read(byteArray, current, (byteArray.length-current));
  170.                 if(bytesRead >= 0)
  171.                     current += bytesRead;
  172.             }
  173.             while(bytesRead > 0);
  174.             bos.write(byteArray, 0, current);
  175.             System.out.println("Done Receiving Master!");
  176.             bos.flush();
  177.             bos.close();
  178.             return true;
  179.            
  180.         } catch (UnknownHostException ex) {
  181.             Logger.getLogger(FileOntvanger.class.getName()).log(Level.SEVERE, null, ex);
  182.         } catch (IOException ex) {
  183.             Logger.getLogger(FileOntvanger.class.getName()).log(Level.SEVERE, null, ex);
  184.         }
  185.         return false;
  186.        
  187.     }
  188.    
  189.    
  190.    
  191.    
  192.    
  193.    
  194. }
Advertisement
Add Comment
Please, Sign In to add comment