如何socket传送文件
我想编一个可以在两台机子上传送文件的程序,哪位高手知道怎么用socket来传文件啊,到底要用什么流.这个程序我急着要用,如果有人能帮我写好接受端和发送端,送100分. 问题点数:100、回复次数:8Top
1 楼treeroot(旗鲁特)回复于 2006-03-14 08:56:20 得分 0
字节流Top
2 楼f_acme(沧海一声笑)回复于 2006-03-14 09:09:03 得分 100
我从我的代码中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)
{
}
}
}Top
3 楼Kyori_YR()回复于 2006-03-14 09:47:44 得分 0
问一下,如何覆盖已存在的文件啊?Top
4 楼hongke1490(oracle)回复于 2006-03-14 10:15:08 得分 0
我也来写一个
客户端:
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);
}
}
}Top
5 楼fenglibing(流星)回复于 2006-03-14 16:16:28 得分 0
這個其實和文件的拷貝沒有什么差別
你如果不懂文件拷貝這個理解起來就很困難
我給你一個文件拷貝的函數吧
你可以先看看這個
因為這個簡單一點
容易上手:
//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的橋梁作用Top
6 楼luanfengxia(流风)回复于 2006-03-14 21:39:12 得分 0
其实ftp也是socket为什么不用呢?
好像这样比较标准哦Top
7 楼kill8108(日月之光)回复于 2006-03-16 14:42:19 得分 0
是的,它们的底层都是TCP/IP协议来的,socket已实现了这个协议的标准了的吧!
讨论!Top
8 楼gujing01(巴洛克)回复于 2006-03-16 18:19:07 得分 0
本质上就是把从fileinputstream里读,写到SOCKET的outputstream,接受端相反Top




