import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class FileServer {
public static void main(String[] args) {
String path="c:\\temp\\test.txt"; // 받을 파일명
ServerSocket ss = null;
FileOutputStream fos = null;
try {
// 서버 객체 생성
ss = new ServerSocket(8000);
System.out.println("서버 시작 !!!");
// 클라이언트가 접속하기를 대기
Socket sc = ss.accept();
System.out.println("클라이언트 접속 !!!");
// 소켓에서 보낸 정보를 받을 입력 스트림을 구한다.
InputStream is = sc.getInputStream();
// 저장할 파일출력스트림 객체 생성
fos = new FileOutputStream(path);
System.out.println("파일 다운로드 시작 !!!");
// 보내온 파일 내용을 파일에 저장
byte []b = new byte[512];
int n;
while((n=is.read(b, 0, b.length))>0) {
fos.write(b, 0, n);
System.out.println(n + "bytes 다운로드 !!!");
}
System.out.println("파일 다운로드 끝 !!!");
fos.close();
sc.close();
ss.close();
} catch (Exception e) {
System.out.println(e.toString());
}
}
}
'자바 DB 연동 > 네트워크' 카테고리의 다른 글
UDP 채팅 프로그램 소스 ( 多 대 多 ) (0) | 2013.06.19 |
---|---|
TCP 채팅 프로그램 - 서버 / 클라이언트(1:1 and 1:多) (1) | 2013.06.19 |
객체 직렬화 Serializable (0) | 2013.06.19 |
웹사이트의 소스보는 소스 (0) | 2013.06.19 |