Guest User

Untitled

a guest
May 21st, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. package aplication;
  2. import java.io.BufferedInputStream;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.IOException;
  6. import java.io.OutputStream;
  7. import java.net.ServerSocket;
  8. import java.net.Socket;
  9. import javax.swing.JOptionPane;
  10.  
  11.  
  12. public class sendFile {
  13. public static void main(String[] args) throws IOException {
  14. String pilihan=JOptionPane.showInputDialog("select 1 or 2");
  15. int a=Integer.parseInt(pilihan);
  16. if (a==1){
  17. ServerSocket ServerSocket=new ServerSocket (6066);
  18. Socket sock =ServerSocket.accept();
  19.  
  20. File myfile=new File ("D:\\data1.txt");
  21. byte[]mybytearray=new byte [(int)myfile.length()];
  22.  
  23.  
  24.  
  25.  
  26. BufferedInputStream bis =new BufferedInputStream(new FileInputStream(myfile));
  27. bis.read(mybytearray,0,mybytearray.length);
  28.  
  29. OutputStream os=sock.getOutputStream();
  30. os.write(mybytearray,0,mybytearray.length);
  31. os.flush();
  32.  
  33. sock.close();}
  34. else {
  35. ServerSocket ServerSocket=new ServerSocket (6066);
  36. Socket sock =ServerSocket.accept();
  37.  
  38. File myfile=new File ("F:\\data2.txt");
  39. byte[]mybytearray=new byte [(int)myfile.length()];
  40. BufferedInputStream bis =new BufferedInputStream(new FileInputStream(myfile));
  41. bis.read(mybytearray,0,mybytearray.length);
  42.  
  43. OutputStream os=sock.getOutputStream();
  44. os.write(mybytearray,0,mybytearray.length);
  45. os.flush();
  46.  
  47. sock.close();
  48. }
  49. }
  50. }
  51.  
  52. ************************************************************************************************************************
  53. secound program: receive program
  54. /*
  55. * To change this template, choose Tools | Templates
  56. * and open the template in the editor.
  57. */
  58. package aplication;
  59. import java.io.BufferedOutputStream;
  60. import java.io.File;
  61. import java.io.FileOutputStream;
  62. import java.io.InputStream;
  63. import java.net.Socket;
  64. import javax.swing.JFileChooser;
  65.  
  66. public class receive {
  67. public static void main(String[] argv) throws Exception{
  68.  
  69. JFileChooser pilih=new JFileChooser();
  70. pilih.setDialogTitle("Tentukan Tempat Penyimpanan");
  71. int userSelection = pilih.showSaveDialog(null);
  72. if(userSelection == JFileChooser.APPROVE_OPTION){
  73. File fileToSave = pilih.getSelectedFile();
  74. System.out.println("Save as file : "+ fileToSave.getAbsolutePath());
  75.  
  76. }
  77.  
  78. Socket sock=new Socket("localhost", 6066);
  79. byte[]mybytearray =new byte[1024];
  80. InputStream is =sock.getInputStream();
  81. FileOutputStream fos=new FileOutputStream("E:\\data.txt");
  82. BufferedOutputStream bos = new BufferedOutputStream (fos);
  83. bos.flush();
  84.  
  85. }
  86. private javax.swing.JTextArea txtPath;
  87. }
Add Comment
Please, Sign In to add comment