简体   繁体   中英

Server socket file transfer

I have used server socket concept in java to transfer files like images and videos. But when i receive at the client side, i am customizing the file names. Can i get the original name of the file as it is?

For Example:

If the file from server end for transfer is "abc.txt", i need this same name to be reflected in the client end(without passing the name separately).

In the server end:

public class FileServer {
  public static void main (String [] args ) throws Exception {
    // create socket
    ServerSocket servsock = new ServerSocket(13267);
    while (true) {
      System.out.println("Waiting...");

      Socket sock = servsock.accept();
      System.out.println("Accepted connection : " + sock);
      OutputStream os = sock.getOutputStream();
    new FileServer().send(os);
      sock.close();
      }
    }

  public void send(OutputStream os) throws Exception{
      // sendfile
      File myFile = new File ("C:\\User\\Documents\\abc.png");
      byte [] mybytearray  = new byte [(int)myFile.length()+1];
      FileInputStream fis = new FileInputStream(myFile);
      BufferedInputStream bis = new BufferedInputStream(fis);
      bis.read(mybytearray,0,mybytearray.length);
      System.out.println("Sending...");
      os.write(mybytearray,0,mybytearray.length);
      os.flush();
  }
}

In the client end:

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


    long start = System.currentTimeMillis();


    // localhost for testing
    Socket sock = new Socket("127.0.0.1",13267);
    System.out.println("Connecting...");
    InputStream is = sock.getInputStream();
    // receive file
    new FileClient().receiveFile(is);
       long end = System.currentTimeMillis();
    System.out.println(end-start);

    sock.close();
  }

  public void receiveFile(InputStream is) throws Exception{
      int filesize=6022386;
      int bytesRead;
      int current = 0;
      byte [] mybytearray  = new byte [filesize];

        FileOutputStream fos = new FileOutputStream("def");
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        bytesRead = is.read(mybytearray,0,mybytearray.length);
        current = bytesRead;


        do {
           bytesRead =
              is.read(mybytearray, current, (mybytearray.length-current));
           if(bytesRead >= 0) current += bytesRead;
        } while(bytesRead > -1);

        bos.write(mybytearray, 0 , current);
        bos.flush();
        bos.close();
  }
}

For the receiver to know the file name, either:

a) it must assume it knows the name because it asked for it,

b) the server sends the name first as part of the stream.

If you invent a way to send information without actually sending it, let me know and we can become billionaires. We can call it 'computer telepathy'.

Yes, simply transfer the metadata (in your case myFile.getName() ) before the actual file contents, and make client and server read and emit that metadata. It's a good idea to use established protocols, for example HTTP and its Content-Disposition header.

There is a simpler way to do this. Just wrap your input stream inside DataInputStream and use methods like .writeUTF(myFile.getName()). Similarly, you can read file name at receiver by applying .readUTF on DataInputStream. Here is a sample code: Sender:

FileInputStream fis = new FileInputStream(myFile);  
    BufferedInputStream bis = new BufferedInputStream(fis);
    DataInputStream dis = new DataInputStream(bis);
    OutputStream os;
    try {
        os = socket.getOutputStream();
        DataOutputStream dos = new DataOutputStream(os);
        dos.writeUTF(myFile.getName());  
.............////// catch exceptions

Receiver:

InputStream in;
    try {
        bufferSize=socket.getReceiveBufferSize();
        in=socket.getInputStream();
        DataInputStream clientData = new DataInputStream(in);
        String fileName = clientData.readUTF();
        System.out.println(fileName);
................../////// catch exceptions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM