Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package connection;
- import java.io.BufferedInputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.OutputStream;
- import java.net.ServerSocket;
- import java.net.Socket;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- *
- * @author FourFinger
- */
- public class SendFile {
- private ServerSocket server;
- private Socket client;
- private File toSend;
- private FileInputStream fis;
- private BufferedInputStream bis;
- private OutputStream os;
- private byte[] byteArray;
- public SendFile()
- {
- connect();
- }
- public SendFile(File input)
- {
- connect();
- toSend = input;
- }
- private void connect() {
- try {
- server = new ServerSocket(1234);
- boolean connected = false;
- while (connected = false) {
- try {
- client = server.accept();
- System.out.println("Server accepted connection!");
- byteArray = new byte [(int)toSend.length()];
- fis = new FileInputStream(toSend);
- bis = new BufferedInputStream(fis);
- bis.read(byteArray,0,byteArray.length);
- os.write(byteArray, 0, byteArray.length);
- os.flush();
- client.close();
- connected = true;
- } catch (IOException ex) {
- Logger.getLogger(SendFile.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- } catch (IOException ex) {
- Logger.getLogger(SendFile.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment