Tcp: 上传图片(自编源码)

来源:互联网 发布:java难不难学 编辑:程序博客网 时间:2024/04/29 00:12
 
import java.io.*;import java.net.*;class L{public static void main(String args[]) throws Exception{Socket s=new Socket("192.168.1.101",10006);FileInputStream fis=new FileInputStream("untitled1.jpg");OutputStream os=s.getOutputStream();    byte[] buf=new byte[1024];int ch=0;while((ch=fis.read(buf))!=-1){            os.write(buf,0,ch);}        s.shutdownOutput();InputStream is=s.getInputStream();byte[] bufIn=new byte[1024];int num=is.read(bufIn);System.out.println(new String(bufIn,0,num));fis.close();os.close();is.close();}}class L1{public static void main(String args[]) throws Exception{ServerSocket ss=new ServerSocket(10006);Socket s=ss.accept();InputStream is=s.getInputStream();FileOutputStream fos=new FileOutputStream("mm.jpg");byte[] buf=new byte[1024];int len=0;while((len=is.read(buf))!=-1){fos.write(buf,0,len);}OutputStream os=s.getOutputStream();os.write("上传成功".getBytes());ss.close();s.close();fos.close();}}

原创粉丝点击