上传图片Tcp交互
Posted windzly
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了上传图片Tcp交互相关的知识,希望对你有一定的参考价值。
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(); } }
以上是关于上传图片Tcp交互的主要内容,如果未能解决你的问题,请参考以下文章