java socket 传送文件
版本一: **只能传文件,服务器会自动命名。 server
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 import java.io.*;import java.net.ServerSocket;import java.net.Socket;public class FileServer { public static void main (String[] args) { try { ServerSocket ss = new ServerSocket(9999 ); System.out.println("服务器开启,等待客户端连接..." ); while (true ) { Socket s = ss.accept(); new Filetask(s).start(); } } catch (IOException e) { e.printStackTrace(); } } } class Filetask extends Thread { private InputStream is; private Socket s; public Filetask (Socket s) { this .s = s; try { this .is = s.getInputStream(); } catch (IOException e) { e.printStackTrace(); } } @Override public void run () { try { FileOutputStream fos = new FileOutputStream("D:\\wepull\\ServerReceive\\" + System.currentTimeMillis()+"1.rar" ); byte [] buf = new byte [1024 ]; int len = 0 ; while ((len = is.read(buf)) != -1 ) { fos.write(buf, 0 , len); } System.out.println("已经成功接收来自" +s.getInetAddress()+"的文件" ); fos.flush(); fos.close(); is.close(); s.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
client
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 import java.io.*;import java.net.Socket;import java.util.Scanner;public class FileClient { public static void main (String[] args) { Scanner sc = new Scanner(System.in); String f = sc.nextLine(); File file = new File(f); if (file.exists()) { try { Socket s = new Socket("127.0.0.1" , 9999 ); OutputStream os = s.getOutputStream(); FileInputStream fis = new FileInputStream(file); byte [] buf = new byte [1024 ]; int len = 0 ; while ((len = fis.read(buf)) != -1 ) { os.write(buf, 0 , len); } os.flush(); fis.close(); os.close(); } catch (IOException e) { e.printStackTrace(); } } else { System.out.println("文件不存在!" ); } } }
版本二: 客户端发送文件名和文件大小,服务器会设置客户端发来的文件名,并检测文件传输的完整性。 server
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 import java.io.*;import java.net.InetAddress;import java.net.ServerSocket;import java.net.Socket;import java.util.HashMap;import java.util.Map;public class FServer { private static Map<InetAddress,File> ip_file_map = new HashMap<InetAddress, File>(); private static Map<InetAddress,Long> ip_file_length_map = new HashMap<InetAddress, Long>(); public static void main (String[] args) { try { ServerSocket fss = new ServerSocket(30001 ); ServerSocket dss = new ServerSocket(30002 ); System.out.println("服务器开启,等待客户端连接..." ); while (true ) { Socket fs = fss.accept(); Socket ds = dss.accept(); new FileTask(fs).start(); new DataTask(ds).start(); } } catch (IOException e) { e.printStackTrace(); } } public static class FileTask extends Thread { private File file; private Socket fs; private BufferedReader br; public FileTask (Socket fs) { try { this .fs = fs; br = new BufferedReader(new InputStreamReader(fs.getInputStream())); } catch (IOException e) { e.printStackTrace(); } } @Override public void run () { try { String filename = br.readLine(); file = new File("D:\\wepull\\ServerReceive\\" + filename); ip_file_map.put(fs.getInetAddress(),file); long length = Long.parseLong(br.readLine()); ip_file_length_map.put(fs.getInetAddress(),length); br.close(); fs.close(); } catch (IOException e) { e.printStackTrace(); } } } public static class DataTask extends Thread { private InputStream is; private Socket ds; public DataTask (Socket s) { this .ds = s; try { this .is = ds.getInputStream(); } catch (IOException e) { e.printStackTrace(); } } @Override public void run () { try { InetAddress inetAddress = ds.getInetAddress(); File file = ip_file_map.get(inetAddress); FileOutputStream fos = new FileOutputStream(file); byte [] buf = new byte [1024 ]; int len = 0 ; while ((len = is.read(buf)) != -1 ) { fos.write(buf, 0 , len); } System.out.println("已经成功接收来自" + ds.getInetAddress() + "的文件,文件名:" +file.getName()); fos.flush(); fos.close(); is.close(); ds.close(); if (ip_file_length_map.get(inetAddress).equals(file.length())){ System.out.println("文件接收正确!" ); }else { System.out.println("文件接收不完整!" ); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } }
client
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 import java.io.*;import java.net.Socket;import java.util.Scanner;public class FClient { public static void main (String[] args) { Scanner sc = new Scanner(System.in); String f = sc.nextLine(); File file = new File(f); if (file.exists()) { try { Socket fs = new Socket("127.0.0.1" , 30001 ); PrintStream ps = new PrintStream(fs.getOutputStream()); ps.println(file.getName()); ps.println(file.length()); ps.flush(); ps.close(); Socket ds = new Socket("127.0.0.1" ,30002 ); OutputStream os = ds.getOutputStream(); FileInputStream fis = new FileInputStream(file); byte [] buf = new byte [1024 ]; int len = 0 ; while ((len = fis.read(buf)) != -1 ) { os.write(buf, 0 , len); } os.flush(); fis.close(); os.close(); } catch (IOException e) { e.printStackTrace(); } } else { System.out.println("文件不存在!" ); } } }