再现理想 发表于 2015-1-18 11:08:01

JAVA网站制作之功能相似于java.net会见http服务器程序...

关于第二点:俺问问你,如果是企业级项目的话,诸如RMI,EJB,等一些关键技术,这些难道都不需要学么?如果光是使用jsp,servlet,javabean的话。程序|会见|服务器packagenet.sonyhome.net;

importjava.io.*;
importjava.net.*;
importjava.util.*;
importjava.text.*;
/**
*一个用来会见http服务器的工具。功效相似于java.net中的谁人。但要强,这个对Post办法的撑持更好。
*实在也不克不及说是我写的。不记得从哪儿找来的程序,稍事修正了一下。以是如今程序的布局都忘啦。
*不外有一点是一定的,那就是能够用。呵呵。
*客岁我做的Java版的精髓区就是用这个类来会见的。
*Creationdate:(2001-8-2423:57:14)
*@author:SonyMusic
*/
publicclassHttpConnection{
privateURLurl=null;
//privatebooleandoInput=true;
//privatebooleandoOutput=true;

privatebooleanusePost=false;

privatebooleanuseCaches=false;

privateVectorreqHeaderNames=newVector();
privateVectorreqHeaderValues=newVector();
privateVectorresHeaderNames=null;
privateVectorresHeaderValues=null;
privateSocketsocket=null;
privateOutputStreamout=null;
privateInputStreamin=null;
privatebooleanuseHttp11=false;

privatebooleanconnected=false;

privatebooleaninputStarted=false;

HashtablepostData=newHashtable();
HashtablegetData=newHashtable();

/**
*HttpConnectionconstructorcomment.
*/
publicHttpConnection(URLurl){
super();
this.url=url;
}
/**
*Insertthemethodsdescriptionhere.
*Creationdate:(2001-8-251:16:52)
*@paramnamejava.lang.String
*@paramvaluejava.lang.String
*/
publicvoidaddGet(Stringname,Stringvalue){
getData.put(name,value);
}
/**
*Insertthemethodsdescriptionhere.
*Creationdate:(2001-8-251:16:52)
*@paramnamejava.lang.String
*@paramvaluejava.lang.String
*/
publicvoidaddPost(Stringname,Stringvalue){
postData.put(name,value);
}
publicvoidclose()throwsIOException{
if(!connected)
return;
out.close();
if(inputStarted)
in.close();
socket.close();
}
publicvoidconnect()throwsIOException{
if(connected)
return;
if(!useCaches){
setRequestProperty("Pragma","no-cache");
//setRequestProperty("Cache-Control","no-cache,must-revalidate");
//setRequestProperty("Expires","Mon,26Jul199705:00:00GMT");
}
Stringprotocol=url.getProtocol();
if(!protocol.equals("http"))
thrownewUnknownServiceException("unknownprotocol");
Stringhost=url.getHost();
intport=url.getPort();
if(port==-1)
port=80;
Stringfile=url.getFile();

socket=newSocket(host,port);
out=socket.getOutputStream();
PrintStreampout=newPrintStream(out);

Stringmethod;
if(usePost){
method="POST";
setRequestProperty("Content-type","application/x-www-form-urlencoded");
intlen=getPostDataLength();
setRequestProperty("Content-length",String.valueOf(getPostDataLength()));

}
else
method="GET";
if(getGetDataLength()>0){
file+="?"+getGetDataString();
}
pout.println(method+""+file+"HTTP/1.0");

for(inti=0;i<reqHeaderNames.size();++i){
Stringname=(String)reqHeaderNames.elementAt(i);
Stringvalue=(String)reqHeaderValues.elementAt(i);
pout.println(name+":"+value);
}
pout.println("");
if(usePost){
Stringttt=getPostDataString();
pout.println(getPostDataString());
}

pout.flush();

connected=true;
}
/**
*Insertthemethodsdescriptionhere.
*Creationdate:(2001-8-252:06:07)
*@returnboolean
*@exceptionjava.lang.IllegalStateExceptionTheexceptiondescription.
*/
publicbooleancontentIsText()throwsIOException{
Stringtype=getContentType();
if(type.startsWith("text"))
returntrue;
returnfalse;
}
/**
*Insertthemethodsdescriptionhere.
*Creationdate:(2001-8-252:20:31)
*@returnbyte[]
*/
publicbyte[]getByteArray()throwsIOException{
DataInputStreamdin=newDataInputStream(getInputStream());

byte[]ret;
byte[]b=newbyte;
intoff=0,len=0;

ByteArrayOutputStreambos=newByteArrayOutputStream();
while((len=din.read(b,off,1024))>0){
bos.write(b,0,len);
if(len<1024)
break;
}
bos.flush();
bos.close();
returnbos.toByteArray();
}
//Getsthecontentlength.Returns-1ifnotknown.
publicintgetContentLength()throwsIOException{
returngetHeaderFieldInt("content-length",-1);
}
///Getsthecontenttype.Returnsnullifnotknown.
publicStringgetContentType()throwsIOException{
returngetHeaderField("content-type");
}
/**
*Insertthemethodsdescriptionhere.
*Creationdate:(2001-8-251:18:23)
*@returnjava.lang.String
*/
publicintgetGetDataLength(){
returngetGetDataString().length();
}
/**
*Insertthemethodsdescriptionhere.
*Creationdate:(2001-8-251:18:23)
*@returnjava.lang.String
*/
publicStringgetGetDataString(){
StringBufferbuf=newStringBuffer();
Enumerationenu=getData.keys();
while(enu.hasMoreElements()){
Stringkey=(String)(enu.nextElement());
Stringvalue=(String)(getData.get(key));
if(buf.length()>0)
buf.append("&");
buf.append(key);
buf.append("=");
buf.append(URLEncoder.encode(value));
}
returnbuf.toString();
}
publicStringgetHeaderField(Stringname)throwsIOException{
if(resHeaderNames==null)
startInput();
inti=resHeaderNames.indexOf(name.toLowerCase());
if(i==-1)
returnnull;
return(String)resHeaderValues.elementAt(i);
}
publiclonggetHeaderFieldDate(Stringname,longdef)throwsIOException{
try{
returnDateFormat.getDateInstance().parse(getHeaderField(name)).getTime();
}
catch(ParseExceptione){
thrownewIOException(e.toString());
}
}
publicintgetHeaderFieldInt(Stringname,intdef)throwsIOException{
try{
returnInteger.parseInt(getHeaderField(name));
}
catch(NumberFormatExceptiont){
returndef;
}
}
/**
*Insertthemethodsdescriptionhere.
*Creationdate:(2001-8-251:12:09)
*@returnjava.util.Enumeration
*/
publicEnumerationgetHeaderNames(){
returnresHeaderNames.elements();
}
publicInputStreamgetInputStream()throwsIOException{
startInput();
returnin;
}
publicOutputStreamgetOutputStream()throwsIOException{
connect();
returnout;
}
/**
*Insertthemethodsdescriptionhere.
*Creationdate:(2001-8-251:18:23)
*@returnjava.lang.String
*/
publicintgetPostDataLength(){
returngetPostDataString().length();
}
/**
*Insertthemethodsdescriptionhere.
*Creationdate:(2001-8-251:18:23)
*@returnjava.lang.String
*/
publicStringgetPostDataString(){
StringBufferbuf=newStringBuffer();
Enumerationenu=postData.keys();
while(enu.hasMoreElements()){
Stringkey=(String)(enu.nextElement());
Stringvalue=(String)(postData.get(key));
if(buf.length()>0)
buf.append("&");
buf.append(key);
buf.append("=");
buf.append(URLEncoder.encode(value));
}
returnbuf.toString();
}
publicStringgetRequestProperty(Stringname){
if(connected)
thrownewIllegalAccessError("alreadyconnected");
inti=reqHeaderNames.indexOf(name);
if(i==-1)
returnnull;
return(String)reqHeaderValues.elementAt(i);
}
publicURLgetURL(){
returnurl;
}
publicbooleangetUseCaches(){
returnuseCaches;
}
publicbooleangetUseHttp11(){
returnuseHttp11;
}
/**
*Insertthemethodsdescriptionhere.
*Creationdate:(2001-8-251:48:15)
*@returnboolean
*/
publicbooleanisUsePost(){
returnusePost;
}
/**
*Insertthemethodsdescriptionhere.
*Creationdate:(2001-8-250:15:53)
*@paramargsjava.lang.String[]
*/
publicstaticvoidmain(String[]args){
try{
/*
URLurl=newURL("http","192.168.0.3","/Post.php");
HttpConnectioncon=newHttpConnection(url);
con.setUsePost(true);
con.setUseCaches(false);
//con.setRequestProperty("Connection","Keep-Alive");

con.addGet("TextField","你好");
con.addGet("Submit","submit");

con.connect();
//ByteArrayOutputStreambos=con.getByteArray();
byte[]ret=con.getByteArray();
System.out.println(newString(ret));

System.out.println("");
Enumerationenu=con.getHeaderNames();
while(enu.hasMoreElements()){
StringheaderName=(String)(enu.nextElement());
System.out.println(headerName+":"+con.getHeaderField(headerName));
}
con.close();
*/

URLurl=newURL("http","192.168.0.3","/codemaker/IMAGES/BO.GIF");
HttpConnectioncon=newHttpConnection(url);
con.connect();

FileOutputStreamfos=newFileOutputStream("d:o.gif");
fos.write(con.getByteArray());
fos.flush();
fos.close();

System.out.println("");
Enumerationenu=con.getHeaderNames();
while(enu.hasMoreElements()){
StringheaderName=(String)(enu.nextElement());
System.out.println(headerName+":"+con.getHeaderField(headerName));
}
con.close();

}
catch(Exceptione){
e.printStackTrace();
}
}
/**
*Insertthemethodsdescriptionhere.
*Creationdate:(2001-8-251:16:52)
*@paramnamejava.lang.String
*@paramvaluejava.lang.String
*/
publicvoidremoveGet(Stringname){
getData.remove(name);
}
/**
*Insertthemethodsdescriptionhere.
*Creationdate:(2001-8-251:16:52)
*@paramnamejava.lang.String
*@paramvaluejava.lang.String
*/
publicvoidremovePost(Stringname){
postData.remove(name);
}
publicvoidsetRequestProperty(Stringname,Stringvalue){
if(connected)
thrownewIllegalAccessError("alreadyconnected");
reqHeaderNames.addElement(name);
reqHeaderValues.addElement(value);
}
publicvoidsetUseCaches(booleanuseCaches){
if(connected)
thrownewIllegalAccessError("alreadyconnected");
this.useCaches=useCaches;
}
publicvoidsetUseHttp11(booleanuseHttp11){
if(connected)
thrownewIllegalAccessError("alreadyconnected");
this.useHttp11=useHttp11;
}
/**
*Insertthemethodsdescriptionhere.
*Creationdate:(2001-8-251:48:15)
*@paramnewUsePostboolean
*/
publicvoidsetUsePost(booleannewUsePost){
if(connected)
thrownewIllegalAccessError("alreadyconnected");
usePost=newUsePost;
}
privatevoidstartInput()throwsIOException{
connect();
if(inputStarted)
return;
in=socket.getInputStream();
resHeaderNames=newVector();
resHeaderValues=newVector();
DataInputStreamdin=newDataInputStream(in);
Stringline;

//Readandignorethestatusline.
line=din.readLine();
//Readandsavetheheaderlines.
while(true){
line=din.readLine();
if(line==null||line.length()==0)
break;
intcolonBlank=line.indexOf(":");
if(colonBlank!=-1){
Stringname=line.substring(0,colonBlank);
Stringvalue=line.substring(colonBlank+2);
resHeaderNames.addElement(name.toLowerCase());
resHeaderValues.addElement(value);
}
}

inputStarted=true;
}
/**
*ReturnsaStringthatrepresentsthevalueofthisobject.
*@returnastringrepresentationofthereceiver
*/
publicStringtoString(){
//Insertcodetoprintthereceiverhere.
//Thisimplementationforwardsthemessagetosuper.Youmayreplaceorsupplementthis.
returnthis.getClass().getName()+":"+url;
}
}
主要缺点就是:速度比较慢,没有C和C++快

