2012. 9. 16. 16:05

[출처 :http://blog.naver.com/inganyoyo?Redirect=Log&logNo=90072811148


receive.java

#####################################################################

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.net.Socket;

import java.net.UnknownHostException;

 

public class receiver extends Thread {

Socket socket = null

File file = null

FileOutputStream fos = null

InputStream is = null

 

public receiver() throws UnknownHostException, IOException {

socket = new Socket("127.0.0.1", 8000);

file = new File("./receiver.avi");

fos = new FileOutputStream(file);

is = socket.getInputStream();

}

 

public static void main(String[] args) throws UnknownHostException,

IOException {

receiver r = new receiver();

 

int readCount = 0;

byte[] buffer = new byte[4096];

r.start();

while ((readCount = r.is.read(buffer)) > 0) {

r.fos.write(buffer, 0, readCount);

}

System.out.println(");

r.is.close();

r.fos.close();

r.stop()

}

 

public void run() {

File file = new File("./receiver.avi");

long curr = 0;

long before = 0;

while (true) {

if (file.exists()) {

try {

Thread.sleep(1000);

curr = file.length();

System.out

.println((double) ((((curr - before) / 1024)) / 1024));

before = curr;

 

catch (InterruptedException e) {

e.printStackTrace();

}

}

}

}

}

 ###################################################################


sender.java

#####################################################################

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.OutputStream

import java.net.ServerSocket;

import java.net.Socket;

 

public class sender {

public static void main(String[] args) throws IOException {

ServerSocket ss = new ServerSocket(8000);

Socket socket = ss.accept();

OutputStream os = socket.getOutputStream();

 

File file = new File("./sender.avi");

FileInputStream fis = new FileInputStream(file);

int readCount = 0;

byte[] buffer = new byte[4096];

 

while((readCount = fis.read(buffer))>0){

os.write(buffer, 0, readCount);

}

 

System.out.println(");

os.close();

fis.close();

 

 

}

}

 

#####################################################################

[출처] 자바 파일전송|작성자 우왕귿


Posted by k1rha