用Socket进行文件的传输及保存

pengpeng11 2008-04-11 12:23:36
用socket实现 客户端读取本地文件并发送;服务器接收文件并保存到本地文件系统中,哪位大哥教教我啊,谢谢!
...全文
141 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
frilly 2008-04-11
  • 打赏
  • 举报
回复
import java.net.*;
import java.io.*;
public class Server
{
private ServerSocket server;
private Socket you;
private receive rec;
public Server()
{
try
{
server = new ServerSocket(2007);
System.out.println( "服务器运行中,监听端口:2007...... ");
while(true)
{
you = server.accept();
if(you != null)
{
System.out.println( "有客户连接启动接收线程... ");
System.out.println( "接收文件中...... ");
new Thread(new receive(you)).start();

}
}
}catch(Exception e){System.out.println( "服务端运行出错! ");}
}

public static void main(String args[])
{
new Server();
}
}

class receive implements Runnable
{
private File files;
private DataInputStream din = null;
private DataOutputStream dout = null;
private Socket mySock = null;
private byte data[] = new byte[1024];
public receive(Socket you)
{
this.mySock = you;

}

public void run()
{
try
{
din = new DataInputStream(new BufferedInputStream(mySock.getInputStream()));
dout = new DataOutputStream(new BufferedOutputStream(mySock.getOutputStream()));
String fileName = din.readUTF();
BufferedOutputStream writeFile = new BufferedOutputStream(
new FileOutputStream(fileName));


while(din.readInt() != 0)
{
din.read(data);
writeFile.write(data,0,data.length);
}
writeFile.close();
clears();
System.out.println( "接收文件完毕!---> 服务器继续监听... ");
}catch(Exception e){System.out.println( "接收出错! ");}
finally
{
clears();
}

}

void clears()
{
try
{
dout.close();
din.close();
mySock.close();
}catch(Exception e){System.out.println( "关闭出错 ");}
}

}

去年回答别人的问题,呵呵
先运行Server 编译运行通过,参考下
frilly 2008-04-11
  • 打赏
  • 举报
回复
import java.net.*;
import java.io.*;
import javax.swing.*;
public class Client
{
private Socket you;
SendData sends;
JFileChooser select;
File file;
String address = null;
public Client()
{
try
{ select = new JFileChooser();
System.out.println( "输入服务器IP地址: ");
BufferedReader bin = new BufferedReader(new InputStreamReader(System.in));
address = bin.readLine();
while(address == " " || address.length() == 0 || address == null)
{
System.out.println( "输入错误,请重新输入:( ");
address = bin.readLine();
}
bin.close();
you = new Socket(address,2007);
System.out.println( "连接服务器监听端口:2007...... ");
if(you != null)
{
System.out.println( "连接服务器成功,请选择文件:> ");
select.showOpenDialog(null);
file = select.getSelectedFile();
new Thread(new SendData(you,file)).start();
System.out.println( "发送完毕!关闭线程... ");
}
}catch(Exception e){System.out.println( "服务端运行出错! ");}
}

public static void main(String args[])
{
new Client();
}
}

class SendData implements Runnable
{
private File files;
private BufferedInputStream readFile = null;
private DataInputStream din = null;
private DataOutputStream dout = null;
private Socket mySock = null;
private String fileName;
private byte data[] = new byte[1024];

public SendData(Socket you,File file)
{
this.mySock = you;
files = file;
fileName = files.getName();
}

public void run()
{
try
{
readFile = new BufferedInputStream(new FileInputStream(files));
din = new DataInputStream(new BufferedInputStream(mySock.getInputStream()));
dout = new DataOutputStream(new BufferedOutputStream(mySock.getOutputStream()));
dout.writeUTF(fileName);
while((readFile.read(data)) != -1)
{
dout.writeInt(1);
dout.write(data,0,data.length);
dout.flush();
}
readFile.close();
dout.writeInt(0);

clears();
}catch(Exception e){}
finally
{
clears();
}

}

void clears()
{
try
{
dout.close();
din.close();
mySock.close();
}catch(Exception e){System.out.println( "关闭出错 ");}
}

}


薛定谔之死猫 2008-04-11
  • 打赏
  • 举报
回复
http://www.java3z.com/cwbwebhome/article/article5/5213.html?id=679

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