package photodemo; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.Socket; import java.net.UnknownHostException; public class send { public static void main(String[] args) throws UnknownHostException, IOException { // TODO Auto-generated method stub Socket s = new Socket("192.168.2.143",10006); BufferedInputStream bfr = new BufferedInputStream(new FileInputStream("d:\\1.jpg")); BufferedOutputStream bout = new BufferedOutputStream(s.getOutputStream()); int ch = 0; while((ch = bfr.read()) != -1) { bout.write(ch); bout.flush(); } s.shutdownOutput(); InputStream in = s.getInputStream(); byte buf[] = new byte[1024]; int len = in.read(buf); System.out.println(new String(buf,0,len)); s.close(); bout.close(); in.close(); bfr.close(); } } package photodemo; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.ServerSocket; import java.net.Socket; public class Server { public static void main(String[] args) throws IOException { // TODO Auto-generated method stub ServerSocket ss = new ServerSocket(10006); Socket s = ss.accept(); BufferedInputStream bin = new BufferedInputStream(s.getInputStream()); BufferedOutputStream bw = new BufferedOutputStream(new FileOutputStream("d:\\1copy.jpg")); int ch = 0; while((ch = bin.read()) != -1) { bw.write(ch); //bw.flush(); } OutputStream out = s.getOutputStream(); out.write("上传成功".getBytes()); ss.close(); bin.close(); s.close(); bw.close(); out.close(); } }
原文地址:https://www.cnblogs.com/WINDZLY/p/11964763.html
时间: 2024-11-06 09:25:16