Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //In de GUI roept den button deze methode aan
- private void sendFile() throws IOException {
- //dialoogvenster openen
- //pad selecteren
- //pad doorgeven aan clienthandler (Sendfile()
- final JFileChooser fc = new JFileChooser();
- int returnVal = fc.showOpenDialog(this);
- if(returnVal == JFileChooser.APPROVE_OPTION)
- {
- byte[] bytearray = new byte[(int)fc.getSelectedFile().length()];
- Inet4Address adres = (Inet4Address) Inet4Address.getLocalHost();
- //cc.SendMessage("MSG " + ontvanger + " SENDFILE " + bytearray.length + " " + adres.getHostAddress() + " " + 13267);
- cc.SendMessage("MSG " + ontvanger + " SENDFILE " + bytearray.length + " " + "127.0.0.1" + " " + 13267);
- SendFile sf = new SendFile(fc.getSelectedFile());
- }
- }
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- /*
- * 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)
- {
- 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");
- toSend = input;
- connect();
- }
- private boolean connect() {
- try {
- server = new ServerSocket(1234);
- while (true) {
- try {
- client = server.accept();
- System.out.println("Server accepted connection!");
- byteArray = new byte [(int)toSend.length()];
- System.out.println("De grootte van de tekstfile: " + toSend.length());
- fis = new FileInputStream(toSend);
- bis = new BufferedInputStream(fis);
- bis.read(byteArray,0,byteArray.length);
- os = client.getOutputStream();
- os.write(byteArray, 0, byteArray.length);
- System.out.println("Done Sending Master!");
- os.flush();
- client.close();
- return 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);
- }
- return false;
- }
- }
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package connection;
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.net.Socket;
- import java.net.UnknownHostException;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- *
- * @author FourFinger
- */
- public class FileOntvanger {
- private static int port = 13267;
- private int filesize = 0;
- private String sourceip = "";
- Socket server = null;
- private InputStream in;
- private FileOutputStream fos;
- private BufferedOutputStream bos;
- private int bytesRead;
- private byte[] byteArray;
- public FileOntvanger()
- {
- }
- public FileOntvanger(String sourcein, int sizein, int port)
- {
- System.out.println("FileOntvanger Constructed!");
- this.sourceip = sourcein;
- this.filesize = sizein;
- byteArray = new byte[sizein];
- ConnectToFileSender();
- }
- private boolean ConnectToFileSender()
- {
- try {
- server = new Socket(sourceip, 1234);
- System.out.println("Connected.");
- in = server.getInputStream();
- fos = new FileOutputStream(new File("abc.txt"));
- bos = new BufferedOutputStream(fos);
- bytesRead = 0;
- bytesRead = in.read(byteArray, 0, byteArray.length);
- int current = bytesRead;
- do
- {
- System.out.println("Doing something.");
- bytesRead = in.read(byteArray, current, (byteArray.length-current));
- if(bytesRead >= 0)
- current += bytesRead;
- }
- while(bytesRead > 0);
- bos.write(byteArray, 0, current);
- System.out.println("Done Receiving Master!");
- bos.flush();
- bos.close();
- return true;
- } catch (UnknownHostException ex) {
- Logger.getLogger(FileOntvanger.class.getName()).log(Level.SEVERE, null, ex);
- } catch (IOException ex) {
- Logger.getLogger(FileOntvanger.class.getName()).log(Level.SEVERE, null, ex);
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment