336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.
Java Socket File 전송 예제
1. Server
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
/*
* file transfer example
* just in case various file name length
* */
public class FileTransferServer {
ServerSocket server;
Socket socket;
final int PORT = 8091;
DataInputStream dis;
FileOutputStream fos;
File file;
String filePath;
byte[] buffer;
int length;
public FileTransferServer() {
filePath = ".//Server//";
buffer = new byte[1024];
}
public void start() {
//server start
try {
server = new ServerSocket(PORT);
socket = server.accept(); // wait for connected client
dis = new DataInputStream(socket.getInputStream());
int fileCnt = dis.readInt();
for(int i=0; i<fileCnt; i++) {
String fileName = dis.readUTF();
file = new File(filePath+fileName);
file.createNewFile(); // create a file
System.out.println(file.getName());
// start to write content to the file
long fileSize = dis.readLong();
long data = 0;
fos = new FileOutputStream(file);
while((length = dis.read(buffer))!= -1) {
fos.write(buffer,0,length);
data += length;
if(data == fileSize) break; // end of the file
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
fos.close();
dis.close();
server.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) {
new FileTransferServer().start();
}
}
2.Client
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
public class FileTransferClient {
Socket socket;
final String IP = "127.0.0.1";
final int PORT = 8091;
DataOutputStream dos;
FileInputStream fis;
File dir;
String filePath = ".//Client";
int length;
byte[] buffer;
public FileTransferClient() {
buffer = new byte[1024];
}
public void start() {
try {
socket = new Socket(IP,PORT);
dos = new DataOutputStream(socket.getOutputStream());
// file read
dir = new File(filePath);
dos.writeInt(dir.listFiles().length); // send file cnt
for(File file : dir.listFiles()) {
if(file.isFile()) { // only in case of file
dos.writeUTF(file.getName());
fis = new FileInputStream(file);
dos.writeLong(file.length()); // send file size
while((length = fis.read(buffer))!=-1) {
dos.write(buffer,0,length);
dos.flush();
}
}
}
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.exit(0);
}
public static void main(String[] args) {
new FileTransferClient().start();
System.out.println("system exit");
}
}
'IT > Java' 카테고리의 다른 글
Java StringController (0) | 2020.06.22 |
---|---|
ArrayList 문자열, 객체 정렬 (0) | 2019.08.28 |
[API] FileUtils (0) | 2019.06.25 |
BubbleSort 및 ArrayList, String API 요약 (0) | 2018.05.08 |
자바에서 XML다루기(2) (0) | 2015.11.13 |