如何socket传送文件

zmq2 2006-03-14 07:17:00
我想编一个可以在两台机子上传送文件的程序,哪位高手知道怎么用socket来传文件啊,到底要用什么流.这个程序我急着要用,如果有人能帮我写好接受端和发送端,送100分.
...全文
915 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
gujing01 2006-03-16
  • 打赏
  • 举报
回复
本质上就是把从fileinputstream里读,写到SOCKET的outputstream,接受端相反
kill8108 2006-03-16
  • 打赏
  • 举报
回复
是的,它们的底层都是TCP/IP协议来的,socket已实现了这个协议的标准了的吧!
讨论!
hongke1490 2006-03-14
  • 打赏
  • 举报
回复
我也来写一个
客户端:
import java.io.*;
import java.net.*;

public class Client
{
public static void main(String args[])
{
Client client=new Client();
// client.sendMsg("Hello world!");
client.sendFile("d:\\a.gif");
}

public void sendFile(String filename)
{
File file=new File(filename);
if(!file.exists())
return ;
Socket sl;
OutputStream slout;
DataOutputStream dos;
try
{
DataInputStream dis = new DataInputStream(new FileInputStream(file));
byte[] line=new byte[1024];
int loop=dis.available()/1024+1;
for(int i=0;i<loop;i++)
{
dis.read(line);
sl = new Socket(InetAddress.getLocalHost(), 5432);
slout = sl.getOutputStream();
dos = new DataOutputStream(slout);
dos.write(line);
Thread.sleep(50);
dos.close();
slout.close();
sl.close();
line=new byte[1024];
}
sl = new Socket(InetAddress.getLocalHost(), 5432);
slout = sl.getOutputStream();
dos = new DataOutputStream(slout);
dos.write("quit".getBytes());
dos.close();
slout.close();
sl.close();
dis.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
服务端:
import java.net.*;
import java.io.*;

public class Server
{
public static void main(String args[])
{
Server server=new Server();
server.receiveFile();
}

public void receiveFile()
{
ServerSocket s = null;
Socket sl;
InputStream slIn;
DataInputStream dis;
DataOutputStream dos=null;
try
{
s = new ServerSocket(5432);
File file=new File("e:\\a.gif");
dos = new DataOutputStream(new FileOutputStream(file));
}
catch (Exception e)
{
System.out.println(e);
}
while (true)
{
try
{
sl = s.accept();
slIn = sl.getInputStream();
dis = new DataInputStream(slIn);
byte[] st = new byte[1024];
dis.read(st);
dis.close();
slIn.close();
sl.close();
if(new String(st).startsWith("quit"))
break;
dos.write(st);
dos.flush();
}
catch (Exception e)
{
System.out.println(e);
}
}
try
{
dos.close();
}
catch (Exception ex)
{
System.out.println(ex);
}
}
}
Kyori_YR 2006-03-14
  • 打赏
  • 举报
回复
问一下,如何覆盖已存在的文件啊?
luanfengxia 2006-03-14
  • 打赏
  • 举报
回复
其实ftp也是socket为什么不用呢?
好像这样比较标准哦
f_acme 2006-03-14
  • 打赏
  • 举报
回复
我从我的代码中copy出来的,可能某些变量没有定义或者其他的问题,不过大致就这样子了。
public void acceptFile (BufferedOutputStream bos,Socket sk)
{
//线程运行实体
BufferedReader in = null;
DataInputStream ins = null;
try{
InputStreamReader isr;
isr = new InputStreamReader (sk.getInputStream ());
in = new BufferedReader (isr);
ins = new DataInputStream(new BufferedInputStream(sk.getInputStream()));
byte[] buf= new byte[65536];
indata=0;

inti=System.currentTimeMillis();
indata=ins.read(buf);
while(indata!=-1){
bos.write(buf,0,indata);
bos.flush();
indata = ins.read(buf);
}
setEnableAll(true);
ifcom=false;
jd1.dispose();
bos.close();
ins.close();

}catch (IOException e)
{
System.out.println (e.toString ()+"传输出现异常");
}
finally
{
try{
if (in != null)
in.close ();
if (sk != null)
sk.close ();
}catch (IOException e)
{
System.out.println("流设备关闭错误!");
}

}
}
}



public void fileSend ()
{
try{
//使用port端口
sk = new Socket ();
InetSocketAddress ia = new InetSocketAddress(addr,port);
sk.connect(ia,2000);

InputStreamReader isr;
isr = new InputStreamReader (sk.getInputStream ());
in = new BufferedReader (isr);
//建立输出
out = new PrintWriter (
new BufferedWriter(
new OutputStreamWriter(
sk.getOutputStream ())), true);
outs = new DataOutputStream(new BufferedOutputStream(sk.getOutputStream()));
File file = new File(filename);
long flength = file.length();
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
byte[] buf = new byte[65536];
outdata=0;
n = fis.available();
inti=System.currentTimeMillis();
outdata=bis.read(buf);

while(outdata!=-1){
outs.write(buf,0,outdata); //发送数据
outs.flush();
outdata=bis.read(buf);
}
jb1.setEnabled(true);
if(bis!=null)bis.close();
}
catch (IOException e)
{
JOptionPane.showMessageDialog(null,"连接出错!");
jl3.setText("连接服务器出错!网络错误!请重试!");
jb1.setEnabled(true);
}
finally
{
//释放资源
try
{
if (in != null) in.close ();
if (out != null) out.close ();
if (sk != null) sk.close ();
}
catch (IOException e)
{
}
}
}
treeroot 2006-03-14
  • 打赏
  • 举报
回复
字节流
冯立彬 2006-03-14
  • 打赏
  • 举报
回复
這個其實和文件的拷貝沒有什么差別
你如果不懂文件拷貝這個理解起來就很困難
我給你一個文件拷貝的函數吧
你可以先看看這個
因為這個簡單一點
容易上手:
//Copy File
//Author:fenglibin
package FileCopy;

import java.io.*;

public class FileCopy
{
FileInputStream FIS;
FileOutputStream FOS;

public boolean copyFile(String src, String des)
{
try
{
FIS = new FileInputStream(src);
FOS = new FileOutputStream(des);
byte[] bt = new byte[1024];
int readNum = 0;
while ((readNum = FIS.read(bt)) != -1)
{
FOS.write(bt, 0, bt.length);
}
FIS.close();
FOS.close();
return true;
}
catch (Exception e)
{
try
{
FIS.close();
FOS.close();
}
catch (IOException f)
{
// TODO
}
return false;
}
finally
{
}
}
}
文件傳送的也很簡單的
首先建立連接
然后一個用輸出流輸出,一個用輸入流接收
和拷貝差不多
只不過中間有一個SOCKET的橋梁作用

62,629

社区成员

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

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