Stoffel

Untitled

Jan 15th, 2012
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.83 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.BufferedInputStream;
  8. import java.io.File;
  9. import java.io.FileInputStream;
  10. import java.io.IOException;
  11. import java.io.OutputStream;
  12. import java.net.ServerSocket;
  13. import java.net.Socket;
  14. import java.util.logging.Level;
  15. import java.util.logging.Logger;
  16.  
  17. /**
  18.  *
  19.  * @author FourFinger
  20.  */
  21. public class SendFile {
  22.     private ServerSocket server;
  23.     private Socket client;
  24.     private File toSend;
  25.     private FileInputStream fis;
  26.     private BufferedInputStream bis;
  27.     private OutputStream os;
  28.     private byte[] byteArray;
  29.    
  30.    
  31.     public SendFile()
  32.     {
  33.         connect();
  34.     }
  35.     public SendFile(File input)
  36.     {
  37.         connect();
  38.         toSend = input;
  39.     }
  40.  
  41.     private void connect() {
  42.         try {
  43.             server = new ServerSocket(1234);
  44.            
  45.         boolean connected = false;
  46.         while (connected = false) {
  47.             try {
  48.                 client = server.accept();
  49.                 System.out.println("Server accepted connection!");
  50.                 byteArray = new byte [(int)toSend.length()];
  51.                 fis = new FileInputStream(toSend);
  52.                 bis = new BufferedInputStream(fis);
  53.                 bis.read(byteArray,0,byteArray.length);
  54.                 os.write(byteArray, 0, byteArray.length);
  55.                 os.flush();
  56.                 client.close();
  57.                 connected = true;
  58.             } catch (IOException ex) {
  59.                 Logger.getLogger(SendFile.class.getName()).log(Level.SEVERE, null, ex);
  60.             }
  61.  
  62.         }            
  63.         } catch (IOException ex) {
  64.             Logger.getLogger(SendFile.class.getName()).log(Level.SEVERE, null, ex);
  65.         }
  66.        
  67.  
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment