- Server.java
Packagecom.huaxin.lesson02;ImportJava.io.FileOutputStream;ImportJava.io.InputStream;ImportJava.io.ObjectInputStream;ImportJava.net.ServerSocket;ImportJava.net.Socket;/*** @thing Implement client send file to server * @thing client sent to client (this code is not implemented) *@authorAdministrator **/ Public classsevers { Public Static voidMain (string[] args) {Newsevers (). Initserver (); } Public voidInitserver () {Try{serversocket Ssocket=NewServerSocket (9191); System.out.println ("The server was created successfully!" "); Socket Socket=ssocket.accept (); System.out.println ("There is a client link successfully!" "); //prepare two streams: file output stream, socket input streamInputStream ins =Socket.getinputstream (); //Save the file uploaded by the client to the serverObjectInputStream Ois =Newobjectinputstream (INS); FileOutputStream Fos=NewFileOutputStream ("c:\\users\\administrator\\desktop\\ temporary document \\test\\2016 plan April (1). doc"); System.out.println ("Start reading files ..."); //1. Length of the read array intLenght =Ois.readint (); //2. Number of reads LongTimes =Ois.readlong (); //3. Read the last byte length intLastbytes =Ois.readint (); byte[] bytes =New byte[lenght];//ois.read (bytes); /**In contrast to read , reads a byte array * Read does not have to read all the bytes in the 2048 bytes, will continue to go down * readfully is the function used in the communication, will determine whether there are no bytes in the stream remaining * There is a case in which the byte array does not transmit all the bytes in place, but block on the network, or block to the sending side of the network card * Readfully method, will wait for the large byte array all the data read, continue to execute down * The Read method will detect if there are any remaining bytes in the stream, and if not, continue to execute * **/ //Looping through Files while(Times > 1) {ois.readfully (bytes); Fos.write (bytes); Fos.flush (); Times-- ; } //processing the last byte arraybytes =New byte[Lastbytes]; ois.readfully (bytes); Fos.write (bytes); Fos.flush (); // //the number of bytes of file that will be read//Long fileSize = Ois.readlong ();//System.out.println (fileSize);// //int value = Ins.read ();//While (FileSize > 0) {//filesize--;//Fos.write (value);//Fos.flush ();//value = Ins.read ();// }System.out.println ("File received! has been saved to the server. "); Ois.close (); Fos.close (); Socket.close (); } Catch(Exception e) {e.printstacktrace (); } } }
- Client.java
Packagecom.huaxin.lesson02;ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;ImportJava.io.ObjectOutputStream;ImportJava.io.OutputStream;ImportJava.net.Socket;ImportOrg.omg.CORBA.portable.InputStream; Public classClient { Public intval = 2048; Public Static voidMain (string[] args) {NewClient (). Initclient (); } Public voidinitclient () {Try{Socket Socket=NewSocket ("localhost", 9191); //prepare two streams, file input stream, socket input stream//the local input streamFileInputStream FIS =NewFileInputStream ("c:\\users\\administrator\\desktop\\ temporary document \\test\\2016 plan April 19.doc"); //send out the local input stream .OutputStream ous =Socket.getoutputstream (); ObjectOutputStream Oos=NewObjectOutputStream (OUs); System.out.println ("Start reading send files ...! "); //get the file size firstFile File =NewFile ("c:\\users\\administrator\\desktop\\ temporary document \\test\\2016 April 19.doc"); LongFileSize =file.length (); byte[] bytes =New byte[Val]; //calculate the number of words to send byte array LongTimes = filesize/val+1; //calculates the number of valid bytes for the last set of byte arrays intLastbytes = (int) filesize%2048; //1. Send byte array lengthOos.writeint (Val); //2. Number of sendsOos.writelong (times); Oos.flush (); //3. Last byte CountOos.writeint (lastbytes); Oos.flush (); //reads bytes of byte array length, returns the number of bytes of data read intValue =fis.read (bytes); while(Value! =-1){ //offset byte count readOos.write (bytes,0, value); Oos.flush (); Value=fis.read (bytes); } //Oos.writelong (fileSize);//Oos.flush ();//System.out.println (fileSize);////ous.write ((int) fileSize);////Ous.flush ();////System.out.println (fileSize);////Thread.Sleep (+);// // //reading byte data from a file//int value = Fis.read ();//While (value! =-1) {//Ous.write (value);//Ous.flush ();//value = Fis.read ();// }System.out.println ("The file has been sent! "); Thread.Sleep (2000); //Close the streamFis.close (); Ous.close (); Socket.close (); } Catch(Exception e) {e.printstacktrace (); } }}
- The notes are in the code.
"Code Note" Java in-depth learning-implementing client-side file transfer to the server