**********服务器作接收端*************************
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Receive //implements MouseListener
{
JFrame jf;
ServerSocket ss;
Socket socket;
public void Receive()
{
jf=new JFrame();
JButton btn1=new JButton("accept");
JButton btn2=new JButton("cancel");
jf.setLayout(new FlowLayout());
jf.add(btn1);
jf.add(btn2);
jf.setSize(200,200);
jf.setVisible(true);
btn1.addActionListener(new savefile());
btn2.addActionListener(new cancelfile());
try
{
ss=new ServerSocket(5000);
socket=ss.accept();
}
catch(IOException e1){System.out.println(e1);}
}
class savefile implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
InputStream insocket=null;
RandomAccessFile infile=null;
JFileChooser jfc=new JFileChooser();
jfc.showSaveDialog(jf);
File file=jfc.getSelectedFile();
String fileName = file.getAbsolutePath();
System.out.println("fileName:"+fileName);
try
{
if(file!=null)
{
infile=new RandomAccessFile(file,"rw");
}
byte[] data=new byte[1024];
insocket=socket.getInputStream();
int sum=insocket.read(data);
while(sum!=-1)
{
infile.write(data,0,sum);
} insocket.close();
System.out.println("接收完毕!");
socket.close();
ss.close();
infile.close();
}
catch(Exception ex)
{
System.out.println(ex);
}
}
}
class cancelfile implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
public static void main(String args[])
{
Receive receive=new Receive();
receive.Receive(); }
}
***********客户端做发送端****************
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.filechooser.FileFilter;
import javax.swing.event.*;
public class Send {
JFrame jf;
File file;
public void Send()
{
jf=new JFrame();
JButton btn=new JButton("send");
jf.add(btn);
jf.setSize(200,200);
jf.setVisible(true);
btn.addActionListener(new openfile());
}
class openfile implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
OutputStream outsocket;
RandomAccessFile outfile=null;
JFileChooser jfc=new JFileChooser();
jfc.showOpenDialog(jf);
file=jfc.getSelectedFile();
try
{
outfile=new RandomAccessFile(file,"r");
}
catch(Exception ex){System.out.println(ex);}
if(file!=null)
{
String filename = file.getAbsolutePath();
System.out.println("You choose to open this file: " +filename);
try
{
byte[] data=new byte[1024];
Socket socket=new Socket("127.0.0.1",5000);
outsocket=socket.getOutputStream();
int sum=outfile.read(data);
while(sum!=-1)
{
outsocket.write(data,0,sum);
}
System.out.println("Send File complete");
outsocket.close();
socket.close();
}
catch(Exception ex)
{
System.out.println(ex);
}
}
}
}
public static void main(String args[])
{
Send sendFile=new Send();
sendFile.Send();
}
}这两个程序分别运行后,文件传输中出现无限循环的发送字节流,而且停止不了
大侠帮忙看看啊
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Receive //implements MouseListener
{
JFrame jf;
ServerSocket ss;
Socket socket;
public void Receive()
{
jf=new JFrame();
JButton btn1=new JButton("accept");
JButton btn2=new JButton("cancel");
jf.setLayout(new FlowLayout());
jf.add(btn1);
jf.add(btn2);
jf.setSize(200,200);
jf.setVisible(true);
btn1.addActionListener(new savefile());
btn2.addActionListener(new cancelfile());
try
{
ss=new ServerSocket(5000);
socket=ss.accept();
}
catch(IOException e1){System.out.println(e1);}
}
class savefile implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
InputStream insocket=null;
RandomAccessFile infile=null;
JFileChooser jfc=new JFileChooser();
jfc.showSaveDialog(jf);
File file=jfc.getSelectedFile();
String fileName = file.getAbsolutePath();
System.out.println("fileName:"+fileName);
try
{
if(file!=null)
{
infile=new RandomAccessFile(file,"rw");
}
byte[] data=new byte[1024];
insocket=socket.getInputStream();
int sum=insocket.read(data);
while(sum!=-1)
{
infile.write(data,0,sum);
} insocket.close();
System.out.println("接收完毕!");
socket.close();
ss.close();
infile.close();
}
catch(Exception ex)
{
System.out.println(ex);
}
}
}
class cancelfile implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
public static void main(String args[])
{
Receive receive=new Receive();
receive.Receive(); }
}
***********客户端做发送端****************
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.filechooser.FileFilter;
import javax.swing.event.*;
public class Send {
JFrame jf;
File file;
public void Send()
{
jf=new JFrame();
JButton btn=new JButton("send");
jf.add(btn);
jf.setSize(200,200);
jf.setVisible(true);
btn.addActionListener(new openfile());
}
class openfile implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
OutputStream outsocket;
RandomAccessFile outfile=null;
JFileChooser jfc=new JFileChooser();
jfc.showOpenDialog(jf);
file=jfc.getSelectedFile();
try
{
outfile=new RandomAccessFile(file,"r");
}
catch(Exception ex){System.out.println(ex);}
if(file!=null)
{
String filename = file.getAbsolutePath();
System.out.println("You choose to open this file: " +filename);
try
{
byte[] data=new byte[1024];
Socket socket=new Socket("127.0.0.1",5000);
outsocket=socket.getOutputStream();
int sum=outfile.read(data);
while(sum!=-1)
{
outsocket.write(data,0,sum);
}
System.out.println("Send File complete");
outsocket.close();
socket.close();
}
catch(Exception ex)
{
System.out.println(ex);
}
}
}
}
public static void main(String args[])
{
Send sendFile=new Send();
sendFile.Send();
}
}这两个程序分别运行后,文件传输中出现无限循环的发送字节流,而且停止不了
大侠帮忙看看啊
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("关闭出错");}
}}
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
{
//bin.close();
dout.close();
din.close();
mySock.close();
}catch(Exception e){System.out.println("关闭出错");}
}}这是关于一个既定文件的传输程序,Server基于多线程,参考下,
传输之前,最好定义一个标志位,文件传输完毕,改变这个标志位。