This article describes the Java socket based file transfer implementation method. Share to everyone for your reference, specific as follows:
1. The Java code is as follows:
Package sterning;
Import Java.io.BufferedInputStream;
Import Java.io.DataInputStream;
Import Java.io.DataOutputStream;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.net.ServerSocket;
Import Java.net.Socket;
public class Servertest {int port = 8821;
void Start () {Socket s = null;
try {serversocket ss = new ServerSocket (port);
while (true) {//Select file to be transmitted String FilePath = "D:\\lib.rar";
File fi = new file (FilePath);
System.out.println ("File Length:" + (int) fi.length ()); The public Socket accept () throws//IOException listens for and accepts connections to this socket.
This method blocks until the connection is made.
s = ss.accept ();
SYSTEM.OUT.PRINTLN ("Establish socket link");
DataInputStream dis = new DataInputStream (New Bufferedinputstream (S.getinputstream ()));
Dis.readbyte ();
DataInputStream fis = new DataInputStream (new Bufferedinputstream (new FileInputStream));
DataOutputStream PS = new DataOutputStream (S.getoutputstream ()); Pass the filename and length to the client. Here to really apply all platforms, such as the Chinese name processing, but also need to process, specific can see thInk in Java 4th has ready-made code.
Ps.writeutf (Fi.getname ());
Ps.flush ();
Ps.writelong ((Long) fi.length ());
Ps.flush ();
int buffersize = 8192;
byte[] buf = new Byte[buffersize];
while (true) {int read = 0;
if (FIS!= null) {read = Fis.read (BUF);
} if (read = = 1) {break;
} ps.write (buf, 0, read);
} ps.flush ();
Note Close the socket link Oh, otherwise the client will wait for the server's data to come,//until the socket timeout, resulting in incomplete data.
Fis.close ();
S.close ();
System.out.println ("File transfer complete");
} catch (Exception e) {e.printstacktrace ();
} public static void Main (String arg[]) {new Servertest (). Start ();
}
}
2.socket Util helper Classes
Package sterning;
Import java.net.*;
Import java.io.*;
public class Clientsocket {private String IP;
private int port;
Private socket socket = NULL;
DataOutputStream out = null;
DataInputStream getmessagestream = null;
Public clientsocket (String IP, int port) {this.ip = IP;
This.port = port; /** *//** * Create socket connection * * @throws Exception * Exception/public void CreateConnection () throws Except
Ion {try {socket = new socket (IP, port);
catch (Exception e) {e.printstacktrace ();
if (socket!= null) socket.close ();
Throw e; The finally {}} public void SendMessage (String SendMessage) throws Exception {try {out = new DataOutputStream (s
Ocket.getoutputstream ());
if (Sendmessage.equals ("Windows")) {out.writebyte (0x1);
Out.flush ();
Return
} if (Sendmessage.equals ("Unix")) {out.writebyte (0x2);
Out.flush ();
Return
} if (Sendmessage.equals ("Linux")) {out.writebyte (0x3); Out.flush();
else {Out.writeutf (SendMessage);
Out.flush ();
} catch (Exception e) {e.printstacktrace ();
if (out!= null) out.close ();
Throw e; The finally {}} public DataInputStream Getmessagestream () throws Exception {try {getmessagestream = new DATAINP
Utstream (New Bufferedinputstream (Socket.getinputstream ()));
return getmessagestream;
catch (Exception e) {e.printstacktrace ();
if (getmessagestream!= null) getmessagestream.close ();
Throw e;
The finally {}} public void Shutdownconnection () {try {if (out!= null) out.close ();
if (getmessagestream!= null) getmessagestream.close ();
if (socket!= null) socket.close ();
The catch (Exception e) {}}}
3. Client
Package sterning;
Import Java.io.BufferedOutputStream;
Import Java.io.DataInputStream;
Import Java.io.DataOutputStream;
Import Java.io.FileOutputStream;
public class Clienttest {private Clientsocket cs = null;
Private String IP = "localhost";//set to server IP private int port = 8821;
Private String SendMessage = "Windwos";
Public clienttest () {try {if (CreateConnection ()) {SendMessage ();
GetMessage ();
} catch (Exception ex) {ex.printstacktrace ();
} private Boolean createconnection () {cs = new Clientsocket (IP, port); try {cs.
CreateConnection ();
System.out.print ("Connect server succeeded!" + "\ n");
return true;
catch (Exception e) {System.out.print ("Connection Server failed!" + "\ n");
return false;
} private void SendMessage () {if (cs = null) return;
try {cs.sendmessage (SendMessage);
catch (Exception e) {System.out.print ("Send message failed!" + "\ n");
} private void GetMessage () {if (cs = null) return; DataInputStream InputStream =Null
try {InputStream = Cs.getmessagestream ();
catch (Exception e) {System.out.print ("receive message cache error \ n");
Return
A try {//local save path, and the file name is automatically inherited from the server side.
String Savepath = "e:\\";
int buffersize = 8192;
byte[] buf = new Byte[buffersize];
int passedlen = 0;
Long len=0;
Savepath + + Inputstream.readutf (); DataOutputStream fileout = new DataOutputStream (New Bufferedoutputstream (new
FileOutputStream (Savepath)));
Len = Inputstream.readlong ();
System.out.println ("The length of the file is:" + len + "\ n");
System.out.println ("Start receiving files!" + "\ n");
while (true) {int read = 0;
if (InputStream!= null) {read = Inputstream.read (BUF);
} Passedlen + = read;
if (read = = 1) {break; //The following progress bar is done for the graphical interface of Prograssbar, here if the file is typed, it may be repeated to print out some of the same percentage System.out.println ("File Received" + (Passedlen * 100/len) + "%\n
");
Fileout.write (buf, 0, read);
SYSTEM.OUT.PRINTLN ("Receive complete, File Save as" + Savepath + "\ n");
Fileout.close (); catch (Exception e) {System.out.println ("Receive message error" + "\ n");
Return
} public static void Main (String arg[]) {new clienttest ();
}
}
I hope this article will help you with your Java programming.