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();
}
}
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) throwsUnknownHostException,
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();
}
}
}
}
}
출처 - http://blog.naver.com/inganyoyo?Redirect=Log&logNo=90072811148
다음은 자바 파일 전송 예제이다.
서버가 파일을 전송하고 클라이언트는 파일을 수신한다.
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;
import java.util.Properties;
public class FileSendingService {
public static final int DEFAULT_PORT = 12346;
private static final int DEFAULT_BUFFER_SIZE = 1024;
private static final String CONFIGURATION_FILENAME = "conf/file_sending_service.properties";
private static final String KEY_FILENAME_TO_SEND = "FILENAME_TO_SEND";
private final int port;
private boolean running = false;
public FileSendingService() {
this(DEFAULT_PORT);
}
public FileSendingService(int port) {
this.port = port;
}
private String getFilenameToSendFromConfiguration() {
Properties properties = new Properties();
try {
properties.load(new FileInputStream(CONFIGURATION_FILENAME));
return properties.getProperty(KEY_FILENAME_TO_SEND);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public void start() {
running = true;
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(port);
System.out.println("This server is listening... (Port: " + port
+ ")");
while (running) {
Socket socket = null;
FileInputStream fis = null;
OutputStream os = null;
try {
socket = serverSocket.accept();
long startTime = System.currentTimeMillis();
System.out.println("Start time: " + new Date());
String filenameToSend = getFilenameToSendFromConfiguration();
System.out.println("Filename to send: " + filenameToSend
+ ")");
File fileToSend = new File(filenameToSend);
long fileSize = fileToSend.length();
System.out.println("File size: " + (fileSize) + " Byte(s)");
fis = new FileInputStream(fileToSend);
InetSocketAddress clientIsa = (InetSocketAddress) socket
.getRemoteSocketAddress();
String clientIp = clientIsa.getAddress().getHostAddress();
int clientPort = clientIsa.getPort();
System.out.println("A client is connected. (" + clientIp
+ ":" + clientPort + ")");
os = socket.getOutputStream();
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
int totalReadBytes = 0;
int readBytes;
while ((readBytes = fis.read(buffer)) != -1) {
os.write(buffer, 0, readBytes);
totalReadBytes += readBytes;
System.out.println("In progress: " + totalReadBytes
+ "/" + fileSize + " Byte(s) ("
+ (totalReadBytes * 100 / fileSize) + " %)");
}
long endTime = System.currentTimeMillis();
System.out.println("End time: " + new Date());
long diffTime = endTime - startTime;
long diffTimeInSeconds = diffTime / 1000;
System.out.println("Elapsed time: " + diffTimeInSeconds
+ " second(s)");
System.out.println("Average transfer speed: "
+ (fileSize / diffTime) + " KB/s");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (serverSocket != null) {
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public void stop() {
running = false;
}
public static void main(String[] args) {
FileSendingService fileSendingService = new FileSendingService();
fileSendingService.start();
}
}
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.net.UnknownHostException;
public class FileReceivingClient {
private static final int DEFAULT_BUFFER_SIZE = 1024;
private static final String DEFAULT_SERVER_IP = "localhost";
private final String serverIp;
private final int serverPort;
private final String filenameToReceive;
public FileReceivingClient(String serverIp, int serverPort,
String filenameToReceive) {
this.serverIp = serverIp;
this.serverPort = serverPort;
this.filenameToReceive = filenameToReceive;
}
public void start() {
FileOutputStream fos = null;
Socket socket = null;
InputStream is = null;
try {
fos = new FileOutputStream(filenameToReceive);
System.out.println("This client is connecting to " + serverIp + ":"
+ serverPort + "...");
socket = new Socket(serverIp, serverPort);
System.out.println("This client is Connected.");
is = socket.getInputStream();
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
int readBytes;
while ((readBytes = is.read(buffer)) > 0) {
fos.write(buffer, 0, readBytes);
}
System.out.println("File transfer completed.");
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
String serverIp = DEFAULT_SERVER_IP;
int serverPort = FileSendingService.DEFAULT_PORT;
String filenameToReceive = "received.txt";
FileReceivingClient fileReceivingClient = new FileReceivingClient(
serverIp, serverPort, filenameToReceive);
fileReceivingClient.start();
}
}
다음은 반대로 서버가 파일을 수신하고 클라이언트는 파일을 전송하는 예제이다
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class FileServer {
public static void main(String[] args) {
int port = 10000;
try {
ServerSocket ss = new ServerSocket(port);
System.out.println("This server is listening... (Port: " + port
+ ")");
Socket socket = ss.accept();
InetSocketAddress isaClient = (InetSocketAddress) socket
.getRemoteSocketAddress();
String clientAddress = isaClient.getAddress().getHostAddress();
int clientPort = isaClient.getPort();
System.out.println("A client is connected. (" + clientAddress + ":"
+ clientPort + ")");
// String filename = "test2.txt";
String filename = "test2.avi";
FileOutputStream fos = new FileOutputStream(filename);
InputStream is = socket.getInputStream();
// int c;
// while ((c = is.read()) != -1) {
// // System.out.println("readByte: " + readByte);
//
// fos.write(c);
// }
byte[] buffer = new byte[Constants.DEFAULT_BUFFER_SIZE];
int readBytes;
while ((readBytes = is.read(buffer)) != -1) {
fos.write(buffer, 0, readBytes);
}
System.out.println("File transfer completed.");
is.close();
fos.close();
socket.close();
ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
클라이언트
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Date;
public class FileClient {
public static void main(String[] args) {
String host = "localhost";
int port = 10000;
// String filename = "test.txt";
String filename = "test.avi";
File file = new File(filename);
long fileSize = file.length();
System.out.println("File size: " + (fileSize) + " Byte(s)");
try {
long startTime = System.currentTimeMillis();
System.out.println("Start time: " + new Date());
FileInputStream fis = new FileInputStream(file);
System.out.println("This client is connecting to " + host + ":"
+ port + "...");
Socket socket = new Socket(host, port);
System.out.println("This client is Connected.");
OutputStream os = socket.getOutputStream();
// int c;
// while ((c = fis.read()) != -1) {
// os.write(c);
// }
long totalReadBytes = 0;
byte[] buffer = new byte[Constants.DEFAULT_BUFFER_SIZE];
int readBytes;
while ((readBytes = fis.read(buffer)) > 0) {
os.write(buffer, 0, readBytes);
totalReadBytes += readBytes;
System.out.println("In progress: " + totalReadBytes + "/"
+ fileSize + " Byte(s) ("
+ (totalReadBytes * 100 / fileSize) + " %)");
}
System.out.println("File transfer completed.");
fis.close();
os.close();
socket.close();
long endTime = System.currentTimeMillis();
System.out.println("End time: " + new Date());
long diffTime = endTime - startTime;
long diffTimeInSeconds = diffTime / 1000;
System.out.println("Elapsed time: " + diffTimeInSeconds
+ " second(s)");
System.out.println("Average transfer speed: " + (fileSize / 1000)
/ diffTimeInSeconds + " KB/s");
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
'Development > Java' 카테고리의 다른 글
java - listener(callback) 만들기 2 (0) | 2013.04.08 |
---|---|
java - listener 만들기 (0) | 2013.04.08 |
system.out 출력 포맷 (0) | 2013.01.31 |
java - CRC32 클래스 (0) | 2013.01.31 |
serialVersionUID (0) | 2013.01.01 |