Stoffel

Untitled

Jan 15th, 2012
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.05 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. import javax.swing.JOptionPane;
  17.  
  18. /**
  19.  *
  20.  * @author FourFinger
  21.  */
  22. public class FileOntvanger {
  23.     private static int port = 13267;
  24.     private int filesize = 0;
  25.     private String path;
  26.     private String filename = "";
  27.     private String sourceip = "";
  28.     Socket server = null;
  29.     private InputStream in;
  30.     private FileOutputStream fos;
  31.     private BufferedOutputStream bos;
  32.     private int bytesRead;
  33.     private byte[] byteArray;
  34.    
  35.    
  36.    
  37.    
  38.     public FileOntvanger()
  39.     {
  40.        
  41.     }
  42.     public FileOntvanger(String sourcein, int sizein, int port, String pathin, String filenamein)
  43.     {
  44.         System.out.println("FileOntvanger Constructed!");
  45.         this.sourceip = sourcein;
  46.         this.filesize = sizein;
  47.         byteArray = new byte[sizein];
  48.         this.path = pathin;
  49.         filename = filenamein;
  50.         ConnectToFileSender();
  51.        
  52.     }
  53.     private boolean ConnectToFileSender()
  54.     {
  55.         try {
  56.             server = new Socket("192.168.1.116", 1234);
  57.             System.out.println("Connected.");
  58.             in = server.getInputStream();
  59.             int yesorno = JOptionPane.showConfirmDialog(null, "Wilt u het bestand \"" + filename + "\" ontvangen?");
  60.             if(yesorno == JOptionPane.OK_OPTION)
  61.             {
  62.                 fos = new FileOutputStream(new File(path + "\\" + filename));
  63.                 bos = new BufferedOutputStream(fos);
  64.                 bytesRead = 0;
  65.                 bytesRead = in.read(byteArray, 0, byteArray.length);
  66.                 int current = bytesRead;
  67.                 do
  68.                 {
  69.                     System.out.println("Doing something.");
  70.                     bytesRead = in.read(byteArray, current, (byteArray.length-current));
  71.                     if(bytesRead >= 0)
  72.                         current += bytesRead;
  73.                 }
  74.                 while(bytesRead > 0);
  75.                 bos.write(byteArray, 0, current);
  76.                 JOptionPane.showConfirmDialog(null, "De file is succesvol afgeleverd.");
  77.                 bos.flush();
  78.                 bos.close();
  79.                 return true;
  80.             }
  81.             else
  82.             {
  83.                 JOptionPane.showConfirmDialog(null, "De file is niet ontvangen.");
  84.                 return false;
  85.             }
  86.            
  87.            
  88.         } catch (UnknownHostException ex) {
  89.             Logger.getLogger(FileOntvanger.class.getName()).log(Level.SEVERE, null, ex);
  90.         } catch (IOException ex) {
  91.             Logger.getLogger(FileOntvanger.class.getName()).log(Level.SEVERE, null, ex);
  92.         }
  93.         return false;
  94.        
  95.     }
  96.    
  97.    
  98.    
  99.    
  100.    
  101.    
  102. }
Advertisement
Add Comment
Please, Sign In to add comment