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.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;
- import javax.swing.JOptionPane;
- /**
- *
- * @author FourFinger
- */
- public class FileOntvanger {
- private static int port = 13267;
- private int filesize = 0;
- private String path;
- private String filename = "";
- 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, String pathin, String filenamein)
- {
- System.out.println("FileOntvanger Constructed!");
- this.sourceip = sourcein;
- this.filesize = sizein;
- byteArray = new byte[sizein];
- this.path = pathin;
- filename = filenamein;
- ConnectToFileSender();
- }
- private boolean ConnectToFileSender()
- {
- try {
- server = new Socket("192.168.1.116", 1234);
- System.out.println("Connected.");
- in = server.getInputStream();
- int yesorno = JOptionPane.showConfirmDialog(null, "Wilt u het bestand \"" + filename + "\" ontvangen?");
- if(yesorno == JOptionPane.OK_OPTION)
- {
- fos = new FileOutputStream(new File(path + "\\" + filename));
- 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);
- JOptionPane.showConfirmDialog(null, "De file is succesvol afgeleverd.");
- bos.flush();
- bos.close();
- return true;
- }
- else
- {
- JOptionPane.showConfirmDialog(null, "De file is niet ontvangen.");
- return false;
- }
- } 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