我从我的代码中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;
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)
{
}
}
}
客户端:
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);
}
}
}
你如果不懂文件拷貝這個理解起來就很困難
我給你一個文件拷貝的函數吧
你可以先看看這個
因為這個簡單一點
容易上手:
//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的橋梁作用
好像这样比较标准哦
讨论!