网络编程2

-通过TCP协议实现图片文件上传的小程序
服务端:


package hcy;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class UploadPicServer {
	
	public static void main(String[] args) throws Exception{
		
		ServerSocket ss= new ServerSocket(10008);
		Socket s= ss.accept();
		
		InputStream in= s.getInputStream();
		FileOutputStream fos= new FileOutputStream("server.mp3");
		
		byte[] buf= new byte[1024];
		int len= 0;
		while((len= in.read(buf))!= -1){
			
			fos.write(buf,0,len);
		}
		
		OutputStream out= s.getOutputStream();
		out.write("上传成功".getBytes());
		
		fos.close();
		s.close();
		ss.close();
	}

}



客户端:

package hcy;

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class UploadPicClient {
	
	public static void main(String[] args) throws Exception{
		
		Socket s= new Socket("10.0.31.236",10008);
		FileInputStream fis= new FileInputStream("client.mp3");
		OutputStream out= s.getOutputStream();
		byte[] buf= new byte[1024];
		int len= 0;
		while((len=fis.read(buf))!= -1){
			out.write(buf,0,len);
		}
		s.shutdownOutput();//----要记得添加数据发送完成的标记符
		
		InputStream in= s.getInputStream();
		byte[] bufIn= new byte[1024];
		
		int num= in.read(bufIn);
		System.out.println(new String(bufIn,0,num));
		
		fis.close();
		s.close();
	}

}


-实现客户端并发上传文件的程序

package hcy;

import java.io.*;
import java.net.*;

class PicThread implements Runnable{
	
	private Socket s;
	public PicThread(Socket s) {
		this.s= s;
	}
	
	public void run(){
		//获取客户端的IP地址
		String ip= s.getInetAddress().getHostAddress();
		System.out.println("客户端IP地址:"+ip);
		
		int count= 1;
		
		try {
			InputStream in= s.getInputStream();
			File file= new File(ip+"("+(count++)+")"+".jpg");
			
			while(file.exists())
				file= new File(ip+"("+(count++)+")"+".jpg");
			FileOutputStream fos= new FileOutputStream(file);
			
			byte[] buf= new byte[1024];
			
			int len= 0;
			while((len= in.read(buf))!= -1){
				fos.write(buf,0,len);
			}
			
			OutputStream out= s.getOutputStream();
			out.write("上传成功".getBytes());
			
			fos.close();
			s.close();
			
		} catch (Exception e) {
			throw new RuntimeException(ip+"上传失败");
		}
		
	}
}
// 多线程上传服务端程序
class UploadPicByThreadServer{
	
	public static void main(String[] args) throws Exception{
		
		ServerSocket ss= new ServerSocket(11002);
		while(true){
			Socket s= ss.accept();
			new Thread(new PicThread(s)).start();
		}
	}
}

//多线程上传客户端程序
class UploadPicByThreadClient {
	
	public static void main(String[] args) throws Exception{
		
		//上传前判断文件是否合法
		if(args.length!= 1){
			System.out.println("请选择一个JPG格式的图片上传");
			return;
		}
		
		File file= new File(args[0]);
		if(!(file.exists() && file.isFile())){
			System.out.println("上传失败,要么文件不存在,要么你上传的不是文件");
			return;
		}
		if(!file.getName().endsWith(".jpg")){
			System.out.println("图片格式错误,请重新选择");
			return;
		}
		if(file.length()>1024*1024*5){
			System.out.println("文件过大,你这小子估计不安好心--");
			return;
		}
		
		Socket s= new Socket("10.0.31.236",11002);
		
		
		FileInputStream fis= new FileInputStream(file);
		OutputStream out= s.getOutputStream();
		
		byte[] buf= new byte[1024];
		int len= 0;
		while((len=fis.read(buf))!= -1){
			out.write(buf, 0, len);
		}
		//-----告诉服务端数据写完-----(别漏了)
		s.shutdownOutput();
		
		InputStream in= s.getInputStream();
		byte[] bufIn= new byte[1024];
		
		int num= in.read(bufIn);
		System.out.println(new String(bufIn,0,num));
		
		fis.close();
		s.close();
	}

}



-通过学习TCP协议,我们自定义一个浏览器服务端
服务端:


package hcy;

import java.io.*;
import java.net.*;

//readme----第24天-04-网络编程(浏览器-自定义服务端)
public class ServerDemo {

	public static void main(String[] args) throws Exception {
		
		ServerSocket ss= new ServerSocket(10009);
		Socket s= ss.accept();
		String ip= s.getInetAddress().getHostAddress();
		
		//读取浏览器的头文件
		InputStream in= s.getInputStream();
		
		byte[] buf= new byte[1024];
		int len= in.read(buf);
		System.out.println(new String(buf,0,len));
		
		PrintWriter out= new PrintWriter(s.getOutputStream(),true);
		out.println("<font color='green' size='8'>你好-客户端,你的IP地址为:"+ip+"</font>");
		
		s.close();
		ss.close();

	}

}



-通过学习TCP协议,我们自定义一个浏览器客户端

import java.io.*;
import java.net.*;

public class MyIE {
	public static void main(String[] args)throws Exception{

		Socket s= new Socket("10.0.31.236",8080);

		PrintWriter out = new PrintWriter(s.getOutputStream(),true);

		out.println("GET /testWeb/demo.html HTTP/1.1");
		out.println("Accept: */*");
		out.println("Accept-Language: zh-cn");
		out.println("Host:10.0.31.236:11004");
		out.println("Connetion: closed");

		out.println();
		out.println();

		BufferedReader bufr= new BufferedReader(new InputStreamReader(s.getInputStream()));
		
		String line=null;
		while((line=bufr.readLine())!= null){
			System.out.println(line);
		}
		s.close();
	}

}


-学习UrlConnection类
package hcy;

import java.io.*;
import java.net.*;

public class UrlConnectionDemo {

	public static void main(String[] args) throws Exception {
		
		URL url= new URL("http://10.0.31.236:8080/testWeb/demo.html");
		
		URLConnection conn= url.openConnection();
		System.out.println(conn);
		
		InputStream in= conn.getInputStream();
		
		byte[] buf= new byte[1024];
		int len= in.read(buf);
		
		System.out.println(new String(buf,0,len));

	}

}



猜你喜欢

转载自huangcaiyan.iteye.com/blog/2233713
今日推荐