客户端:
package client; import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException; public class MyClient { private static final String ADDRESS = "127.0.0.1";
private static final int PORT = 7869; private Socket CreateSocket() {
try {
Socket server = new Socket(ADDRESS, PORT);
return server;
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
} public boolean FileTransfer(String fileName) {
Socket server = this.CreateSocket();
try {
BufferedOutputStream bos = new BufferedOutputStream(
server.getOutputStream());
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(fileName));
byte[] bytes = new byte[1024];
int len = 0;
while ((len = bis.read(bytes)) != -1) {
bos.write(bytes, 0, len);
}
bis.close();
bos.close();
return true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MyClient client = new MyClient();
if (client.FileTransfer("wario.png")) {
System.out.println("Trans file sucessfully!");
}
} }
服务器端:
package server; import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket; public class FileServer implements Runnable { private ServerSocket server = null;
private int port = -1;
private String fileName; public FileServer(int port, String fileName) {
this.port = port;
this.fileName = fileName;
try {
this.server = new ServerSocket(this.port);
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("Please check this port!");
e.printStackTrace();
}
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Thread thread=new Thread(new FileServer(7869,"nannan.png"));
thread.start(); }; @Override
public void run() {
// TODO Auto-generated method stub
while (true) {
try {
Socket client;
client = this.server.accept();
AcceptFile accept=new AcceptFile(client,fileName);
Thread thread=new Thread(accept);
thread.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} } package server; import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.Socket; public class AcceptFile extends Thread { private Socket client;
private String fileName; public AcceptFile(Socket client,String fileName) {
this.client = client;
this.fileName=fileName;
} @Override
public void run() {
// TODO Auto-generated method stub
try {
BufferedInputStream bis=new BufferedInputStream(client.getInputStream());
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(fileName));
System.out.println("Server begin receive file!");
int len=0;
byte[] bytes=new byte[1024];
while((len=bis.read(bytes))!=-1){
bos.write(bytes, 0, len);
}
bos.close();
bis.close();
System.out.println("Accept file sucessfully!");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub } }