实现一个客户端下载服务端提供的文件的程序
首先,服务端开启,7*24小时全天候360度无死角监控,针对每一个连接的Socket对象单独开启一个线程提供文件共享服务,线程中的run方法将目录中文件列表传输到客户端.然后,每当有客户端连接后,客户端就能够获取服务端传递过来的输入流并包装为对象输入流.接着,客户端向服务端传输文件编号,该过程中需要完成一个检测编号是否正确的方法.服务端的线程中有接收指令和传输文件的方法,服务端接收到客户端发送过来的指令后,便会传输文件.整个流程就是这样了.使用到的知识点包括IO流,Socket,Set,Map集合,线程等等.
![..](https://img-blog.csdn.net/20170803223201306?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvTGFuZ2xhbmd6b2RpYWM=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
/**
* 客户端
*/
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Set;
import java.util.TreeMap;
public class FtpClient {
private Socket s;
private File dir;
public FtpClient(Socket s,File dir) {
this.s = s;
this.dir = dir;
}
public void init(){
try {
/****显示可供下载的文件列表****/
ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
TreeMap<Integer,String> files = (TreeMap<Integer, String>) ois.readObject();
System.out.println("请输入需要下载的文件编号:");
Set<Integer> ids = files.keySet();
for (Integer id : ids) {
String fname = files.get(id);
System.out.println(id+"----"+fname);
}
/******向服务端传输文件编号*****/
sendCommand(files);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public void sendCommand(TreeMap<Integer,String> map) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String comm = br.readLine();
if(comm.matches("^\\d*$")){
int inputNo = Integer.parseInt(comm);
boolean isExist = false;
for (int i: map.keySet()) {
if(inputNo == i){
isExist = true;
break;
}
}
if(isExist){
realSendComm(inputNo);
String fname = map.get(inputNo);
File distFile = new File(dir,fname);
download(distFile);
}
else{
System.out.println("输入的文件编号有误!请重新输入");
sendCommand(map);
}
}else{
System.out.println("输入的文件编号有误!请重新输入");
sendCommand(map);
}
}
private void download(File distFile) throws IOException {
System.out.println("开始下载...");
BufferedInputStream bis = new BufferedInputStream(s.getInputStream());
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(distFile));
byte[] b = new byte[1024];
int len = 0;
while((len = bis.read(b)) != -1){
bos.write(b, 0, len);
}
bos.close();
bis.close();
System.out.println("下载完成!");
}
private void realSendComm(int inputNo) throws IOException {
PrintStream ps = new PrintStream(s.getOutputStream());
ps.println(String.valueOf(inputNo));
}
public static void main(String[] args) throws UnknownHostException, IOException {
File dist = new File("D:/");
Socket s = new Socket("192.168.46.26",10086);
new FtpClient(s,dist).init();
}
}
/**
* 服务端
*/
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.TreeMap;
public class FtpServer extends Thread{
private File dir;
private Socket s;
public FtpServer(File dir, Socket s) {
super();
this.dir = dir;
this.s = s;
}
@Override
public void run() {
sendList();
}
public void sendList(){
String[] filesName = dir.list();
TreeMap<Integer,String> files = new TreeMap<>();
for (int i = 0; i < filesName.length; i++) {
files.put(i+1, filesName[i]);
}
try {
ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
oos.writeObject(files);
oos.flush();
receiveCommand(files);
} catch (IOException e) {
e.printStackTrace();
}
}
public void receiveCommand(TreeMap<Integer,String> files) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
String s = br.readLine();
int fno = Integer.parseInt(s);
String fname = files.get(fno);
File source = new File(dir,fname);
trasFile(source);
}
private void trasFile(File source) throws IOException {
System.out.println("开始传输...");
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(source));
BufferedOutputStream bos = new BufferedOutputStream(s.getOutputStream());
byte[] b = new byte[1024];
int len = 0;
while((len = bis.read(b)) != -1){
bos.write(b, 0, len);
}
bos.close();
bis.close();
System.out.println("传输完成!");
}
public static void main(String[] args) {
try {
File dir = new File("D:\\larva搞笑虫子");
System.out.println("服务器已启动...");
ServerSocket ss = new ServerSocket(10086);
Socket s = null;
while(true){
s = ss.accept();
System.out.println("客户端已连接."+s.getInetAddress().getHostAddress());
new FtpServer(dir,s).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}