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;
- /**
- *
- * @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)
- {
- this.sourceip = sourcein;
- this.filesize = sizein;
- byteArray = new byte[sizein];
- ConnectToFileSender();
- }
- private boolean ConnectToFileSender()
- {
- try {
- server = new Socket(sourceip, 1234);
- 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
- {
- bytesRead = in.read(byteArray, current, (byteArray.length-current));
- if(bytesRead >= 0) current += bytesRead;
- }while(bytesRead > -1);
- bos.write(byteArray, 0, current);
- 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