服务端
package com.cotel.mina;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.DecimalFormat;
import java.util.Date;
public class TestServer {
/**
* @ClassName: TestServer
* @Description: 服务端
* @author Yanghui
* @date 2010-5-13 上午11:25:08
*
*/
public static void main(String[] agrs) {
try {
ServerSocket serverSocket = new ServerSocket(8899);
while(true){
Socket socket = serverSocket.accept();
System.out.println("客户端发送信息>>>>>");
DataInputStream inputStream = new DataInputStream(socket.getInputStream());
DataOutputStream outputStream=new DataOutputStream(socket.getOutputStream());
String result = inputStream.readUTF();
System.out.println("读取客户端数据:" + result);
System.out.println("返回信息给客户端成功>>>>>");
outputStream.writeUTF("已经收到信息");
byte[] b=new byte[1000000];
StringBuffer saveFile=new StringBuffer("E:\\");
saveFile.append(inputStream.readUTF());
File file=new File(saveFile.toString());
FileOutputStream fileOutputStream=new FileOutputStream(file);
System.out.println("收到文件,正在下载文件>>>>>");
long length=inputStream.readLong();
DecimalFormat format=new DecimalFormat("###,##0");
format.setMinimumFractionDigits(2);
double stat=new Date().getTime();
while(true)
{
int read= inputStream.read(b);
if(read==-1)
{
break;
}
fileOutputStream.write(b,0,read);
System.out.println("已经接收:"+format.format(((double)file.length()/length)*100)+"%");
}
double end=new Date().getTime();
System.out.println("消耗时间:"+(end-stat)/1000);
System.out.println("接受文件完成>>>>");
fileOutputStream.flush();
fileOutputStream.close();
outputStream.flush();
outputStream.close();
inputStream.close();
socket.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
客户端
package com.cotel.mina;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
/**
* @ClassName: TestClient
* @Description: 客户端
* @author Yanghui
* @date 2010-5-13 上午11:30:30
*
*/
public class TestClient {
public static void main(String[] args) {
try {
System.out.println("请求连接服务器......");
Socket socket=new Socket("localhost", 8899);
System.out.println("连接服务器成功>>>>>");
DataOutputStream outputStream=new DataOutputStream(socket.getOutputStream());
DataInputStream inputStream=new DataInputStream(socket.getInputStream());
System.out.println("向服务器发送信息......");
outputStream.writeUTF("我最近还不错");
System.out.println("向服务器发送信息成功>>>>>");
System.out.println("接受服务器返回信息>>>>");
System.out.println("返回信息:"+inputStream.readUTF());
System.out.println("请求发送文件>>>>>>");
File file=new File("F:\\书.rar");
System.out.println("文件总长度:"+file.length());
byte[] b=new byte[1000000];
FileInputStream fileInputStream=new FileInputStream(file);
outputStream.writeUTF(file.getName());
outputStream.writeLong(file.length());
while(true)
{
//返回读入的字节长度,其实就是b数组的长度
int read=fileInputStream.read(b);
//都完以后返回-1
if(read==-1)
{
break;
}
//将指定字节数组中从偏移量 0 开始的 read 个字节写入基础输出流。
outputStream.write(b,0,read);
}
System.out.println("发送完成>>>>>");
outputStream.flush();
outputStream.close();
inputStream.close();
socket.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Socket 发送文件
最新推荐文章于 2022-11-20 18:32:47 发布