out.write(new String("HTTP/1.1 200 OK\r\n").getBytes()); 
    out.write(new String("Cache-Control: private\r\n").getBytes()); 
    out.write(new String("Content-Length: "+msg.getBytes().length+"\r\n").getBytes()); 
    out.write(new String("Content-Type: text/html; charset=UTF-8\r\n\r\n").getBytes());

Posted by 타다키치
,

package mp3example;


import java.io.DataInputStream;

import java.io.DataOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.net.ServerSocket;

import java.net.Socket;


public class Mp3Server {

private ServerSocket serverSocket;

private Mp3Service service;

public Mp3Server()throws Exception{

serverSocket = new ServerSocket(5555);

service = new Mp3Service("C:\\zzz\\mp3");

System.out.println("ready server.....");

waitClient();

}

public void waitClient(){

System.out.println("연결을 대기중입니다.");

Socket client = null;

DataInputStream din = null;

DataOutputStream dos = null;

try{

client = serverSocket.accept();

System.out.println("클라이언트가 연결되었습니다." + client);

din = new DataInputStream(client.getInputStream());

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

String cmd = din.readUTF();

System.out.println(cmd);

switch (cmd) {

case "1":

showUsage(din, dos);

break;

case "2":

sendList(din, dos);

break;

case "3":

sendMp3(din, dos);

break;

case "4":

receiveMp3(din, dos);

break;

default:

showUsage(din, dos);

break;

}

}catch(Exception e){

System.out.println(e.getMessage());

}finally{

try{ dos.close(); }catch(Exception e){}

try{ din.close(); }catch(Exception e){}

try{ client.close(); }catch(Exception e){}

}

waitClient();

}

public void showUsage(DataInputStream din, DataOutputStream dos)throws Exception{

dos.writeUTF("1.사용법,2.리스트,3.다운로드 <파일명>, 4.업로드 <파일명>");

}

public void sendList(DataInputStream din, DataOutputStream dos)throws Exception{

dos.writeUTF(service.getList());

}

public void sendMp3(DataInputStream din, DataOutputStream dos)throws Exception{

File targetFile = service.getFile(din.readUTF());

dos.writeUTF(targetFile.getName());

FileInputStream fin = new FileInputStream(targetFile);

CopyUtil.copy(fin, dos, true, false);

}

public void receiveMp3(DataInputStream din, DataOutputStream dos)throws Exception{

String uploadFileName = din.readUTF();

service.saveFile(uploadFileName, din);

}

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

Mp3Server server = new Mp3Server();

}

}




------------------------------------------------------------------------------------------------



package mp3example;

import java.io.DataInputStream;

import java.io.File;

import java.io.FileOutputStream;

public class Mp3Service {

private String rootDirName;

public Mp3Service(String rootDirName) {

super();

this.rootDirName = rootDirName;

}

public String getList()throws Exception{

File mp3Dir = new File(this.rootDirName);

String[] songList = mp3Dir.list();

StringBuilder builder = new StringBuilder();

for (String title : songList) {

builder.append(title+"|");

}

String result = builder.toString();

return result;

}

public File getFile(String fileName)throws Exception{

return new File(this.rootDirName+"\\" + fileName);

}

public void saveFile(String uploadFileName, DataInputStream din)throws Exception {

FileOutputStream fos = new FileOutputStream(this.rootDirName+"\\"+uploadFileName);

CopyUtil.copy(din, fos, false, true);

}

}

Posted by 타다키치
,

clientAgent와 CopyUtil을 참조.

여기서 Key Annotation 은 Mp3Client 객체의 매소드를 HashMap에 저장했다가 번호에 따라 불러오는데 쓰임.




package mp3example;


import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.lang.reflect.Method;

import java.util.HashMap;

import java.util.Map;

import java.util.Scanner;


public class Mp3Client {

private Scanner keyScanner;

private String ip;

private int port;

private String path;

private Map<String, Method> methodMap;

public Mp3Client(String ip, String path) throws Exception{

super();

this.ip = ip;

this.port = 5555;

this.keyScanner = new Scanner(System.in);

this.path = path;

this.methodMap = new HashMap<String, Method>();

Class clz = this.getClass();

Method[] arr = clz.getDeclaredMethods();

for (Method method : arr) {

KeyAnno anno = method.getAnnotation(KeyAnno.class);

if(anno == null){

continue;

}

methodMap.put(anno.value(), method);

}

}

public void execute()throws Exception{

System.out.println("작업을 시작할까요?");

getUsage();

while(true){

methodMap.get(keyScanner.nextLine()).invoke(this, null);

}

}

@KeyAnno("1")

public void getUsage()throws Exception{

new ClientAgent(this.ip,this.port){

@Override

protected void sendMsg() throws Exception {

dos.writeUTF("1");

}

@Override

protected void receiveMsg() throws Exception {

System.out.println(din.readUTF());

}

}.doExecute();

}

@KeyAnno("2")

public void getList()throws Exception{

new ClientAgent(this.ip,this.port){

@Override

protected void sendMsg() throws Exception {

dos.writeUTF("2");

}

@Override

protected void receiveMsg() throws Exception {

System.out.println(din.readUTF());

}

}.doExecute();

}

@KeyAnno("3")

public void download()throws Exception{

System.out.println("다운로드할 파일명을 입력하세요");

final String fileName = keyScanner.nextLine();

new ClientAgent(this.ip,this.port){

@Override

protected void sendMsg() throws Exception {

dos.writeUTF("3");

dos.writeUTF(fileName);

}

@Override

protected void receiveMsg() throws Exception {

String saveFileName = din.readUTF();

FileOutputStream fos = new FileOutputStream(path+saveFileName);

CopyUtil.copy(din, fos, false, true);

System.out.println("전송이 완료되었습니다.");

}

}.doExecute();

}

@KeyAnno("4")

public void upload()throws Exception{

System.out.println("업로드할 파일명을 입력하세요");

final String fileName = keyScanner.nextLine();

new ClientAgent(this.ip,this.port){

@Override

protected void sendMsg() throws Exception {

dos.writeUTF("4");

dos.writeUTF(fileName);

FileInputStream fin = new FileInputStream(path+fileName);

CopyUtil.copy(fin, dos, true, false);

System.out.println("업로드가 완료되었습니다.");

}

@Override

protected void receiveMsg() throws Exception {

}

}.doExecute();

}

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

Mp3Client client = new Mp3Client("127.0.0.1","C:\\zzz\\");

client.execute();

}

}



--------------------------------------------------------------------------------------------------

package mp3example;


import java.lang.annotation.ElementType;

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

import java.lang.annotation.Target;


@Target(ElementType.METHOD)

@Retention(RetentionPolicy.RUNTIME)

public @interface KeyAnno {


public String value();

}

Posted by 타다키치
,