JAVA/자바

자바 이미지파일 전송 클라이언트쪽

왕왕왕왕 2015. 10. 29. 23:13

// 소켓 생성

Socket socket = new Socket(dstAddress, dstPort);

// 데이터 전송용 스트림 생성

DataOutputStream dos = new DataOutputStream(socket.getOutputStream());

System.out.println("파일 전송 작업을 시작합니다.");


// 파일 이름 전송

// String fName = "작업용a.txt";

// String fName = "피티a.ppt";

// String fName = "작업용a.jpg";

String fName = "작업용a.png";

dos.writeUTF(fName);

System.out.printf("파일 이름(%s)을 전송하였습니다.\n", fName);


// 파일 내용을 읽으면서 전송

BufferedInputStream bis = new BufferedInputStream(new ByteArrayInputStream(bitmapToByteArray(b)));


int len;

int size = 4096;

byte[] data = new byte[size];

while ((len = bis.read(data)) != -1) {

dos.write(data, 0, len);

}


dos.flush();

dos.close();

bis.close();

System.out.println("파일 전송 작업을 완료하였습니다.");

System.out.println("보낸 파일의 사이즈 : " + bitmapToByteArray(b).length);