码迷,mamicode.com
首页 > Web开发 > 详细

上传图片Tcp交互

时间:2019-12-01 10:06:01      阅读:79      评论:0      收藏:0      [点我收藏+]

标签:void   buffered   hot   fileinput   div   tcp   java   highlight   上传   

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交互

标签:void   buffered   hot   fileinput   div   tcp   java   highlight   上传   

原文地址:https://www.cnblogs.com/WINDZLY/p/11964763.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!