下面分别为发送端与接收端的具体实现:
发送端:
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
public class Client {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
int lenRead=0;
double sumL=0;
boolean IsSuccess=false;
DataOutputStream dos=null;
FileInputStream fis=null;
Socket socket=null;
try{
File file=new File("D:/cocosAndpython/workspace/TestSocket/src/TransData.xml");
long lenth=file.length();
socket=new Socket();
socket.connect(new InetSocketAddress("127.0.0.1",48123));
dos=new DataOutputStream(socket.getOutputStream());
fis=new FileInputStream(file);
byte[] sendBytes=new byte[1024];
while((lenRead=fis.read(sendBytes, 0, sendBytes.length))>0){
sumL+=lenRead;
System.out.println("已传输: "+((sumL/lenth)*100)+"%");
dos.write(sendBytes,0,(int)lenth);
}
dos.flush();
if(sumL==lenth){ //会自动地进行转换
IsSuccess=true;
}
}catch(Exception e){
System.out.println("客户端传送文件失败!");
IsSuccess=false;
e.printStackTrace();
}
finally{
if(dos!=null){
dos.close();
}
if(fis!=null){
fis.close();
}
if(socket!=null){
socket.close();
}
}
System.out.println(IsSuccess?"成功":"失败");
}
}
接收端:
import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Random;
public class Server {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
final ServerSocket server=new ServerSocket(48123);
Thread thread=new Thread(new Runnable(){
public void run() {
// TODO Auto-generated method stub
while(true){
try{
System.out.println("开始监听...");
Socket socket=server.accept();
System.out.println("有连接!");
receiveFile(socket);
}catch(Exception e){
System.out.println("服务器异常!");
}
}
}
});
thread.run();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void receiveFile(Socket socket) throws IOException{
byte[] receiveByte=null;
int length=0;
DataInputStream dis=null;
FileOutputStream fos=null;
String filePath="D:/temp/"+"ZYQ"+new Random().nextInt(10000)+".xml";
try{
dis=new DataInputStream(socket.getInputStream());
File file=new File("D:/temp");
if(!file.exists()){
file.mkdir();
}
fos=new FileOutputStream(new File(filePath));
receiveByte=new byte [1024];
System.out.println("开始接受数据...");
while((length=dis.read(receiveByte,0,receiveByte.length))>0){
fos.write(receiveByte,0,length);
fos.flush();
}
System.out.println("完成接受: "+filePath);
}
catch(Exception e){
e.printStackTrace();
}
finally{
if(fos!=null){
fos.close();
}
if(dis!=null){
dis.close();
}
if(socket!=null){
socket.close();
}
}
}
}
其中传输文件TransData.xml内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<Accounts>
<Account type="type1">
<code>100001</code>
<pass>123</pass>
<name>李四</name>
<money>1000000.00</money>
</Account>
<Account type="type2">
<code>100002</code>
<pass>123</pass>
<name>张三</name>
<money>1000.00</money>
</Account>
</Accounts>