简体   繁体   中英

How to transfer multiple files between client and server?

I rewrote a simple example of file transfer code between server and client.

And it works.

But i want to make it able to transfer multiple files in a particular directory. User will write the file names (which sits in that particular directory) and client will download them from the server. How can i do that? Any ideas? Thank you.

Client code:

    import java.net.*;
    import java.io.*;

    public class Client {

        static String hostname = "127.0.0.1";
        static int port = 4588;
        static int processedByte; 
        static byte[] theByte = new byte[1];

        static Socket client = null; 
          static InputStream inuputSt = null; 

        public static void main(String[] args) throws InterruptedException {
            System.out.println("connecting...");
            Thread.sleep(500);
            try { 
                client = new Socket(hostname, port); 
                inuputSt = client.getInputStream(); 
            } catch (IOException ex) { 
                System.out.println("connection error.");
            } 

            ByteArrayOutputStream arrayOutput = new ByteArrayOutputStream(); 

            if (inuputSt != null) { 

                FileOutputStream fileOutput = null; 
                BufferedOutputStream bufferedOutput = null; 
                try { 
                    System.out.println("downloading target file...");
                    Thread.sleep(800);
                    fileOutput = new FileOutputStream("file1_downloaded.txt"); 
                    bufferedOutput = new BufferedOutputStream(fileOutput); 
                    processedByte = inuputSt.read(theByte, 0, theByte.length); 

                    do { 
                            arrayOutput.write(theByte); 
                            processedByte = inuputSt.read(theByte); 
                    } while (processedByte != -1); 

                    bufferedOutput.write(arrayOutput.toByteArray()); 
                    bufferedOutput.flush(); 
                    bufferedOutput.close(); 
                    System.out.println("file downloaded");
                    client.close(); 
                } catch (IOException ex) { 
                    System.out.println("file transfer error."); 
                } 
            }

        }
    }

Server code:

import java.net.*;
import java.io.*;

public class Server {
static int port = 4588;
public static void main(String[] args) {

    while (true) {
        ServerSocket server = null; 
        Socket connection = null; 
        BufferedOutputStream bufferedOutput = null; 

        try { 
            server = new ServerSocket(port); 
            connection = server.accept(); 
            bufferedOutput = new BufferedOutputStream(connection.getOutputStream()); 
        } catch (IOException ex) { 
            // Do exception handling 
        } 

        if (bufferedOutput != null) { 
            File fileToSend = new File("files\\file1.txt"); 
            byte[] mybytearray = new byte[(int) fileToSend.length()]; 

            FileInputStream fileInputSt = null; 

            try { 
                fileInputSt = new FileInputStream(fileToSend); 
            } catch (FileNotFoundException ex) { 
                // exception stuff
            } 
            BufferedInputStream bufferedInput = new BufferedInputStream(fileInputSt); 

            try { 
                bufferedInput.read(mybytearray, 0, mybytearray.length); 
                bufferedOutput.write(mybytearray, 0, mybytearray.length); 
                bufferedOutput.flush(); 
                bufferedOutput.close(); 
                connection.close(); 

                //file1.txt has been downloaded
                return; 
            } catch (IOException ex) { 
                // exception stuff
            } 
        } 
    } 
}

}

You suggest HTTP as a protocol for your clients and servers -- HTTP is a fine protocol but may be a large implementation hurdle if you want to do the whole thing yourself. The HTTP PUT verb can be used to upload a file, and the benefit of using HTTP in this fashion is that your client and server could communicate with other tools designed to use PUT requests. ( PUT is less-used than other HTTP verbs, so not all HTTP tools will work; the curl(1) program does support PUT via the -T command line option. This will be a great implementation aid, should you chose HTTP.)

There are a variety of REST Frameworks that can assist you in writing HTTP software; I have heard good things about Restlet , it would be my recommended starting point.

But you don't have to pick HTTP as your protocol. I think you can learn a lot about networking programming if you implement your own protocol -- it will teach you a lot about API design and sockets programming in a way that would be difficult to learn by using pre-written HTTP protocol tools (and frustrating if you tried to implement HTTP in its entirety yourself).

Consider this conversation:

client -> server: upload file named "flubber" sized 200000 bytes
server -> client: ok
client -> server: flubber contents
server -> client: ok
client -> server: upload file named "blort" sized 10 bytes
server -> client: error, file exists
...

You might want to add new commands to provide for hashing the file on both ends to ensure the file transfer succeeded, commands for sending specific byte ranges (either to append to an existing file or re-start a failed transfer), commands to list existing file names on the server, commands to overwrite existing files, commands to delete files from the server, and so forth. The best part of writing your own protocol is you get to decide what your programs will support. The downside is that you get to test the features you write, and testing some cases may be difficult. (Say, consider that a client may send each character of a command in a different TCP packet. Implementing the buffering to store up an entire command isn't trivial , but it is already done for you with a tool such as Restlet.)

Juan's advice to use multiple TCP sessions isn't strictly necessary -- though it may be the easiest path forward for you. You'll need to add some mechanism to provide the filename to the remote peer, and that might be best done through the "control" channel (the first session running -- similar to FTP) or it might be something you send immediately before the file's contents (similar to HTTP).

I'd like to suggest avoiding multiple connections, though -- each connection requires three times the round-trip time between systems to set up and start transferring bytes. This delay can be extremely annoying, especially when you're trying to transfer hundreds of small files. You can even spend more time setting up connections than you do actually sending data.

But it's your tool -- you get to design it as you wish. Have fun.

I think you need to create a new connection for each file so in that situation you'll be able to transfer files simultaneously.

You may have to modify your server to create a new thread (or get one from a thread pool) for each client connection so it can work with many at the same time.

Then you can run the client once per file.

Cheers

Ok, can you transfer multi files making a ArrayList or List files. Getting into in the array after get out in a filesystem path. I hope help you.

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