------- android培训、java培训、期待与您交流! ----------
URL对象的使用
方法:
URL.openConnection();//里面封装了Socket对象,直接访问输入的地址
URL.getProtocl();获取此协议名称
URL.getHost();获取URL的主机名
URL.getPort();获取此URL的端口号
URL.getPath();获取此URL的路径部分
URL.getFile();获取此URL的文件名
URL.getQuery();获取此URL的查询部
/*
例子:URL对象的用法
*/
import java.net.*;
import java.io.*;
class URLDemo
{
public static void main(String[] args)throws Exception
{
URL url=new URL("http://www.baidu.com.cn");
URLConnection conn=url.openConnection();
System.out.println(conn);
InputStream in=conn.getInputStream();//
byte[] buf=new byte[1024];
int len=in.read(buf);
sop(new String(buf,0,len));
sop(url.getProtocol());
sop(url.getHost());
sop(url.getPort());
sop(url.getPath());
sop(url.getFile());
sop(url.getQuery());
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}
例子:上传图片到服务器
/*
例子:上传图片到服务器
*/
import java.io.*;
import java.net.*;
class PicClient
{
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("该文件有问题,要么不存在,要么不是文件");
}
if(!file.getName().endsWith(".jpg"))
{
System.out.println("图片格式错误,请重新选择");
}
if(file.length()>1024*1024*5)
{
System.out.println("图片过大,没安好心");
}
Socket s=new Socket("169.254.0.84",1006);
FileInputStream fis=new FileInputStream(file);
InputStream fisin=s.getInputStream();
OutputStream fosout=s.getOutputStream();
byte[] buf=new byte[1024];
int len=0;
while((len=fis.read(buf))!=-1)
{
fosout.write(buf,0,len);
}
s.shutdownOutput();
byte[] buf2=new byte[1024];
int len2=0;
len2=fisin.read(buf2);
System.out.println(new String(buf2,0,len2));
fis.close();
s.close();
}
}
class PicThread implements Runnable
{
private Socket s;
PicThread(Socket s)
{
this.s=s;
}
public void run()
{
int count=1;
String ip=s.getInetAddress().getHostAddress();
try
{
System.out.println("connect..."+ip);
File file=new File(ip+"("+(count)+")"+".jpg");
while(file.exists())
file=new File(ip+"("+(count++)+")"+".jpg");
FileOutputStream fos=new FileOutputStream(file);
InputStream fisin=s.getInputStream();
byte[] buf=new byte[1024];
int len=0;
while((len=fisin.read(buf))!=-1)
{
fos.write(buf,0,len);
}
OutputStream fosout=s.getOutputStream();
fosout.write("上传图片成功".getBytes());
fos.close();
s.close();
}
catch(Exception e)
{
throw new RuntimeException("文件上传失败");
}
}
}
class PicServer
{
public static void main(String[] args)throws Exception
{
ServerSocket ss=new ServerSocket(1006);
while(true)
{
Socket s=ss.accept();
new Thread(new PicThread(s)).start();
}
//ss.close();
}
}
/*
键盘录入用户名,发送到服务器如果服务器上面有这个用户,则登录成功,如果没有则登录失败
*/
import java.io.*;
import java.net.*;
class Client
{
public static void main(String[] args)throws Exception
{
Socket s=new Socket("169.254.0.84",10008);
BufferedReader bufr=new BufferedReader(new InputStreamReader(System.in));
PrintWriter ps=new PrintWriter(s.getOutputStream(),true);
BufferedReader bufint=new BufferedReader(new InputStreamReader(s.getInputStream()));
for(int x=0;x<3;x++)
{
String line=bufr.readLine();
if(line==null)
break;
ps.println(line);
String info=null;
info=bufint.readLine();
System.out.println(info);
if(info.contains("欢迎"))
break;
}
s.close();
bufr.close();
}
}
class UserThread implements Runnable
{
private Socket s;
UserThread(Socket s)
{
this.s=s;
}
public void run()
{
String ip=s.getInetAddress().getHostAddress();
System.out.println("connect..."+ip);
try
{
for(int x=0;x<3;x++)
{
BufferedReader bufr=new BufferedReader(new FileReader("user.txt"));
BufferedReader bufrint=new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter ps=new PrintWriter(s.getOutputStream(),true);
String name=null;
String info=null;
name=bufrint.readLine();
if(name==null)
break;
boolean flag=false;
while((info=bufr.readLine())!=null)
{
if(info.equals(name))
{
flag=true;
break;
}
}
if(flag)
{
System.out.println(name+",已登录");
ps.println(name+",欢迎光临");
break;
}
else
{
System.out.println(name+",尝试登录");
ps.println(name+",用户名不存在");
}
}
s.close();
}
catch(Exception e)
{
throw new RuntimeException(ip+"校验失败");
}
}
}
class Server
{
public static void main(String[] args)throws Exception
{
ServerSocket ss=new ServerSocket(10008);
while(true)
{
Socket s=ss.accept();
new Thread(new UserThread(s)).start();
}
}
}
------- android培训 、 java培训 、期待与您交流! ----------