The following is the service-side code
Package lyj.srever;
public class Server {
public static void Main (string[] args) {
New Serverthread (). Start ();
}
}
Package lyj.srever;
Import Java.io.File;
Import java.io.IOException;
Import Java.net.ServerSocket;
Import Java.net.Socket;
Import java.util.ArrayList;
public class Serverthread extends thread{
public static arraylist<socket> sockets=new arraylist<socket> ();
Public Serverthread () {};
Private arraylist<myfile> data=new arraylist<myfile> ();
Private file[] files;
private int size;
Public arraylist<socket> Getssockets () {return sockets;}
@Override
public void Run () {
try {
GetFile ();
Service-Side Open
ServerSocket server=new ServerSocket (9999);
SYSTEM.OUT.PRINTLN ("service-side open");
while (true) {
Receiving clients
Socket socket=server.accept ();
Pass the socket to a thread processing
if (Sockets.size () ==0) {
New Socketthread (socket, data, size);
Sockets.add (0,socket);
}else{
Sockets.add (1,socket);
New Socketmanager (socket, data);
}
}
} catch (IOException e) {
E.printstacktrace ();
}
}
/**
* Server-side files
* @return
*/
public void GetFile () {
File Folder=new file ("e:\\ cloud Disk");
Files=folder.listfiles ();
Size=files.length;
for (int i=0;i<size;i++) {
File File=files[i];
MyFile myfile=new MyFile ();
Myfile.setname (File.getname ());
Myfile.setsize (File.length ());
Myfile.seturi (File.getpath ());
Data.add (myfile);
}
}
}
Package lyj.srever;
Import Java.io.BufferedInputStream;
Import Java.io.BufferedOutputStream;
Import Java.io.BufferedReader;
Import Java.io.File;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
Import Java.io.ObjectInputStream;
Import Java.io.ObjectOutputStream;
Import Java.io.OutputStream;
Import Java.net.Socket;
Import java.util.ArrayList;
public class Socketthread extends thread{
private socket socket;
Private file[] files;
private int size;
Private arraylist<myfile> data;
Private OutputStream Ops;
Public Socketthread (Socket socket,arraylist<myfile> data,int size) {
This.socket=socket;
This.data=data;
This.size=size;
Start ();
}
@Override
public void Run () {
try {
Ops=socket.getoutputstream ();
String temp1= "";
for (int i=0;i<size;i++) {
String Str=data.get (i). GetName () + "]" +data.get (i). GetSize () + "\ r \ n";
TEMP1=TEMP1+STR;
}
Ops.write (Temp1.getbytes ("Utf-8"));
Ops.flush ();
SYSTEM.OUT.PRINTLN ("Data send Complete");
} catch (IOException E1) {
E1.printstacktrace ();
}
}
}
Package lyj.srever;
Import Java.io.BufferedInputStream;
Import Java.io.BufferedReader;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
Import Java.io.ObjectInputStream;
Import Java.io.OutputStream;
Import java.net.InetAddress;
Import Java.net.Socket;
Import java.net.UnknownHostException;
Import java.util.ArrayList;
public class Socketmanager extends thread{
private socket socket;
Private arraylist<myfile> data;
Public Socketmanager (Socket socket,arraylist<myfile> data) {
This.socket=socket;
This.data=data;
Start ();
}
@Override
public void Run () {
Bufferedinputstream bis;
try {
bis = new Bufferedinputstream (Socket.getinputstream ());
InputStreamReader isr=new InputStreamReader (bis, "utf-8");
BufferedReader br=new BufferedReader (ISR);
String temp= "";
try {
while ((Temp=br.readline ())!=null) {
String str=temp.substring (0,2);
int Index=integer.parseint (temp.substring (2));
SYSTEM.OUT.PRINTLN (index);
if (str.equals ("Download")) {
Arraylist<socket> sockets=serverthread.sockets;
System.out.println (Sockets.size ());
System.out.println (Sockets.get (0). Getport ());
New Downloadthread (socket, index, data);
}else if (str.equals ("upload")) {
New Uploadthread ();
}
}
} catch (IOException E1) {
E1.printstacktrace ();
}
} catch (NumberFormatException e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
}
}
}
Package lyj.srever;
Import Java.io.BufferedInputStream;
Import Java.io.BufferedOutputStream;
Import Java.io.DataInputStream;
Import Java.io.DataOutputStream;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.OutputStream;
Import Java.net.Socket;
Import java.util.ArrayList;
public class Downloadthread extends Thread {
private socket socket;
private int index;
Private arraylist<myfile> data;
Public Downloadthread (socket socket, int index, arraylist<myfile> data) {
This.index = index;
This.socket = socket;
This.data = data;
Start ();
}
@Override
public void Run () {
try {
File File = new file (Data.get (index). GetURI ());
System.out.println (File.getabsolutepath ());
FileInputStream fis = new FileInputStream (file);
System.out.println ("Start transfer" + Data.get (Index). GetName ());
try {
Bufferedoutputstream Ops = new Bufferedoutputstream (
Socket.getoutputstream ());
byte[] buf = new byte[100];
int num = 0;
while (num = Fis.read (BUF))! =-1) {
Ops.write (buf, 0, num);
}
ServerThread.sockets.remove (socket);
System.out.println ("Off");
} catch (IOException e) {
E.printstacktrace ();
}
} catch (FileNotFoundException e) {
E.printstacktrace ();
}
}
}
Android Course design-use sockets to enable simple cloud disks between your computer and your phone