Guest User

Untitled

a guest
Jul 20th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.77 KB | None | 0 0
  1. import java.io.OutputStream;
  2. import java.io.File;
  3. import java.net.Socket;
  4. public class FileSender {
  5. public void main(Socket socket,String[] args) {
  6. try {
  7.  
  8. OutputStream os = socket.getOutputStream();
  9. int cnt_files = args.length;
  10.  
  11. // How many files?
  12. ByteStream.toStream(os, cnt_files);
  13.  
  14. for (int cur_file=0; cur_file<cnt_files; cur_file++) {
  15. ByteStream.toStream(os, new File(args[cur_file]).getName());
  16. ByteStream.toStream(os, new File(args[cur_file]));
  17. }
  18. }
  19. catch (Exception ex) {
  20. ex.printStackTrace();
  21. }
  22. }
  23. }
  24.  
  25. import java.io.File;
  26. import java.io.InputStream;
  27. import java.net.Socket;
  28.  
  29. public class FileReceiver {
  30.  
  31. public void main(Socket socket,String arg) {
  32. try {
  33. InputStream in = socket.getInputStream();
  34.  
  35. int nof_files = ByteStream.toInt(in);
  36. System.out.println("reach 1 "+ nof_files);
  37. for (int cur_file=0;cur_file < nof_files; cur_file++) {
  38. String file_name = ByteStream.toString(in);
  39.  
  40. File file=new File(arg+file_name);
  41. System.out.println("Received path is : "+file);
  42. ByteStream.toFile(in, file);
  43. }
  44.  
  45. }
  46. catch (java.lang.Exception ex) {
  47. ex.printStackTrace(System.out);
  48. }
  49. }
  50.  
  51. }
  52.  
  53. import java.io.*;
  54. import java.net.*;
  55. public class ClientFile implements Runnable{
  56. Socket clientsocket;
  57. public void run() {
  58. try
  59. {
  60. clientsocket = new Socket("219.64.189.14",6789);
  61. // Some code
  62. copy(outtoserver,infromserver, files); // files contains the path of files to be transferred.
  63. // Some code
  64. clientsocket.close();
  65. }
  66. catch(Exception e2)
  67. {
  68. System.out.println("ClientFile "+String.valueOf(e2) + "n");
  69. }
  70. }
  71. public void copy(DataOutputStream outtoserver,BufferedReader infromserver,String[] files)
  72. {
  73. try
  74. {
  75. FileSender fs = new FileSender();
  76. int totalfiles=0;
  77. int r=0;
  78. File oldfile;
  79. outtoserver.write(files.length);
  80. String chk;
  81. while(totalfiles<files.length)
  82. {
  83.  
  84. oldfile = new File(files[totalfiles]);
  85. if(oldfile.isDirectory())
  86. {
  87. outtoserver.writeBytes("foldern");
  88. File folder1[] = oldfile.listFiles();
  89. String[] folder = new String[folder1.length];
  90. int count=0;
  91. for(File name : folder1)
  92. {
  93. folder[count] = name + "";
  94. System.out.println(folder[count]);
  95. count++;
  96. }
  97. outtoserver.writeBytes(oldfile.getName()+"n");
  98. fs.main(clientsocket, folder);
  99.  
  100. }
  101. else if(oldfile.isFile())
  102. {
  103. outtoserver.writeBytes("filen");
  104. chk = infromserver.readLine();
  105. if(chk.equals("send"))
  106. {
  107. outtoserver.writeBytes(oldfile.getName()+"n");
  108. String[] folder = new String[]{oldfile.getAbsolutePath()};
  109. fs.main(clientsocket, folder);
  110. }
  111. totalfiles++;
  112. outtoserver.flush();
  113.  
  114. }
  115. }
  116. }
  117. catch(Exception e)
  118. {
  119. System.out.println("ClientFile -->> "+e.toString());
  120. }
  121. }
  122. }
  123.  
  124. import java.io.*;
  125. import java.net.*;
  126. import javax.swing.*;
  127. class ServerFile implements Runnable {
  128. Socket conn;
  129. public ServerFile(Socket a)
  130. {
  131. conn = a;
  132. }
  133. public void run() {
  134. File file1;
  135. String clientsen="";
  136. try
  137. { // Some code
  138. copy(outtoclient,infromclient,file1.getAbsolutePath()); //file1 is the directory to which the file has to stored.
  139. // some code
  140. }
  141. catch(Exception e0)
  142. {
  143. System.out.println("ServerFile "+String.valueOf(e0)+"n"+e0.getCause());
  144. }
  145. }//end main
  146. public void copy(DataOutputStream outtoclient,BufferedReader infromclient,String basepath)
  147. {
  148. try
  149. {
  150. FileReceiver fr = new FileReceiver();
  151. int totfiles = infromclient.read();
  152. int tot=0;
  153. File file;
  154. String path = null,chk;
  155. while(tot<totfiles)
  156. {
  157. chk = infromclient.readLine();
  158. if(chk.equals("file"))
  159. {
  160. outtoclient.writeBytes("sendn");
  161. path = infromclient.readLine();
  162. path = basepath+File.separator+path;
  163. file=new File(path);
  164. fr.main(conn, basepath+File.separator);
  165. }
  166. else if(chk.equals("folder"))
  167. {
  168. String name = infromclient.readLine();
  169. name = basepath+File.separator+name;
  170. new File(name).mkdir();
  171. fr.main(conn, name+File.separator);
  172. }
  173. tot++;
  174. }
  175. }
  176. catch(Exception e)
  177. {
  178. System.out.println("Server file: "+e.toString());
  179. }
  180. }
  181.  
  182. }//end class
  183.  
  184. ByteStream.toStream(os, "file")
  185.  
  186. ByteStream.toString(in)
Add Comment
Please, Sign In to add comment