DownloadClient:
packageday1901;
import java.io.BufferedInputStream;
importjava.io.BufferedOutputStream;
importjava.io.DataInputStream;
importjava.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.io.InputStream;
import java.io.ObjectInputStream;
importjava.io.OutputStream;
importjava.net.Socket;
importjava.util.Scanner;
publicclass DownloadClient {
Socket s;
InputStream in;
OutputStream out;
//下载文件的保存目录
File dir=new File("D:/data_bd");
publicvoid start(){
try {
Socket s=new Socket("176.136.12.23",8000);
in=s.getInputStream();
out=s.getOutputStream();
while(true){
receiveFileList(); //接收文件列表并显示
String str=new Scanner(System.in).nextLine();
if("q".equals(str.toLowerCase())){
s.close();
break;
}
intn=Integer.parseInt(str);//把字符串解析成int
sendNumber(n);//发送文件序号
FileInfo info=receiveFileInfo(); //接收文件信息
recieveFile(info);
System.out.println("文件下载完成");
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("连接已断开");
}
privatevoid receiveFileList() throws IOException { //接收文件列表
//DataInputStream--in
DataInputStream dis=new DataInputStream(in);
String s = dis.readUTF();
System.out.print(s);
}
privatevoid sendNumber(intn) throws IOException {
//DataOutputStream----out
DataOutputStream dos=new DataOutputStream(out);
dos.writeInt(n);
dos.flush();
}
private FileInfo receiveFileInfo() throws Exception, IOException {
//ObjectInputStream---in
ObjectInputStream ois=new ObjectInputStream(in);
return (FileInfo) ois.readObject();
}
privatevoid recieveFile(FileInfo info) throws IOException {
if(! dir.exists()){//判断下载文件夹是否存在,不存在则创建
dir.mkdirs();//目录不存在,创建目录
}
//dir目录下的info.getName(文件)
File file = new File(dir,info.getName());
//字节计数变量
intcount=0;
//BOS----FOS----file
BufferedOutputStream bos=new BufferedOutputStream(newFileOutputStream(file));
//单字节从网络接收
intb;
while((b=in.read())!=-1){
bos.write(b);//向文件输出数据
count++;
if(count==info.getSize()){
break;//文件数据接收结束
}
}
bos.close();
file.setLastModified(info.getTime());//修改文件最后修改时间
System.out.println("文件已保存"+file.getAbsolutePath());//显示文件绝对路径
}
publicstaticvoid main(String[] args) {
DownloadClient client=new DownloadClient();
client.start();
}
}
DownloadServer:
packageday1901;
importjava.io.DataInputStream;
importjava.io.DataOutputStream;
import java.io.File;
importjava.io.FileFilter;
importjava.io.FileInputStream;
import java.io.FileNotFoundException;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.ObjectOutputStream;
importjava.io.OutputStream;
importjava.net.ServerSocket;
importjava.net.Socket;
publicclass DownloadServer {
//给客户端下载的文件的目录
File dir=new File("D:/abc");
publicvoid start(){
new Thread() { //新建线程的固定写法
@Override
publicvoid run() {
try{
ServerSocket ss = new ServerSocket(8000);
System.out.println("服务器已启动");
while(true){
Socket s=ss.accept();
TongXinThread t = new TongXinThread(s);
t.start();
}
}catch(Exception e){
System.out.println("服务器已停止");
}
}
}.start();
}
class TongXinThread extends Thread{
Socket s;
//原始网络流
InputStream in;
OutputStream out;
public TongXinThread(Socket s) {
super();
this.s = s;
}
@Override
publicvoid run() {
try {
in=s.getInputStream();
out=s.getOutputStream();
//获取文件的列表
File[] files=listFiles();
while(s.isConnected()){
sendFileList(files);//发送文件列表
intn=receiveNumber()-1;//接收文件序号
sendFileInfo(files,n);//发送文件信息
sendFileData(files,n);//发送文件数据
//提示
System.out.println("文件发送完成 "+files[n].getName());
}
} catch (Exception e) {
e.printStackTrace();//打印异常
}
System.out.println("连接已断开");
}
privatevoid sendFileInfo(File[] files, intn) throws IOException {
FileInfo info=new FileInfo();
info.setName(files[n].getName()); //文件名
info.setSize(files[n].length()); //文件大小
info.setTime(files[n].lastModified()); //最后的修改时间
//序列化输出 ObjectOutputStream----out
ObjectOutputStream oos=new ObjectOutputStream(out);
oos.writeObject(info); //把info对象序列号写入oos
oos.flush(); //刷新缓存
}
privatevoid sendFileData(File[] files, intn) throws IOException {
//发送文件数据,把磁盘文件的数据发送网络流
//创建文件的一个输入流,直接插在文件上,FileInputStream----files[n]
FileInputStream fis=new FileInputStream(files[n]);
byte[] buff=newbyte[8192];
intm;
while((m=fis.read(buff))!=-1){
//从文件读的数据,向网络远程发送
out.write(buff, 0, m);
}
fis.close();
out.flush(); //刷新缓存 网络输出流
}
privateint receiveNumber() throws IOException {
//DataInputStream---in
DataInputStream dis=new DataInputStream(in);
returndis.readInt();
}
privatevoid sendFileList(File[] files) throws IOException {
StringBuilder sb=new StringBuilder();//用于字符串连接
sb.append("------------------\n");
for(inti=0;i<files.length;i++){
File file=files[i];
sb.append(i+1+"."+file.getName()+"\n");
}
sb.append("------------------\n");
sb.append("选择(q退出):");
///
//--DataOutputString---out
DataOutputStream dos=new DataOutputStream(out);
dos.writeUTF(sb.toString());
dos.flush();
}
private File[] listFiles() {
//只列出文件不列出子目录
return
dir.listFiles(new FileFilter() { //文件过滤器
@Override
publicboolean accept(File f) {
returnf.isFile();
}
});
}
}
publicstaticvoid main(String[] args) {
DownloadServer server=new DownloadServer();
server.start();
}
}
FileInfo:
packageday1901;
importjava.io.Serializable;
publicclass FileInfo implements Serializable {
privatestaticfinallongserialVersionUID=1L;
private String name; //文件名
privatelongsize; //文件大小
privatelongtime; //修改时间
public String getName() {
returnname;
}
publicvoid setName(String name) {
this.name = name;
}
publiclong getSize() {
returnsize;
}
publicvoid setSize(longsize) {
this.size = size;
}
publiclong getTime() {
returntime;
}
publicvoid setTime(longtime) {
this.time = time;
}
}