Java编写网络文件传输_利用Java编写一个程序利用Socket和ServerSocket类,实现网络的点对点文件传输...

这个时比较简单的。package QQ;import java.net.*;import java.io.*;import java.sql.*;import java.util.*;public class QQServer {public static void main(String args[]) {e69da5e887aa62616964757a686964616f31333264646463try {HashMap hm = new HashMap() ;// 服务器到8000端口监听(1)ServerSocket ss = new ServerSocket(8000);while (true) {System.out.println("服务器正在8000端口监听.....");Socket s = ss.accept();MyService t = new MyService();t.setSocket(s) ;t.setHashMap(hm) ;t.start();}} catch (Exception e) {e.printStackTrace();}}}class MyService extends Thread {private Socket s ;private HashMap hm ;public void setHashMap(HashMap hm){this.hm = hm ;}public void setSocket(Socket s){this.s = s ;}public void run() {try {// 接收客户端发送来的用户名和密码(2)InputStream is = s.getInputStream();InputStreamReader isr = new InputStreamReader(is);BufferedReader br = new BufferedReader(isr);String uandp = br.readLine();// 拆分用户名和密码(4)String u = "";String p = "";try{u = uandp.split("%")[0];p = uandp.split("%")[1];}catch(Exception e){}// 到数据库中验证(5)Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");Connection cn = DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1:1433;databasename=qq2","sa", "123");PreparedStatement ps = cn.prepareStatement("select username from username where username=? and password=?");ps.setString(1, u);ps.setString(2, p);ResultSet rs = ps.executeQuery();// 发送确认信息到客户端(7)OutputStream os = s.getOutputStream();OutputStreamWriter osw = new OutputStreamWriter(os);PrintWriter pw = new PrintWriter(osw, true);if (rs.next()) {pw.println("ok");//将自己的名字发送给HashMap中的其他人(13)for(Socket ts : hm.values()){OutputStream tos = ts.getOutputStream() ;OutputStreamWriter tosw = new OutputStreamWriter(tos) ;PrintWriter tpw = new PrintWriter(tosw , true) ;tpw.println("user%"+u) ;}//将其他人的名字发送给自己(13)for(String tu : hm.keySet()){pw.println("user%"+tu) ;}//将用户名和对应的Socket存入HashMap(13)hm.put(u, s) ;// 接收客户端发送来的信息(11)while (true) {String message = br.readLine();if(message.equals("exit")){for(Socket ts : hm.values()){OutputStream tos = ts.getOutputStream() ;OutputStreamWriter tosw = new OutputStreamWriter(tos) ;PrintWriter tpw = new PrintWriter(tosw , true) ;tpw.println("exit%"+u);}hm.remove(u);}String to = message.split("%")[0];String mess = message.split("%")[1];Socket ts = hm.get(to);OutputStream tos = ts.getOutputStream();OutputStreamWriter tosw = new OutputStreamWriter(tos);PrintWriter tpw = new PrintWriter(tosw, true);tpw.println("mess%"+mess+"\n");System.out.println(message);}} else {pw.println("err");}} catch (Exception e) {}}},我倒是有简单的QQ和聊天室,比较基础的那种,刚学的时候写的,如果需要的话,留一下邮箱或QQ追问那麻烦你发给我下,920421387@qq.com,谢谢了来自:求助得到的回答,0分 让人帮你写········www.mh456.com防采集。

客户端代码:package net;import java.net.*;import java.io.*;public class Client {private Socket clientEndPoint;private InputStream in;private OutputStream out;public void connectToService() throws IOException, InterruptedException {62616964757a686964616fe4b893e5b19e31333332633633clientEndPoint = new Socket("127.0.0.1", 9001);//根据实际情况修改此处地址,本地测试默认为“127.0.0.1”in = clientEndPoint.getInputStream();out = clientEndPoint.getOutputStream();DataInputStream dis = new DataInputStream(in);DataOutputStream dos = new DataOutputStream(out);BufferedReader br = new BufferedReader(new InputStreamReader(System.in));System.out.println("Server is ready. Please type in file to get: ");String request;while (true) {System.out.print("Get: ");request = br.readLine() + "\r\n";dos.writeUTF(request);//输入“bye”、“exit”“quit”则退出if (request.trim().equals("bye") || request.trim().equals("exit")|| request.trim().equals("quit"))break;while (true) {if (in != null) {System.out.println("Getting file(s)...");int i;long count = 0;boolean fileExist = dis.readBoolean();if (fileExist) {File f = getTargetFile(request.trim());FileOutputStream fos = new FileOutputStream(f);long fileLength = dis.readLong();byte[] buf = new byte[1024];System.out.println(fileLength);while (true) {i = dis.read(buf, 0, 1024);fos.write(buf, 0, i);count += i;if (count >= fileLength) {break;}}fos.close();}break;}}System.out.println(dis.readUTF());}System.out.println("Client ready to close...");Thread.sleep(5000);dis.close();dos.close();clientEndPoint.close();}private File getTargetFile(String path) {path = path.replace("/", "\\");path = path.substring(path.lastIndexOf("\\") + 1);return new File(path);}public static void main(String[] args) {try {new Client().connectToService();} catch (Exception e) {e.printStackTrace();}}}

主要是你通过socket api封装要发送的数据,内部会自动封装成数据流进行传输。 1,什么是Socket 网络上的两个程序通过一个双向的通讯连接实现数据的交换,这个双向链路的一端称为一个Socket。Socket通常用来实现客户方和服务方的连接。

u=919984888,379278956&fm=214&gp=0.jpg

服务器端代码,32313133353236313431303231363533e58685e5aeb931333332633633支持多用户访问,端口号9001:package net;import java.net.*;import java.io.*;public class Server extends Thread {private ServerSocket serverSocket;private Socket serverEndPoint;private InputStream in;private OutputStream out;public Server() {}private Server(Socket serverEndPoint) {this.serverEndPoint = serverEndPoint;}public void startService(int port) throws IOException, InterruptedException {System.out.println("service started...");serverSocket = new ServerSocket(port);while (true)new Server(serverSocket.accept()).start();}public void run() {try {in = serverEndPoint.getInputStream();out = serverEndPoint.getOutputStream();DataInputStream dis = new DataInputStream(in);DataOutputStream dos = new DataOutputStream(out);String request;while (true) {if ((request = dis.readUTF()) != null) {if (request.trim().equals("bye")|| request.trim().equals("exit")|| request.trim().equals("quit"))break;System.out.println(serverEndPoint.getInetAddress().getCanonicalHostName()+ " is getting '" + request.trim() + "'...");File f = new File(request.trim());if (!f.exists()) {dos.writeBoolean(false);dos.writeUTF("File '" + f.getPath()+ "' doesn't exist!");continue;}if (f.isDirectory()) {dos.writeBoolean(false);dos.writeUTF("'" + f.getPath() + "' is a folder!");continue;}FileInputStream fis = new FileInputStream(f);int i;int count = 0;byte[] buf = new byte[1024];dos.writeBoolean(true);dos.writeLong(f.length());while ((i = fis.read(buf)) != -1) {dos.write(buf, 0, i);count += i;}fis.close();Thread.sleep(1000);dos.writeUTF("File '" + f.getName()+ "' has been saved in the current folder.");}}System.out.println("Connection to "+ serverEndPoint.getInetAddress().getCanonicalHostName()+ " is ready to close...");Thread.sleep(5000);dis.close();dos.close();serverEndPoint.close();} catch (Exception e) {e.printStackTrace();}}public static void main(String[] args) {try {new Server().startService(90011);} catch (Exception e) {e.printStackTrace();}}}

Server端: import java.io.*; import java.net.*; import java.applet.Applet; public class TalkServer{ public static void main(String args[]) { try{ ServerSocket server=null; try{ server=new ServerSocket(4700); }catch(Exception e)

a89671a5a6f8f37fa207f29f6e1c4b21.gif

内容来自www.mh456.com请勿采集。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值