乐观 发表于 2015-1-20 14:53:22

你可以去承接一些项目做了,一开始可能有些困难,可是你有技术积累,又考虑周全,接下项目来可以迅速作完,相信大家以后都会来找你的,所以Money就哗啦啦的。。。。。。

深爱那片海 发表于 2015-1-27 13:39:56

在全球云计算和移动互联网的产业环境下,Java更具备了显著优势和广阔前景。

透明 发表于 2015-2-5 05:22:21

一般学编程语言都是从C语开始学的,我也不例外,但还是可能不学过程语言而直接学面向对象语言的,你是刚接触语言,还是从C开始学比较好,基础会很深点,如果你直接学习JAVA也能上手,一般大家在学语言的时候都记一些语言的关键词,常有的包和接口等。再去做逻辑代码的编写,以后的学习过程都是从逻辑代码编写中提升的,所以这方面都是经验积累的。你要开始学习就从

灵魂腐蚀 发表于 2015-2-11 05:07:42

Java语言支持Internet应用的开发,在基本的Java应用编程接口中有一个网络应用编程接口(java net),它提供了用于网络应用编程的类库,包括URL、URLConnection、Socket、ServerSocket等。Java的RMI(远程方法激活)机制也是开发分布式应用的重要手段。

若相依 发表于 2015-3-1 21:35:26

科学超级计算机、移动电话和互联网,同时拥有全球最大的开发者专业社群。

admin 发表于 2015-3-10 23:53:19

是一种突破用户端机器环境和CPU

仓酷云 发表于 2015-3-17 16:10:30

Java自面世后就非常流行,发展迅速,对C++语言形成了有力冲击。Java 技术具有卓越的通用性、高效性、平台移植性和安全性,广泛应用于个人PC、数据中心、游戏控制台

蒙在股里 发表于 2015-3-24 11:27:13

一般学编程语言都是从C语开始学的,我也不例外,但还是可能不学过程语言而直接学面向对象语言的,你是刚接触语言,还是从C开始学比较好,基础会很深点,如果你直接学习JAVA也能上手,一般大家在学语言的时候都记一些语言的关键词,常有的包和接口等。再去做逻辑代码的编写,以后的学习过程都是从逻辑代码编写中提升的,所以这方面都是经验积累的。你要开始学习就从
页: [1]
查看完整版本: JAVA网站制作之功效相似于java.net会见http服务器程序...