import java.io.*; import java.net.*; import java.util.*; public class Server { public static void main(String args[]){ try { ServerSocket server = new ServerSocket(8888); System.out.println(\"服务器在监听8888端口...\"); while (true) { Socket socket = server.accept(); new ReceFile(socket).start(); } } catch (Exception e) { e.printStackTrace(); } } }
//服务器接收文件线程
class ReceFile extends Thread { private Socket socket; public ReceFile(Socket socket){ this.socket = socket; } public void run(){ InputStream is = null; OutputStream os = null; DataInputStream dis = null; InetAddress address = null; String ip = \"\"; try { //获得输入流
is = socket.getInputStream(); //获取连接此输入流的地址
address = socket.getInetAddress(); //返回ip
ip = address.getHostAddress();
dis = new DataInputStream(is); //读取上传过过来的文件名 String fileName = dis.readUTF();
System.out.println(\"服务器接收到自\"+ip+\"的\"+fileName+\"文件\"); //将接收到的文件保存到指定的目录
os = new FileOutputStream(\"File/\"+fileName);
int len = 0; byte[] b = new byte[1024]; while (true) { len = is.read(b); if (len==-1) { break; } os.write(b,0,len); os.flush(); } }
catch (Exception e) { e.printStackTrace(); }
finally{ try { if (is!=null) { is.close(); } if (dis!=null) { dis.close(); } if (os!=null)
{ os.close(); } } catch (Exception ex) { ex.printStackTrace(); } } } }
import java.io.*; import java.net.*; import java.util.*; import java.awt.*;
import java.awt.event.*; import javax.swing.*; import org.roy.*;
public class Client extends JFrame implements ActionListener { private JButton selbutton,upbutton; private JPanel jp; private String path; public Client(){ super(\"文件上传\"); this.setSize(250,250); this.setLocation(300,400); init(); this.setVisible(true); this.setDefaultCloseOperation(EXIT_ON_CLOSE); } public void init(){ selbutton = new JButton(\"选择文件\"); upbutton = new JButton(\"上 传\"); jp = new JPanel(); selbutton.addActionListener(this);
upbutton.addActionListener(this); jp.add(selbutton); jp.add(upbutton); this.add(jp); } public void actionPerformed(ActionEvent e){ if(e.getSource()==selbutton){ //使用包 路径 path =IOUtil.fetch().getPathFile().getAbsolutePath(); } if(e.getSource()==upbutton){ System.out.println(\"发送文件\"); if(path!=null&&!path.equals(\"\")){ //在后台运行的 new SendFile(path).start();//开启一个线程 }else{ System.out.println(\"请选择文件..\"); } } } public static void main(String args[]){ new Client(); } }
class SendFile extends Thread { private Socket socket; private String path; public SendFile(String path){ this.path = path; } public void run(){ InputStream is = null; OutputStream os = null; File file = null; Properties pro = null; try
{ file = new File(path); if (file.exists()) { is = new FileInputStream(file); InputStream ispro = new FileInputStream(\"ip.properties\"); pro = new Properties(); //从输入流中读取属性列表(键和元素对)。 pro.load(ispro); socket =
Socket(pro.getProperty(\"ip\"),Integer.parseInt(pro.getProperty(\"port\"))); os = socket.getOutputStream(); String fileName = file.getName(); DataOutputStream dos = new DataOutputStream(os); dos.writeUTF(fileName); int len = 0; byte[] b = new byte[1024]; while (true) { len = is.read(b); if (len==-1) { break; } os.write(b,0,len); os.flush(); } } } catch (Exception e) { e.printStackTrace(); } finally{ try { if (is!=null)
new
}
}
}
{ is.close(); } if (os!=null) { os.close(); } socket.close(); }
catch (Exception ex) { ex.printStackTrace(); }