Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package aplication;
- import java.io.BufferedInputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.OutputStream;
- import java.net.ServerSocket;
- import java.net.Socket;
- import javax.swing.JOptionPane;
- public class sendFile {
- public static void main(String[] args) throws IOException {
- String pilihan=JOptionPane.showInputDialog("select 1 or 2");
- int a=Integer.parseInt(pilihan);
- if (a==1){
- ServerSocket ServerSocket=new ServerSocket (6066);
- Socket sock =ServerSocket.accept();
- File myfile=new File ("D:\\data1.txt");
- byte[]mybytearray=new byte [(int)myfile.length()];
- BufferedInputStream bis =new BufferedInputStream(new FileInputStream(myfile));
- bis.read(mybytearray,0,mybytearray.length);
- OutputStream os=sock.getOutputStream();
- os.write(mybytearray,0,mybytearray.length);
- os.flush();
- sock.close();}
- else {
- ServerSocket ServerSocket=new ServerSocket (6066);
- Socket sock =ServerSocket.accept();
- File myfile=new File ("F:\\data2.txt");
- byte[]mybytearray=new byte [(int)myfile.length()];
- BufferedInputStream bis =new BufferedInputStream(new FileInputStream(myfile));
- bis.read(mybytearray,0,mybytearray.length);
- OutputStream os=sock.getOutputStream();
- os.write(mybytearray,0,mybytearray.length);
- os.flush();
- sock.close();
- }
- }
- }
- ************************************************************************************************************************
- secound program: receive program
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package aplication;
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.InputStream;
- import java.net.Socket;
- import javax.swing.JFileChooser;
- public class receive {
- public static void main(String[] argv) throws Exception{
- JFileChooser pilih=new JFileChooser();
- pilih.setDialogTitle("Tentukan Tempat Penyimpanan");
- int userSelection = pilih.showSaveDialog(null);
- if(userSelection == JFileChooser.APPROVE_OPTION){
- File fileToSave = pilih.getSelectedFile();
- System.out.println("Save as file : "+ fileToSave.getAbsolutePath());
- }
- Socket sock=new Socket("localhost", 6066);
- byte[]mybytearray =new byte[1024];
- InputStream is =sock.getInputStream();
- FileOutputStream fos=new FileOutputStream("E:\\data.txt");
- BufferedOutputStream bos = new BufferedOutputStream (fos);
- bos.flush();
- }
- private javax.swing.JTextArea txtPath;
- }
Add Comment
Please, Sign In to add comment