采用socket传输文件
客户端输入文件的地址,服务端判断文件存在,就将文件传输到客户端
package com.fly.socket; import java.io.BufferedInputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; public class Server { public static void main(String[] args) { try { int i=0; ServerSocket serverSocket=new ServerSocket(9992); while(true){ System.out.println("服务器已启动!"); Socket socket =serverSocket.accept(); Thread thread=new Thread(new ThreadHandler(socket),"Thread-"+i++); thread.start(); } } catch (IOException e) { e.printStackTrace(); } } } class ThreadHandler implements Runnable { private Socket socket; public ThreadHandler(Socket socket) { this.socket = socket; } @Override public void run() { DataOutputStream dataOutputStream=null; DataInputStream dataInputStream=null; DataInputStream localRead =null; try { dataOutputStream=new DataOutputStream(socket.getOutputStream()); dataInputStream=new DataInputStream(new BufferedInputStream(socket.getInputStream())); String filePath = dataInputStream.readUTF(); File file=new File(filePath); //判断文件是否存在 if(!file.exists()){ return; }else{ //文件名 String fileName = file.getName(); dataOutputStream.writeUTF(fileName); dataOutputStream.flush(); //文件大小 long length = file.length(); dataOutputStream.writeUTF(String.valueOf(length)); dataOutputStream.flush(); System.out.println("开始向 "+Thread.currentThread().getName()+ " 发送文件,文件名:"+fileName+" 文件大小"+length); localRead =new DataInputStream(new BufferedInputStream(new FileInputStream(file))); byte[] bytes=new byte[4096]; while (true){ int read=0; if(localRead!=null){ read = localRead.read(bytes); } if(read==-1){ break; } dataOutputStream.write(bytes,0,read); dataOutputStream.flush(); } System.out.println("向 "+Thread.currentThread().getName()+" 发送文件完毕!"); } } catch (IOException e) { e.printStackTrace(); }finally { try { localRead.close(); dataOutputStream.close(); dataInputStream.close(); socket.close(); } catch (IOException e) { e.printStackTrace(); } } } }
package com.fly.socket; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.FileOutputStream; import java.io.IOException; import java.net.InetSocketAddress; import java.net.Socket; import java.net.SocketAddress; import java.util.Scanner; public class Client { public static void main(String[] args) { Scanner in = new Scanner(System.in); DataInputStream dateInputStream=null; DataOutputStream dataOutputStream=null; DataOutputStream localWrite=null; Socket socket=null; try { socket=new Socket(); SocketAddress address = new InetSocketAddress("127.0.0.1", 9992); socket.connect(address); //输入流 dateInputStream = new DataInputStream(new BufferedInputStream(socket.getInputStream())); //输出流 dataOutputStream = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream())); System.out.println("请输入文件路径:"); String filePath = in.next(); dataOutputStream.writeUTF(filePath); dataOutputStream.flush(); String fileName = dateInputStream.readUTF(); String length=dateInputStream.readUTF(); System.out.println("文件名:"+fileName+" 文件大小:"+length); localWrite=new DataOutputStream(new BufferedOutputStream(new FileOutputStream("F:/"+fileName))); byte[] bytes=new byte[4096]; while (true){ int read=0; if(dateInputStream!=null){ read = dateInputStream.read(bytes); } if(read==-1){ break; } localWrite.write(bytes,0,read); localWrite.flush(); } System.out.println("文件传输完毕!"); } catch (IOException e) { e.printStackTrace(); }finally { in.close(); try { localWrite.close(); dataOutputStream.close(); dateInputStream.close(); socket.close(); } catch (IOException e) { e.printStackTrace(); } } } }