Step Two: Implement file delivery.
The previous step just passed a string from the server to the client, this time the code needs to be adjusted to achieve from the server to obtain the file, the client will save the file to the destination address.
The adjusted code:
Service side:
ImportJava.io.DataInputStream;ImportJava.io.DataOutputStream;ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.net.ServerSocket;ImportJava.net.Socket; Public classServer { Public Static voidMain (string[] args) {//Create a service-side socket for receiving network informationServerSocket ServerSocket =NULL; Try { //Server socket using local IP, port number using 36000ServerSocket =NewServerSocket (36000); //ensure continuous operation on the service side while(true){ //receiving network information, not receiving it, blocking the current threadSocket socket =serversocket.accept (); //write file information to socket communicationWriteFile (socket); //close the socket to prevent the client from waiting for a long timeSocket.close (); } } Catch(Exception e) {e.printstacktrace (); } } /*** Write the file information to the socket communication stream *@paramSocket * Socket*/ Private Static voidWriteFile (socket socket) {Try{ //get local fileString FilePath = "D:/test.pdf"; //To create a file objectFile File =NewFile (FilePath); //transferring file information using DataOutputStreamDataOutputStream dos =NewDataOutputStream (Socket.getoutputstream ()); //Write file nameDos.writeutf (File.getname ()); //Write File sizeDos.writelong (File.length ()); //emptying buffersDos.flush (); //using DataInputStream to write files toDataInputStream dis =NewDataInputStream (Newfileinputstream (file)); byte[] buffer =New byte[1024]; intHead = 0; while((head=dis.read (buffer)) > 0) {dos.write (buffer,0, head); } dis.close (); Dos.close (); }Catch(Exception e) {e.printstacktrace (); } }}
Client:
ImportJava.io.DataInputStream;ImportJava.io.DataOutputStream;ImportJava.io.FileOutputStream;ImportJava.net.Socket; Public classClient { Public Static voidMain (string[] args) {DataInputStream input=NULL; Socket Clientsocket=NULL; Try { //Create a client socket to connect to the serverClientsocket =NewSocket ("127.0.0.1", 36000); //read the traffic sent by the service sideinput =NewDataInputStream (Clientsocket.getinputstream ()); String FileName=Input.readutf (); LongFileSize =Input.readlong (); System.out.println ("received the server sent over the file, the file name is called:" + filename + ", the file size is:" +fileSize); //to read a file into a localString Savepath = "E:\\just a test.pdf"; DataOutputStream Fos=NewDataOutputStream (NewFileOutputStream (Savepath)); byte[] buffer =New byte[1024]; intHead = 0; while((head=input.read (buffer)) > 0) {fos.write (buffer,0, head); } fos.close (); Input.close (); Clientsocket.close (); } Catch(Exception e) {e.printstacktrace (); } }}
This file stream is transmitted using DataOutputStream and DataInputStream, and the data type of byte stream can be processed separately according to the information type.
Use Java to implement the Fly Pigeon Book 2-File transfer