1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | import java.io.File; import java.io.IOException; import java.net.Socket; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class FileSenderMain { public static final int PORT = 12345; public static void main(String[] args) { Socket socket = null; ExecutorService service = Executors.newFixedThreadPool(5); File dir = new File("./FILES"); for(File file : dir.listFiles()) { String filePath = file.getAbsolutePath(); try { socket = new Socket("127.0.0.1", PORT); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } if(socket.isConnected()) { System.out.println("sender socket :" + socket.hashCode()); FileSender sender = new FileSender(socket, filePath); service.execute(sender); } } service.shutdown(); } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.net.Socket; import java.util.Arrays; public class FileSender implements Runnable { Socket socket; String filePath; public static final int FILE_NAME_LNE = 128; public FileSender(Socket socket, String filePath) { super(); this.socket = socket; this.filePath = filePath; } public void sendFile() { File file = new File(filePath); byte[] buffer = new byte[1024]; int readLen = 0; String fileName = file.getName(); System.out.println("FileName : " + fileName); byte[] fileNameByte = new byte[128]; System.arraycopy(fileName.getBytes(), 0, fileNameByte, 0, fileName.getBytes().length); try { BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream()); bos.write(fileNameByte, 0, 100); bos.flush(); bos.write(fileNameByte, 100, 28); bos.flush(); while((readLen = bis.read(buffer, 0, 1024)) != -1) { bos.write(buffer, 0, readLen); } bis.close(); bos.close(); socket.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void run() { // TODO Auto-generated method stub sendFile(); } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import java.io.IOException; import java.net.Socket; public class FileReceiverMain { public static final int PORT = 12345; public static void main(String[] args) { // TODO Auto-generated method stub FileReceiverBroker receiver = new FileReceiverBroker(); receiver.acceptConnect(); } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class FileReceiverBroker { public static final int PORT = 12345; public static int count; ServerSocket server; ExecutorService service ; public FileReceiverBroker() { super(); try { this.server = new ServerSocket(PORT); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } this.count = 0; } public void acceptConnect() { Socket socket = null; service = Executors.newFixedThreadPool(10); try { while((socket = server.accept()) != null) { FileReceiver receiver = new FileReceiver(socket); service.execute(receiver); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } service.shutdown(); } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; public class FileReceiver implements Runnable{ Socket socket; public FileReceiver(Socket socket) { super(); this.socket = socket; } public void getFile() { byte[] buffer = new byte[1024]; byte[] byteFileName = new byte[128]; int readLen = 0; BufferedInputStream bis; BufferedOutputStream bos; try { bis = new BufferedInputStream(socket.getInputStream()); int totalLen = 0; while(totalLen < 128) { readLen = bis.read(byteFileName, totalLen, 128 - totalLen); if(readLen == -1) { System.out.println("전송이 종료되었습니다."); return; } System.out.println("ReadLen :" + readLen); totalLen += readLen; } String fileName = new String(byteFileName); System.out.println("FileName : [" + fileName+ "]"); fileName = fileName.trim(); System.out.println("FileName : [" + fileName+ "]"); File file = new File("./RECV/" + fileName); bos = new BufferedOutputStream(new FileOutputStream(file)); while( (readLen = bis.read(buffer, 0, 1024)) != -1 ) { bos.write(buffer, 0, readLen); } bis.close(); bis = null; bos.close(); bos = null; System.out.println("socket : " + socket.hashCode()); //socket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void run() { // TODO Auto-generated method stub getFile(); } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | import java.io.File; public class FileOpenTestMain { public static void main(String[] args) { File dir = new File("./FILES"); if( dir.isDirectory() == true) { String[] fileNames = dir.list(); for(String fileName : fileNames) { System.out.println(fileName); } File[] files = dir.listFiles(); for(File file : files) { System.out.println(file.getAbsolutePath()); } } } } | cs |